From 04796c4410128cd9b678e0d7532ad25001fb84ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Fri, 7 Jan 2022 16:57:52 +0100 Subject: [PATCH] Add missing `last_rest` in Tibber (#61914) --- homeassistant/components/tibber/sensor.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/homeassistant/components/tibber/sensor.py b/homeassistant/components/tibber/sensor.py index 602a6b9277a..1b6940456b6 100644 --- a/homeassistant/components/tibber/sensor.py +++ b/homeassistant/components/tibber/sensor.py @@ -455,6 +455,24 @@ class TibberSensorRT(TibberSensor, update_coordinator.CoordinatorEntity): state = live_measurement.get(self.entity_description.key) if state is None: return + if self.entity_description.key in ( + "accumulatedConsumption", + "accumulatedProduction", + ): + # Value is reset to 0 at midnight, but not always strictly increasing due to hourly corrections + # If device is offline, last_reset should be updated when it comes back online if the value has decreased + ts_local = dt_util.parse_datetime(live_measurement["timestamp"]) + if ts_local is not None: + if self.last_reset is None or ( + state < 0.5 * self.native_value # type: ignore # native_value is float + and ( + ts_local.hour == 0 + or (ts_local - self.last_reset) > timedelta(hours=24) + ) + ): + self._attr_last_reset = dt_util.as_utc( + ts_local.replace(hour=0, minute=0, second=0, microsecond=0) + ) if self.entity_description.key == "powerFactor": state *= 100.0 self._attr_native_value = state