Compare commits

...

3 Commits

Author SHA1 Message Date
Gunnar Skjold
b25564a89f Clear all unknown prices instead of just last 2022-04-03 13:27:21 +02:00
Gunnar Skjold
191d9fa562 Added month max to mqtt 2022-04-01 20:39:22 +02:00
Gunnar Skjold
e009b4e54e Fixed price update every hour 2022-04-01 07:58:58 +02:00
8 changed files with 42 additions and 28 deletions

View File

@@ -91,34 +91,39 @@ bool EntsoeApi::loop() {
time_t t = time(nullptr);
if(t < BUILD_EPOCH) return false;
bool ret = false;
tmElements_t tm;
breakTime(tz->toLocal(t), tm);
if(currentHour != tm.Hour) {
currentHour = tm.Hour;
ret = today != NULL; // Only trigger MQTT publish if we have todays prices.
}
if(midnightMillis == 0) {
if(t <= 0) return false; // NTP not ready
time_t epoch = tz->toLocal(t);
breakTime(epoch, tm);
if(tm.Year > 50) { // Make sure we are in 2021 or later (years after 1970)
uint32_t curDayMillis = (((((tm.Hour * 60) + tm.Minute) * 60) + tm.Second) * 1000);
midnightMillis = now + (SECS_PER_DAY * 1000) - curDayMillis + 1000; // Adding 1s to ensure we have passed midnight
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Setting midnight millis %lu\n", midnightMillis);
}
} else if(now > midnightMillis) {
uint32_t curDayMillis = (((((tm.Hour * 60) + tm.Minute) * 60) + tm.Second) * 1000);
midnightMillis = now + (SECS_PER_DAY * 1000) - curDayMillis;
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Setting midnight millis %lu\n", midnightMillis);
currentDay = tm.Day;
return false;
} else if(now > midnightMillis && currentDay != tm.Day) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Rotating price objects at %lu\n", t);
delete today;
today = tomorrow;
tomorrow = NULL;
if(today != NULL) delete today;
if(tomorrow != NULL) {
today = tomorrow;
tomorrow = NULL;
}
currentDay = tm.Day;
midnightMillis = 0; // Force new midnight millis calculation
return true;
} else {
breakTime(t, tm);
breakTime(t, tm); // Break UTC to find UTC midnight
if(today == NULL && (lastTodayFetch == 0 || now - lastTodayFetch > 60000)) {
lastTodayFetch = now;
time_t e1 = t - (tm.Hour * 3600) - (tm.Minute * 60) - tm.Second;
time_t e1 = t - (tm.Hour * 3600) - (tm.Minute * 60) - tm.Second; // UTC midnight
time_t e2 = e1 + SECS_PER_DAY;
tmElements_t d1, d2;
breakTime(tz->toUTC(e1), d1);
breakTime(tz->toUTC(e1), d1); // To get day and hour for CET/CEST at UTC midnight
breakTime(tz->toUTC(e2), d2);
snprintf(buf, BufferSize, "%s?securityToken=%s&documentType=A44&periodStart=%04d%02d%02d%02d%02d&periodEnd=%04d%02d%02d%02d%02d&in_Domain=%s&out_Domain=%s",
@@ -185,7 +190,7 @@ bool EntsoeApi::loop() {
}
}
}
return false;
return ret;
}
bool EntsoeApi::retrieve(const char* url, Stream* doc) {

View File

@@ -32,6 +32,7 @@ private:
RemoteDebug* debugger;
EntsoeConfig* config = NULL;
uint8_t currentDay = 0, currentHour = 0;
uint32_t tomorrowFetchMillis = 36000000; // Number of ms before midnight. Default fetch 10hrs before midnight (14:00 CE(S)T)
uint64_t midnightMillis = 0;
uint64_t lastTodayFetch = 0;

View File

@@ -24,7 +24,8 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
data->getActiveImportPower(),
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold()
ea->getCurrentThreshold(),
ea->getMonthMax()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 2) {
@@ -51,7 +52,8 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
data->getL3Voltage(),
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold()
ea->getCurrentThreshold(),
ea->getMonthMax()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 3) {
@@ -83,7 +85,8 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
data->getMeterTimestamp(),
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold()
ea->getCurrentThreshold(),
ea->getMonthMax()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 4) {
@@ -119,7 +122,8 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
data->getMeterTimestamp(),
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold()
ea->getCurrentThreshold(),
ea->getMonthMax()
);
return mqtt->publish(topic, json);
}

View File

@@ -75,6 +75,7 @@ bool RawMqttHandler::publish(AmsData* data, AmsData* meterState, EnergyAccountin
mqtt->publish(topic + "/realtime/import/hour", String(ea->getUseThisHour(), 3));
mqtt->publish(topic + "/realtime/import/day", String(ea->getUseToday(), 2));
mqtt->publish(topic + "/realtime/import/threshold", String(ea->getCurrentThreshold(), 10), true, 0);
mqtt->publish(topic + "/realtime/import/monthmax", String(ea->getMonthMax(), 3), true, 0);
return true;
}
@@ -180,7 +181,6 @@ bool RawMqttHandler::publishPrices(EntsoeApi* eapi) {
float val = values[i];
if(val == ENTSOE_NO_VALUE) {
mqtt->publish(topic + "/price/" + String(i), "", true, 0);
break;
} else {
mqtt->publish(topic + "/price/" + String(i), String(val, 4), true, 0);
}

View File

@@ -12,6 +12,7 @@
"realtime" : {
"h" : %.2f,
"d" : %.1f,
"t" : %d
"t" : %d,
"x" : %.2f
}
}

View File

@@ -24,6 +24,7 @@
"realtime" : {
"h" : %.2f,
"d" : %.1f,
"t" : %d
"t" : %d,
"x" : %.2f
}
}

View File

@@ -29,6 +29,7 @@
"realtime" : {
"h" : %.2f,
"d" : %.1f,
"t" : %d
"t" : %d,
"x" : %.2f
}
}

View File

@@ -33,6 +33,7 @@
"realtime" : {
"h" : %.2f,
"d" : %.1f,
"t" : %d
"t" : %d,
"x" : %.2f
}
}