From 719ceb610ce0a387a60a677ff806e84ee22e1194 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Thu, 24 Aug 2023 07:49:09 +0200 Subject: [PATCH] Fixed realtime export counter --- .../include/EnergyAccounting.h | 3 +- lib/EnergyAccounting/src/EnergyAccounting.cpp | 47 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/EnergyAccounting/include/EnergyAccounting.h b/lib/EnergyAccounting/include/EnergyAccounting.h index bf86e8ce..ff831db2 100644 --- a/lib/EnergyAccounting/include/EnergyAccounting.h +++ b/lib/EnergyAccounting/include/EnergyAccounting.h @@ -103,7 +103,8 @@ public: private: RemoteDebug* debugger = NULL; - unsigned long lastUpdateMillis = 0; + unsigned long lastImportUpdateMillis = 0; + unsigned long lastExportUpdateMillis = 0; bool init = false, initPrice = false; AmsDataStorage *ds = NULL; EntsoeApi *eapi = NULL; diff --git a/lib/EnergyAccounting/src/EnergyAccounting.cpp b/lib/EnergyAccounting/src/EnergyAccounting.cpp index 278e563d..f1bd1bfe 100644 --- a/lib/EnergyAccounting/src/EnergyAccounting.cpp +++ b/lib/EnergyAccounting/src/EnergyAccounting.cpp @@ -142,29 +142,36 @@ bool EnergyAccounting::update(AmsData* amsData) { } } - unsigned long ms = this->lastUpdateMillis > amsData->getLastUpdateMillis() ? 0 : amsData->getLastUpdateMillis() - this->lastUpdateMillis; - float kwhi = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0; - float kwhe = (amsData->getActiveExportPower() * (((float) ms) / 3600000.0)) / 1000.0; - lastUpdateMillis = amsData->getLastUpdateMillis(); - if(kwhi > 0) { - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) Adding %.4f kWh import\n"), kwhi); - use += kwhi; - if(price != ENTSOE_NO_VALUE) { - float cost = price * kwhi; - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) and %.4f %s\n"), cost / 100.0, currency.c_str()); - costHour += cost; - costDay += cost; + if(this->lastImportUpdateMillis < amsData->getLastUpdateMillis()) { + unsigned long ms = amsData->getLastUpdateMillis() - this->lastImportUpdateMillis; + float kwhi = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0; + if(kwhi > 0) { + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) Adding %.4f kWh import\n"), kwhi); + use += kwhi; + if(price != ENTSOE_NO_VALUE) { + float cost = price * kwhi; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) and %.4f %s\n"), cost / 100.0, currency.c_str()); + costHour += cost; + costDay += cost; + } } + lastImportUpdateMillis = amsData->getLastUpdateMillis(); } - if(kwhe > 0) { - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) Adding %.4f kWh export\n"), kwhe); - produce += kwhe; - if(price != ENTSOE_NO_VALUE) { - float income = price * kwhe; - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) and %.4f %s\n"), income / 100.0, currency.c_str()); - incomeHour += income; - incomeDay += income; + + if(amsData->getListType() > 1 && this->lastExportUpdateMillis < amsData->getLastUpdateMillis()) { + unsigned long ms = amsData->getLastUpdateMillis() - this->lastExportUpdateMillis; + float kwhe = (amsData->getActiveExportPower() * (((float) ms) / 3600000.0)) / 1000.0; + if(kwhe > 0) { + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) Adding %.4f kWh export\n"), kwhe); + produce += kwhe; + if(price != ENTSOE_NO_VALUE) { + float income = price * kwhe; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) and %.4f %s\n"), income / 100.0, currency.c_str()); + incomeHour += income; + incomeDay += income; + } } + lastExportUpdateMillis = amsData->getLastUpdateMillis(); } if(config != NULL) {