Skip to content

Commit

Permalink
[Energy] Fix hardware sensor and restore
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
SteWers committed Nov 22, 2024
1 parent bf872de commit 797f216
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand Down Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 797f216

Please sign in to comment.