From 65d1f3aacedb9dfe9b9cb32fde40de762beda4f5 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 17 Jan 2020 10:12:57 +0100 Subject: [PATCH] Fix PowerDelta calculation Fix PowerDelta calculation not detecting zero power (#7515) --- tasmota/xdrv_03_energy.ino | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tasmota/xdrv_03_energy.ino b/tasmota/xdrv_03_energy.ino index 900bb2767..166fd7aa8 100644 --- a/tasmota/xdrv_03_energy.ino +++ b/tasmota/xdrv_03_energy.ino @@ -289,14 +289,19 @@ void EnergyMarginCheck(void) if (Settings.energy_power_delta) { uint16_t delta = abs(Energy.power_history[0] - energy_power_u); - uint16_t min_power = (Energy.power_history[0] > energy_power_u) ? energy_power_u : Energy.power_history[0]; - - DEBUG_DRIVER_LOG(PSTR("NRG: Delta %d, Power %d"), delta, min_power); - - if ((delta > 0) && (min_power > 0)) { // Fix divide by 0 exception (#6741) - if (((Settings.energy_power_delta < 101) && (((delta * 100) / min_power) > Settings.energy_power_delta)) || // 1..100 = Percentage - ((Settings.energy_power_delta > 100) && (delta > (Settings.energy_power_delta -100)))) { // 101..32000 = Absolute - Energy.power_delta = true; + if (delta > 0) { + if (Settings.energy_power_delta < 101) { // 1..100 = Percentage + uint16_t min_power = (Energy.power_history[0] > energy_power_u) ? energy_power_u : Energy.power_history[0]; + if (0 == min_power) { min_power++; } // Fix divide by 0 exception (#6741) + if (((delta * 100) / min_power) > Settings.energy_power_delta) { + Energy.power_delta = true; + } + } else { // 101..32000 = Absolute + if (delta > (Settings.energy_power_delta -100)) { + Energy.power_delta = true; + } + } + if (Energy.power_delta) { Energy.power_history[1] = Energy.active_power[0]; // We only want one report so reset history Energy.power_history[2] = Energy.active_power[0]; }