From e5a02772f0db5e43fa821b41aa8527f911497a08 Mon Sep 17 00:00:00 2001 From: "Povl H. Pedersen" Date: Tue, 25 Aug 2020 09:33:27 +0200 Subject: [PATCH] Update xnrg_01_hlw8012.ino Fix rounding error in kWhtoday_delta calculation. At 3.6kW load, hlw_len could be as low as 5, so rounding could make the value 20% off. Multiply hlw_len by 1000 and the dividend a few lines below as well fixes this. With integer math only, dividends should keep precision as long as possible. --- tasmota/xnrg_01_hlw8012.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasmota/xnrg_01_hlw8012.ino b/tasmota/xnrg_01_hlw8012.ino index 3a0873dd1..ae07a6102 100644 --- a/tasmota/xnrg_01_hlw8012.ino +++ b/tasmota/xnrg_01_hlw8012.ino @@ -205,10 +205,10 @@ void HlwEverySecond(void) unsigned long hlw_len; if (Hlw.energy_period_counter) { - hlw_len = 10000 / Hlw.energy_period_counter; + hlw_len = 10000 * 1000 / Hlw.energy_period_counter; Hlw.energy_period_counter = 0; if (hlw_len) { - Energy.kWhtoday_delta += ((Hlw.power_ratio * Settings.energy_power_calibration) / hlw_len) / 36; + Energy.kWhtoday_delta += ((Hlw.power_ratio * Settings.energy_power_calibration) * 1000 / hlw_len) / 36; EnergyUpdateToday(); } }