From 2740c48e22739c492227d4b641f882e5312a24be Mon Sep 17 00:00:00 2001 From: Barbudor Date: Sun, 16 May 2021 16:18:00 +0200 Subject: [PATCH 1/2] extract power value as 3 bytes from Tuya frame --- tasmota/xdrv_16_tuyamcu.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tasmota/xdrv_16_tuyamcu.ino b/tasmota/xdrv_16_tuyamcu.ino index f34c56af2..91abb8d76 100644 --- a/tasmota/xdrv_16_tuyamcu.ino +++ b/tasmota/xdrv_16_tuyamcu.ino @@ -729,7 +729,7 @@ void TuyaProcessStatePacket(void) { if (RtcTime.valid) { if (Tuya.lastPowerCheckTime != 0 && Energy.active_power[0] > 0) { - Energy.kWhtoday += (float)Energy.active_power[0] * (Rtc.utc_time - Tuya.lastPowerCheckTime) / 36; + Energy.kWhtoday += Energy.active_power[0] * (float)(Rtc.utc_time - Tuya.lastPowerCheckTime) / 36.0; EnergyUpdateToday(); } Tuya.lastPowerCheckTime = Rtc.utc_time; @@ -856,12 +856,13 @@ void TuyaProcessStatePacket(void) { Energy.current[0] = (float)packetValue / 1000; AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Current=%d"), Tuya.buffer[dpidStart], packetValue); } else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_POWER) { + uint32_t packetValue = Tuya.buffer[dpidStart + 5] << 16 |Tuya.buffer[dpidStart + 6] << 8 | Tuya.buffer[dpidStart + 7]; Energy.active_power[0] = (float)packetValue / 10; AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Active_Power=%d"), Tuya.buffer[dpidStart], packetValue); if (RtcTime.valid) { if (Tuya.lastPowerCheckTime != 0 && Energy.active_power[0] > 0) { - Energy.kWhtoday += (float)Energy.active_power[0] * (Rtc.utc_time - Tuya.lastPowerCheckTime) / 36; + Energy.kWhtoday += Energy.active_power[0] * (float)(Rtc.utc_time - Tuya.lastPowerCheckTime) / 36.0; EnergyUpdateToday(); } Tuya.lastPowerCheckTime = Rtc.utc_time; From fa0565b40fbed1b341bdb2f6f8ec54ba38c2db8e Mon Sep 17 00:00:00 2001 From: Barbudor Date: Sun, 16 May 2021 18:09:53 +0200 Subject: [PATCH 2/2] improve reactive_power calculation range --- tasmota/xdrv_03_energy.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tasmota/xdrv_03_energy.ino b/tasmota/xdrv_03_energy.ino index b9697dfca..7c675e19e 100644 --- a/tasmota/xdrv_03_energy.ino +++ b/tasmota/xdrv_03_energy.ino @@ -985,7 +985,12 @@ void EnergyShow(bool json) if ((Energy.current[i] > 0.005) && ((difference > 15) || (difference > (uint32_t)(apparent_power * 100 / 1000)))) { // calculating reactive power only if current is greater than 0.005A and // difference between active and apparent power is greater than 1.5W or 1% - reactive_power = (float)(RoundSqrtInt((uint32_t)(apparent_power * apparent_power * 100) - (uint32_t)(Energy.active_power[i] * Energy.active_power[i] * 100))) / 10; + //reactive_power = (float)(RoundSqrtInt((uint64_t)(apparent_power * apparent_power * 100) - (uint64_t)(Energy.active_power[i] * Energy.active_power[i] * 100))) / 10; + float power_diff = apparent_power * apparent_power - Energy.active_power[i] * Energy.active_power[i]; + if (power_diff < 10737418) // 2^30 / 100 (RoundSqrtInt is limited to 2^30-1) + reactive_power = (float)(RoundSqrtInt((uint32_t)(power_diff * 100.0))) / 10.0; + else + reactive_power = (float)(SqrtInt((uint32_t)(power_diff))); } }