From 16b6d353f4f5470b2693d1add1c73b1d76379a82 Mon Sep 17 00:00:00 2001 From: SteWers <42718143+SteWers@users.noreply.github.com> Date: Fri, 22 Nov 2024 21:01:31 +0100 Subject: [PATCH] [Energy] Fix hardware sensor and restore (#22528) - Fix handling, when using hardware sensor for EnergytTotal (SO72) - Fix restore values, when device is off over midnight and power supply is unstable while powering on at the next day --- tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino | 8 ++++---- tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino b/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino index a26644832..5cb7fbb46 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino @@ -368,14 +368,14 @@ void EnergyUpdateTotal(void) { } } - if ((Energy->total[i] < (Energy->import_active[i] - 0.01f)) && // We subtract a little offset of 10Wh to avoid continuous updates - Settings->flag3.hardware_energy_total) { // SetOption72 - Enable hardware energy total counter as reference (#6561) + if (Settings->flag3.hardware_energy_total && // SetOption72 - Enable hardware energy total counter as reference (#6561) + fabs(Energy->total[i] - Energy->import_active[i]) > 0.01f) { // to avoid continuous updates, check for difference of min 10Wh // The following calculation allows total usage (Energy->import_active[i]) up to +/-2147483.647 kWh RtcSettings.energy_kWhtotal_ph[i] = (int32_t)((Energy->import_active[i] * 1000) - ((Energy->kWhtoday_offset[i] + Energy->kWhtoday[i]) / 100)); Settings->energy_kWhtotal_ph[i] = RtcSettings.energy_kWhtotal_ph[i]; Energy->total[i] = Energy->import_active[i]; Settings->energy_kWhtotal_time = (!Energy->kWhtoday_offset[i]) ? LocalTime() : Midnight(); - // AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Energy Total updated with hardware value")); + // AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: EnergyTotal updated with hardware value")); } } @@ -407,7 +407,7 @@ void Energy200ms(void) { } bool midnight = (LocalTime() == Midnight()); - if (midnight || (RtcTime.day_of_year > Settings->energy_kWhdoy)) { + if ((midnight || RtcTime.day_of_year > Settings->energy_kWhdoy) && TasmotaGlobal.uptime > 10) { Energy->kWhtoday_offset_init = true; Settings->energy_kWhdoy = RtcTime.day_of_year; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino index 6b7423329..9fd4a0c47 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino @@ -624,14 +624,14 @@ void EnergyUpdateTotal(void) { } } - if ((Energy->total[i] < (Energy->import_active[i] - 0.01f)) && // We subtract a little offset of 10Wh to avoid continuous updates - Settings->flag3.hardware_energy_total) { // SetOption72 - Enable hardware energy total counter as reference (#6561) + if (Settings->flag3.hardware_energy_total && // SetOption72 - Enable hardware energy total counter as reference (#6561) + fabs(Energy->total[i] - Energy->import_active[i]) > 0.01f) { // to avoid continuous updates, check for difference of min 10Wh // The following calculation allows total usage (Energy->import_active[i]) up to +/-2147483.647 kWh RtcEnergySettings.energy_total_kWh[i] = Energy->import_active[i] - (Energy->energy_today_offset_kWh[i] + ((float)Energy->kWhtoday[i] / 100000)); Energy->Settings.energy_total_kWh[i] = RtcEnergySettings.energy_total_kWh[i]; Energy->total[i] = Energy->import_active[i]; Energy->Settings.energy_kWhtotal_time = (!Energy->energy_today_offset_kWh[i]) ? LocalTime() : Midnight(); - // AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Energy Total updated with hardware value")); + // AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: EnergyTotal updated with hardware value")); } } @@ -663,7 +663,7 @@ void Energy200ms(void) { } bool midnight = (LocalTime() == Midnight()); - if (midnight || (RtcTime.day_of_year > Energy->Settings.energy_kWhdoy)) { + if ((midnight || RtcTime.day_of_year > Energy->Settings.energy_kWhdoy) && TasmotaGlobal.uptime > 10) { Energy->kWhtoday_offset_init = true; Energy->Settings.energy_kWhdoy = RtcTime.day_of_year;