diff --git a/src/AmsConfiguration.cpp b/src/AmsConfiguration.cpp index 1b6d3f9e..3f391e99 100644 --- a/src/AmsConfiguration.cpp +++ b/src/AmsConfiguration.cpp @@ -517,6 +517,7 @@ bool AmsConfiguration::getEnergyAccountingConfig(EnergyAccountingConfig& config) } bool AmsConfiguration::setEnergyAccountingConfig(EnergyAccountingConfig& config) { + if(config.hours > 5) config.hours = 5; EnergyAccountingConfig existing; if(getEnergyAccountingConfig(existing)) { for(int i = 0; i < 9; i++) { diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 23e7b52b..abb85c18 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -1051,7 +1051,7 @@ void WiFi_connect() { } #endif WiFi.mode(WIFI_STA); - WiFi.setSleep(WIFI_PS_MIN_MODEM); + WiFi.setSleep(WIFI_PS_MAX_MODEM); #if defined(ESP32) if(wifi.power >= 195) WiFi.setTxPower(WIFI_POWER_19_5dBm); diff --git a/src/EnergyAccounting.cpp b/src/EnergyAccounting.cpp index 60deaa43..d31441fb 100644 --- a/src/EnergyAccounting.cpp +++ b/src/EnergyAccounting.cpp @@ -237,11 +237,12 @@ float EnergyAccounting::getMonthMax() { uint32_t maxHour = 0.0; bool included[5] = { false, false, false, false, false }; - while(count < config->hours) { + while(count < config->hours && count <= 5) { uint8_t maxIdx = 0; uint16_t maxVal = 0; for(uint8_t i = 0; i < 5; i++) { if(included[i]) continue; + if(data.peaks[i].day == 0) continue; if(data.peaks[i].value > maxVal) { maxVal = data.peaks[i].value; maxIdx = i; @@ -253,9 +254,7 @@ float EnergyAccounting::getMonthMax() { for(uint8_t i = 0; i < 5; i++) { if(!included[i]) continue; - if(data.peaks[i].day > 0) { - maxHour += data.peaks[i].value; - } + maxHour += data.peaks[i].value; } return maxHour > 0 ? maxHour / count / 100.0 : 0.0; } @@ -266,7 +265,7 @@ float EnergyAccounting::getPeak(uint8_t num) { uint8_t count = 0; bool included[5] = { false, false, false, false, false }; - while(count < config->hours) { + while(count < config->hours && count <= 5) { uint8_t maxIdx = 0; uint16_t maxVal = 0; for(uint8_t i = 0; i < 5; i++) { @@ -341,7 +340,7 @@ bool EnergyAccounting::load() { this->data.peaks[b].day = b; memcpy(&this->data.peaks[b].value, buf+i, 2); b++; - if(b >= config->hours) break; + if(b >= config->hours || b >= 5) break; } ret = true; } else if(buf[0] == 1) { diff --git a/src/mqtt/HomeAssistantMqttHandler.cpp b/src/mqtt/HomeAssistantMqttHandler.cpp index 6794946b..18057c2b 100644 --- a/src/mqtt/HomeAssistantMqttHandler.cpp +++ b/src/mqtt/HomeAssistantMqttHandler.cpp @@ -64,9 +64,11 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En } String peaks = ""; - for(uint8_t i = 1; i <= ea->getConfig()->hours; i++) { + uint8_t peakCount = ea->getConfig()->hours; + if(peakCount > 5) peakCount = 5; + for(uint8_t i = 1; i <= peakCount; i++) { if(!peaks.isEmpty()) peaks += ","; - peaks += String(ea->getPeak(i)); + peaks += String(ea->getPeak(i), 2); } snprintf_P(json, BufferSize, REALTIME_JSON, ea->getMonthMax(), @@ -246,6 +248,8 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, Energ #endif String haUrl = "http://" + haUID + ".local/"; // Could this be necessary? haUID.replace("-", "_"); + uint8_t peakCount = ea->getConfig()->hours; + if(peakCount > 5) peakCount = 5; uint8_t peaks = 0; for(int i=0;igetCurrency()); } if(strncmp(sensor.path, "peaks[", 6) == 0) { - if(peaks >= ea->getConfig()->hours) continue; + if(peaks >= peakCount) continue; peaks++; } snprintf_P(json, BufferSize, HADISCOVER_JSON,