From 3a4fc707b03aaf08a96c0202a157764323975d97 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Wed, 10 Aug 2022 10:53:16 +0200 Subject: [PATCH] Show real time production --- src/AmsDataStorage.cpp | 4 +- src/EnergyAccounting.cpp | 73 +++++++++++++++++++++++++++--------- src/EnergyAccounting.h | 10 ++++- src/mqtt/JsonMqttHandler.cpp | 16 ++++++-- src/mqtt/RawMqttHandler.cpp | 2 + src/web/AmsWebServer.cpp | 3 ++ web/application.js | 6 +++ web/data.json | 9 +++-- web/index.html | 31 ++++++++++++++- web/json1.json | 4 +- web/json2.json | 4 +- web/json3.json | 4 +- web/json4.json | 4 +- 13 files changed, 137 insertions(+), 33 deletions(-) diff --git a/src/AmsDataStorage.cpp b/src/AmsDataStorage.cpp index 091eef04..75bf4b56 100644 --- a/src/AmsDataStorage.cpp +++ b/src/AmsDataStorage.cpp @@ -67,7 +67,7 @@ bool AmsDataStorage::update(AmsData* data) { } tmElements_t last; breakTime(day.lastMeterReadTime, last); - for(int i = last.Hour; i < utc.Hour; i++) { + for(int i = last.Hour; i <= utc.Hour; i++) { if(debugger->isActive(RemoteDebug::VERBOSE)) { debugger->printf("(AmsDataStorage) Clearing hour: %d\n", i); } @@ -90,7 +90,7 @@ bool AmsDataStorage::update(AmsData* data) { } tmElements_t last; breakTime(tz->toLocal(month.lastMeterReadTime), last); - for(int i = last.Day; i < ltz.Day; i++) { + for(int i = last.Day; i <= ltz.Day; i++) { if(debugger->isActive(RemoteDebug::VERBOSE)) { debugger->printf("(AmsDataStorage) Clearing day: %d\n", i); } diff --git a/src/EnergyAccounting.cpp b/src/EnergyAccounting.cpp index 031e2128..9e6dd087 100644 --- a/src/EnergyAccounting.cpp +++ b/src/EnergyAccounting.cpp @@ -80,6 +80,7 @@ bool EnergyAccounting::update(AmsData* amsData) { } use = 0; + produce = 0; costHour = 0; currentHour = local.Hour; @@ -106,19 +107,24 @@ bool EnergyAccounting::update(AmsData* amsData) { } unsigned long ms = this->lastUpdateMillis > amsData->getLastUpdateMillis() ? 0 : amsData->getLastUpdateMillis() - this->lastUpdateMillis; - float kwh = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0; + float kwhi = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0; + float kwhe = (amsData->getActiveExportPower() * (((float) ms) / 3600000.0)) / 1000.0; lastUpdateMillis = amsData->getLastUpdateMillis(); - if(kwh > 0) { - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh\n", kwh); - use += kwh; + if(kwhi > 0) { + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh import\n", kwhi); + use += kwhi; if(eapi != NULL && eapi->getValueForHour(0) != ENTSOE_NO_VALUE) { float price = eapi->getValueForHour(0); - float cost = price * kwh; + float cost = price * kwhi; if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) and %.4f %s\n", cost / 100.0, eapi->getCurrency()); costHour += cost; costDay += cost; } } + if(kwhe > 0) { + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh export\n", kwhe); + produce += kwhe; + } if(config != NULL) { if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) calculating threshold, currently at %d\n", currentThresholdIdx); @@ -151,10 +157,6 @@ double EnergyAccounting::getUseThisHour() { return use; } -double EnergyAccounting::getCostThisHour() { - return costHour; -} - double EnergyAccounting::getUseToday() { float ret = 0.0; time_t now = time(nullptr); @@ -168,14 +170,6 @@ double EnergyAccounting::getUseToday() { return ret + getUseThisHour(); } -double EnergyAccounting::getCostToday() { - return costDay; -} - -double EnergyAccounting::getCostYesterday() { - return data.costYesterday / 10.0; -} - double EnergyAccounting::getUseThisMonth() { time_t now = time(nullptr); if(now < BUILD_EPOCH) return 0; @@ -191,6 +185,51 @@ double EnergyAccounting::getUseThisMonth() { return ret + getUseToday(); } +double EnergyAccounting::getProducedThisHour() { + return produce; +} + +double EnergyAccounting::getProducedToday() { + float ret = 0.0; + time_t now = time(nullptr); + if(now < BUILD_EPOCH) return 0; + tmElements_t local, utc; + breakTime(tz->toLocal(now), local); + for(int i = 0; i < local.Hour; i++) { + breakTime(now - ((local.Hour - i) * 3600), utc); + ret += ds->getHourExport(utc.Hour) / 1000.0; + } + return ret + getProducedThisHour(); +} + +double EnergyAccounting::getProducedThisMonth() { + time_t now = time(nullptr); + if(now < BUILD_EPOCH) return 0; + tmElements_t tm; + if(tz != NULL) + breakTime(tz->toLocal(now), tm); + else + breakTime(now, tm); + float ret = 0; + for(int i = 0; i < tm.Day; i++) { + ret += ds->getDayExport(i) / 1000.0; + } + return ret + getProducedToday(); +} + + +double EnergyAccounting::getCostThisHour() { + return costHour; +} + +double EnergyAccounting::getCostToday() { + return costDay; +} + +double EnergyAccounting::getCostYesterday() { + return data.costYesterday / 10.0; +} + double EnergyAccounting::getCostThisMonth() { return data.costThisMonth + getCostToday(); } diff --git a/src/EnergyAccounting.h b/src/EnergyAccounting.h index 96c82a2d..30a894ed 100644 --- a/src/EnergyAccounting.h +++ b/src/EnergyAccounting.h @@ -42,11 +42,16 @@ public: bool save(); double getUseThisHour(); - double getCostThisHour(); double getUseToday(); + double getUseThisMonth(); + + double getProducedThisHour(); + double getProducedToday(); + double getProducedThisMonth(); + + double getCostThisHour(); double getCostToday(); double getCostYesterday(); - double getUseThisMonth(); double getCostThisMonth(); uint16_t getCostLastMonth(); @@ -66,6 +71,7 @@ private: Timezone *tz = NULL; uint8_t currentHour = 0, currentDay = 0, currentThresholdIdx = 0; double use, costHour, costDay; + double produce; EnergyAccountingData data = { 0, 0, 0, 0, 0, 0 }; void calcDayCost(); diff --git a/src/mqtt/JsonMqttHandler.cpp b/src/mqtt/JsonMqttHandler.cpp index b5d6103a..82847182 100644 --- a/src/mqtt/JsonMqttHandler.cpp +++ b/src/mqtt/JsonMqttHandler.cpp @@ -27,7 +27,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou ea->getUseThisHour(), ea->getUseToday(), ea->getCurrentThreshold(), - ea->getMonthMax() + ea->getMonthMax(), + ea->getProducedThisHour(), + ea->getProducedToday() ); return mqtt->publish(topic, json); } else if(data->getListType() == 2) { @@ -55,7 +57,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou ea->getUseThisHour(), ea->getUseToday(), ea->getCurrentThreshold(), - ea->getMonthMax() + ea->getMonthMax(), + ea->getProducedThisHour(), + ea->getProducedToday() ); return mqtt->publish(topic, json); } else if(data->getListType() == 3) { @@ -88,7 +92,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou ea->getUseThisHour(), ea->getUseToday(), ea->getCurrentThreshold(), - ea->getMonthMax() + ea->getMonthMax(), + ea->getProducedThisHour(), + ea->getProducedToday() ); return mqtt->publish(topic, json); } else if(data->getListType() == 4) { @@ -125,7 +131,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou ea->getUseThisHour(), ea->getUseToday(), ea->getCurrentThreshold(), - ea->getMonthMax() + ea->getMonthMax(), + ea->getProducedThisHour(), + ea->getProducedToday() ); return mqtt->publish(topic, json); } diff --git a/src/mqtt/RawMqttHandler.cpp b/src/mqtt/RawMqttHandler.cpp index 0475f60a..dffdb8a6 100644 --- a/src/mqtt/RawMqttHandler.cpp +++ b/src/mqtt/RawMqttHandler.cpp @@ -76,6 +76,8 @@ bool RawMqttHandler::publish(AmsData* data, AmsData* meterState, EnergyAccountin 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); + mqtt->publish(topic + "/realtime/export/hour", String(ea->getProducedThisHour(), 3)); + mqtt->publish(topic + "/realtime/export/day", String(ea->getProducedToday(), 2)); return true; } diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 1ae62136..13770818 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -747,10 +747,13 @@ void AmsWebServer::dataJson() { ea->getCurrentThreshold(), ea->getUseThisHour(), ea->getCostThisHour(), + ea->getProducedThisHour(), ea->getUseToday(), ea->getCostToday(), + ea->getProducedToday(), ea->getUseThisMonth(), ea->getCostThisMonth(), + ea->getProducedThisMonth(), (uint32_t) time(nullptr) ); diff --git a/web/application.js b/web/application.js index 1c1ee04e..04d603d6 100644 --- a/web/application.js +++ b/web/application.js @@ -790,6 +790,12 @@ var fetch = function() { if(currency) { $('.sp').show(); } + if(om > 0) { + $('.se').removeClass('d-none'); + $('#eache').html(json.ea.h.p.toFixed(2)); + $('#eacde').html(json.ea.d.p.toFixed(1)); + $('#eacme').html(json.ea.m.p.toFixed(0)); + } } if(json.me) { diff --git a/web/data.json b/web/data.json index 6a11c168..0265bb0f 100644 --- a/web/data.json +++ b/web/data.json @@ -38,15 +38,18 @@ "t" : %d, "h" : { "u" : %.2f, - "c" : %.2f + "c" : %.2f, + "p" : %.2f }, "d" : { "u" : %.2f, - "c" : %.2f + "c" : %.2f, + "p" : %.2f }, "m" : { "u" : %.2f, - "c" : %.2f + "c" : %.2f, + "p" : %.2f } }, "c" : %lu diff --git a/web/index.html b/web/index.html index d428ce3b..4f230fde 100644 --- a/web/index.html +++ b/web/index.html @@ -113,7 +113,7 @@
- Real time calculation
+ Real time consumption
@@ -151,6 +151,35 @@
+ + Real time production
+
+
+
+
Hour
+
+ kWh +
+
+
+
+
+
Day
+
+ kWh +
+
+
+
+
+
Month
+
+ kWh +
+
+
+
+
diff --git a/web/json1.json b/web/json1.json index b7dd9495..70f74801 100644 --- a/web/json1.json +++ b/web/json1.json @@ -13,6 +13,8 @@ "h" : %.2f, "d" : %.1f, "t" : %d, - "x" : %.2f + "x" : %.2f, + "he" : %.2f, + "de" : %.1f } } diff --git a/web/json2.json b/web/json2.json index 5a5c0014..b875ac11 100644 --- a/web/json2.json +++ b/web/json2.json @@ -25,6 +25,8 @@ "h" : %.2f, "d" : %.1f, "t" : %d, - "x" : %.2f + "x" : %.2f, + "he" : %.2f, + "de" : %.1f } } diff --git a/web/json3.json b/web/json3.json index 48701fe9..33d022ba 100644 --- a/web/json3.json +++ b/web/json3.json @@ -30,6 +30,8 @@ "h" : %.2f, "d" : %.1f, "t" : %d, - "x" : %.2f + "x" : %.2f, + "he" : %.2f, + "de" : %.1f } } diff --git a/web/json4.json b/web/json4.json index 3e0dc904..701c06f4 100644 --- a/web/json4.json +++ b/web/json4.json @@ -34,6 +34,8 @@ "h" : %.2f, "d" : %.1f, "t" : %d, - "x" : %.2f + "x" : %.2f, + "he" : %.2f, + "de" : %.1f } }