From 958ddcad919c2589948480a753a0c15f709bf633 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 25 Oct 2019 18:16:09 +0200 Subject: [PATCH] Fix exception 0 on power delta Fix exception 0 on power delta (#6741) --- sonoff/xdrv_03_energy.ino | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index 30b0237e2..aa580421d 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -292,11 +292,13 @@ void EnergyMarginCheck(void) DEBUG_DRIVER_LOG(PSTR("NRG: Delta %d, Power %d"), delta, min_power); - 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; - Energy.power_history[1] = Energy.active_power[0]; // We only want one report so reset history - Energy.power_history[2] = Energy.active_power[0]; + if (delta) { // 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; + Energy.power_history[1] = Energy.active_power[0]; // We only want one report so reset history + Energy.power_history[2] = Energy.active_power[0]; + } } } Energy.power_history[0] = Energy.power_history[1]; // Shift in history every second allowing power changes to settle for up to three seconds