Fixed hour skew on price modifier (#959)

* Use local timezone in price config

* Additional changes for previous commit

* Use CET/CEST for energy prices collected from server
This commit is contained in:
Gunnar Skjold
2025-06-05 07:40:29 +02:00
committed by GitHub
parent 3eaefefd26
commit 16f9ed7ecb
3 changed files with 16 additions and 8 deletions

View File

@@ -71,6 +71,7 @@ public:
PriceService(Stream*); PriceService(Stream*);
#endif #endif
void setup(PriceServiceConfig&); void setup(PriceServiceConfig&);
void setTimezone(Timezone* tz);
bool loop(); bool loop();
char* getToken(); char* getToken();
@@ -114,6 +115,7 @@ private:
std::vector<PriceConfig> priceConfig; std::vector<PriceConfig> priceConfig;
Timezone* tz = NULL; Timezone* tz = NULL;
Timezone* entsoeTz = NULL;
static const uint16_t BufferSize = 256; static const uint16_t BufferSize = 256;
char* buf; char* buf;

View File

@@ -32,7 +32,8 @@ PriceService::PriceService(Stream* Debug) : priceConfig(std::vector<PriceConfig>
// Entso-E uses CET/CEST // Entso-E uses CET/CEST
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60};
tz = new Timezone(CEST, CET); entsoeTz = new Timezone(CEST, CET);
tz = entsoeTz;
tomorrowFetchMinute = 15 + random(45); // Random between 13:15 and 14:00 tomorrowFetchMinute = 15 + random(45); // Random between 13:15 and 14:00
} }
@@ -72,6 +73,10 @@ void PriceService::setup(PriceServiceConfig& config) {
load(); load();
} }
void PriceService::setTimezone(Timezone* tz) {
this->tz = tz;
}
char* PriceService::getToken() { char* PriceService::getToken() {
return this->config->entsoeToken; return this->config->entsoeToken;
} }
@@ -153,24 +158,24 @@ float PriceService::getEnergyPriceForHour(uint8_t direction, time_t ts, int8_t h
int8_t pos = hour; int8_t pos = hour;
breakTime(tz->toLocal(ts), tm); breakTime(entsoeTz->toLocal(ts), tm);
while(tm.Hour > 0) { while(tm.Hour > 0) {
ts -= 3600; ts -= 3600;
breakTime(tz->toLocal(ts), tm); breakTime(entsoeTz->toLocal(ts), tm);
pos++; pos++;
} }
uint8_t hoursToday = 0; uint8_t hoursToday = 0;
uint8_t todayDate = tm.Day; uint8_t todayDate = tm.Day;
while(tm.Day == todayDate) { while(tm.Day == todayDate) {
ts += 3600; ts += 3600;
breakTime(tz->toLocal(ts), tm); breakTime(entsoeTz->toLocal(ts), tm);
hoursToday++; hoursToday++;
} }
uint8_t hoursTomorrow = 0; uint8_t hoursTomorrow = 0;
uint8_t tomorrowDate = tm.Day; uint8_t tomorrowDate = tm.Day;
while(tm.Day == tomorrowDate) { while(tm.Day == tomorrowDate) {
ts += 3600; ts += 3600;
breakTime(tz->toLocal(ts), tm); breakTime(entsoeTz->toLocal(ts), tm);
hoursTomorrow++; hoursTomorrow++;
} }
@@ -231,7 +236,7 @@ bool PriceService::loop() {
return false; return false;
tmElements_t tm; tmElements_t tm;
breakTime(tz->toLocal(t), tm); breakTime(entsoeTz->toLocal(t), tm);
if(currentDay == 0) { if(currentDay == 0) {
currentDay = tm.Day; currentDay = tm.Day;
@@ -402,7 +407,7 @@ float PriceService::getCurrencyMultiplier(const char* from, const char* to, time
PricesContainer* PriceService::fetchPrices(time_t t) { PricesContainer* PriceService::fetchPrices(time_t t) {
if(strlen(getToken()) > 0) { if(strlen(getToken()) > 0) {
tmElements_t tm; tmElements_t tm;
breakTime(tz->toLocal(t), tm); breakTime(entsoeTz->toLocal(t), tm);
time_t e1 = t - (tm.Hour * 3600) - (tm.Minute * 60) - tm.Second; // Local midnight time_t e1 = t - (tm.Hour * 3600) - (tm.Minute * 60) - tm.Second; // Local midnight
time_t e2 = e1 + SECS_PER_DAY; time_t e2 = e1 + SECS_PER_DAY;
tmElements_t d1, d2; tmElements_t d1, d2;
@@ -439,7 +444,7 @@ PricesContainer* PriceService::fetchPrices(time_t t) {
} }
} else if(hub) { } else if(hub) {
tmElements_t tm; tmElements_t tm;
breakTime(tz->toLocal(t), tm); breakTime(entsoeTz->toLocal(t), tm);
String data; String data;
snprintf_P(buf, BufferSize, PSTR("http://hub.amsleser.no/hub/price/%s/%d/%d/%d?currency=%s"), snprintf_P(buf, BufferSize, PSTR("http://hub.amsleser.no/hub/price/%s/%d/%d/%d?currency=%s"),

View File

@@ -977,6 +977,7 @@ void handleNtpChange() {
ws.setTimezone(tz); ws.setTimezone(tz);
ds.setTimezone(tz); ds.setTimezone(tz);
ea.setTimezone(tz); ea.setTimezone(tz);
ps->setTimezone(tz);
} }
config.ackNtpChange(); config.ackNtpChange();