diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 4d043384..b5acd238 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -930,11 +930,6 @@ bool readHanPort() { meterState.apply(data); - if(ea.update(&data)) { - debugI("Saving energy accounting"); - ea.save(); - } - bool saveData = false; if(!ds.isHappy() && now > BUILD_EPOCH) { debugD("Its time to update data storage"); @@ -952,6 +947,11 @@ bool readHanPort() { ds.save(); } } + + if(ea.update(&data)) { + debugI("Saving energy accounting"); + ea.save(); + } } delay(1); return true; diff --git a/src/EnergyAccounting.cpp b/src/EnergyAccounting.cpp index 621763ea..720de4c6 100644 --- a/src/EnergyAccounting.cpp +++ b/src/EnergyAccounting.cpp @@ -52,7 +52,7 @@ bool EnergyAccounting::update(AmsData* amsData) { calcDayCost(); } - if(amsData->getListType() >= 3 && local.Hour != currentHour) { + if(local.Hour != currentHour && (amsData->getListType() >= 3 || local.Minute == 1)) { if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EnergyAccounting) New local hour %d\n", local.Hour); if(calcDayUse()) ret = true; @@ -102,14 +102,9 @@ bool EnergyAccounting::update(AmsData* amsData) { if(config != NULL) { if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) calculating threshold, currently at %d\n", currentThresholdIdx); while(getMonthMax() > config->thresholds[currentThresholdIdx] && currentThresholdIdx < 10) currentThresholdIdx++; - while(use > config->thresholds[currentThresholdIdx] && currentThresholdIdx < 10) currentThresholdIdx++; if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) new threshold %d\n", currentThresholdIdx); } - if(use > data.maxHour) { - data.maxHour = use; // Not really a good idea to use calculated value, but when you pass midnight and have the highest use at hour 23, it will not be included through 'calcDayUse' - } - return ret; } @@ -207,6 +202,10 @@ uint8_t EnergyAccounting::getCurrentThreshold() { return config->thresholds[currentThresholdIdx]; } +uint8_t EnergyAccounting::getCurrentHour() { + return currentHour; +} + float EnergyAccounting::getMonthMax() { return data.maxHour / 100.0; } diff --git a/src/EnergyAccounting.h b/src/EnergyAccounting.h index f873df1b..333db494 100644 --- a/src/EnergyAccounting.h +++ b/src/EnergyAccounting.h @@ -35,6 +35,7 @@ public: float getMonthMax(); uint8_t getCurrentThreshold(); + uint8_t getCurrentHour(); EnergyAccountingData getData(); void setData(EnergyAccountingData&);