From e45dbc66e0432bba1497c98a1e2e9906aaa2742c Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 30 Aug 2021 18:03:24 +0200 Subject: [PATCH] Fix Shelly Dimmer 2 Energy usage Fix Shelly Dimmer 2 Energy usage (#12815) --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + tasmota/xdrv_45_shelly_dimmer.ino | 44 ++++++++++--------------------- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b41715cea..9d0feb195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ All notable changes to this project will be documented in this file. - Wiegand support for keypad zero key in single key mode using ``SetOption124 1`` (#12960) - Hass and Tasmota discovery prefix topic notifications (#12972) - Unable to disable MusicSync mode on Sonoff L1 Lite regression from 9.3.0 (#12930) +- Shelly Dimmer 2 Energy usage (#12815) ## [9.5.0.6] 20210820 ### Added diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 19bace10e..d7c6c6b6e 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -171,6 +171,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo - Discovery fails when using ``%hostname%`` in a topic [#12710](https://github.com/arendst/Tasmota/issues/12710) - ESP32 buzzer in PWM mode exception (#12717)[#12717](https://github.com/arendst/Tasmota/issues/12717) - Neopool communication error [#12813](https://github.com/arendst/Tasmota/issues/12813) +- Shelly Dimmer 2 Energy usage [#12815](https://github.com/arendst/Tasmota/issues/12815) - WDT reset on shutters with stepper motors during deceleration [#12849](https://github.com/arendst/Tasmota/issues/12849) - Negative power values for ADE7953 based devices like Shelly EM [#12874](https://github.com/arendst/Tasmota/issues/12874) - Unable to disable MusicSync mode on Sonoff L1 Lite regression from 9.3.0 [#12930](https://github.com/arendst/Tasmota/issues/12930) diff --git a/tasmota/xdrv_45_shelly_dimmer.ino b/tasmota/xdrv_45_shelly_dimmer.ino index aa8534cf8..b96b83d4b 100644 --- a/tasmota/xdrv_45_shelly_dimmer.ino +++ b/tasmota/xdrv_45_shelly_dimmer.ino @@ -85,22 +85,22 @@ typedef struct struct SHD { + SHD_DIMMER dimmer; uint8_t *buffer = nullptr; // Serial receive buffer int byte_counter = 0; // Index in serial receive buffer - uint16_t req_brightness = 0; - bool req_on = false; - SHD_DIMMER dimmer; +#ifdef USE_ENERGY_SENSOR + uint32_t last_power_check = 0; // Time when last power was checked +#endif // USE_ENERGY_SENSOR uint32_t start_time = 0; - uint8_t counter = 1; // Packet counter + uint16_t req_brightness = 0; uint16_t req_fade_rate = 0; uint16_t leading_edge = 2; // Leading edge = 2 Trailing edge = 1 uint16_t warmup_brightness = 100; // 10% uint16_t warmup_time = 20; // 20ms -#ifdef USE_ENERGY_SENSOR - uint32_t last_power_check = 0; // Time when last power was checked -#endif // USE_ENERGY_SENSOR - bool present = false; + uint8_t counter = 1; // Packet counter uint8_t hw_version = 0; // Dimmer 1 = 1 Dimmer 2 = 2 + bool present = false; + bool req_on = false; } Shd; /*********************************************************************************************\ @@ -534,32 +534,16 @@ bool ShdPacketProcess(void) Energy.active_power[0] = wattage; Energy.voltage[0] = voltage; Energy.current[0] = current; -/* - // This is duplicated code as it is already services by the Energy driver (xdrv_03_energy.ino) - Energy.apparent_power[0] = voltage * current; - - if ((voltage * current) > wattage) - Energy.reactive_power[0] = sqrtf((voltage * current) * (voltage * current) - wattage * wattage); - else - Energy.reactive_power[0] = 0; - - if (wattage > (voltage * current)) - Energy.power_factor[0] = 1; - else if ((voltage * current) == 0) - Energy.power_factor[0] = 0; - else - Energy.power_factor[0] = wattage / (voltage * current); -*/ - if (Shd.last_power_check > 10 && Energy.active_power[0] > 0) - { - float kWhused = (float)Energy.active_power[0] * (Rtc.utc_time - Shd.last_power_check) / 36; + if (Shd.last_power_check > 10 && Energy.active_power[0] > 0) { + uint32_t time_passed = abs(TimePassedSince(Shd.last_power_check)); // Time passed in milliseconds + uint32_t deca_microWh = (uint32_t)(Energy.active_power[0] * time_passed) / 36; #ifdef SHELLY_DIMMER_DEBUG - AddLog(LOG_LEVEL_DEBUG, PSTR(SHD_LOGNAME "Adding %i mWh to todays usage from %lu to %lu"), (int)(kWhused * 10), Shd.last_power_check, Rtc.utc_time); + AddLog(LOG_LEVEL_DEBUG, PSTR(SHD_LOGNAME "%4_f W is %u dmWh during %u ms"), &Energy.active_power[0], deca_microWh, time_passed); #endif // SHELLY_DIMMER_DEBUG - Energy.kWhtoday_delta += kWhused; + Energy.kWhtoday_delta += deca_microWh; EnergyUpdateToday(); } - Shd.last_power_check = Rtc.utc_time; + Shd.last_power_check = millis(); #endif // USE_ENERGY_SENSOR #ifdef SHELLY_DIMMER_DEBUG