From 627da2b2f9fd33c925b5e82c1e502ea4e348a629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gir=C3=B3n?= Date: Sat, 26 Oct 2024 17:35:01 +0200 Subject: [PATCH 1/2] fix: negative measurements when loading recent data * Retrieve the last stored value of `accumulatedConsumption` in the `_async_import_statistics` method. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/duhow/hass-aigues-barcelona?shareId=XXXX-XXXX-XXXX-XXXX). --- custom_components/aigues_barcelona/sensor.py | 22 ++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/custom_components/aigues_barcelona/sensor.py b/custom_components/aigues_barcelona/sensor.py index e765a2c..49c289c 100644 --- a/custom_components/aigues_barcelona/sensor.py +++ b/custom_components/aigues_barcelona/sensor.py @@ -205,16 +205,29 @@ async def _async_import_statistics(self, consumptions) -> None: consumptions, key=lambda x: datetime.fromisoformat(x["datetime"]) ) - # TODO: Hay que cargar datos historicos para actualizar sum_total desde el primer registro. - # Conforme tengamos más datos para representar, aparecerá el fallo para depurar. + # Retrieve the last stored value of accumulatedConsumption + last_stored_value = None + all_ids = await get_db_instance(self.hass).async_add_executor_job( + list_statistic_ids, self.hass + ) + for stat_id in all_ids: + if stat_id["statistic_id"] == self.internal_sensor_id: + if stat_id["sum"] and (last_stored_value is None or stat_id["sum"] > last_stored_value): + last_stored_value = stat_id["sum"] stats = list() - sum_total = 0.0 + sum_total = last_stored_value or 0.0 for metric in consumptions: start_ts = datetime.fromisoformat(metric["datetime"]) start_ts = start_ts.replace(minute=0, second=0, microsecond=0) # required + # Calculate deltaConsumption + deltaConsumption = metric["accumulatedConsumption"] - last_stored_value + # Ensure deltaConsumption is positive before adding to sum_total + if deltaConsumption < 0: + _LOGGER.warn(f"Negative deltaConsumption detected: {deltaConsumption}") + deltaConsumption = 0 # round: fixes decimal with 20 digits precision - sum_total = round(sum_total + metric["deltaConsumption"], 4) + sum_total = round(sum_total + deltaConsumption, 4) stats.append( { "start": start_ts, @@ -224,6 +237,7 @@ async def _async_import_statistics(self, consumptions) -> None: # "last_reset": start_ts, } ) + last_stored_value = metric["accumulatedConsumption"] metadata = { "has_mean": False, "has_sum": True, From f9e6e06d718c16f2d9ae75ad8e68d77053354d73 Mon Sep 17 00:00:00 2001 From: duhow Date: Sat, 26 Oct 2024 19:28:20 +0200 Subject: [PATCH 2/2] fix code --- custom_components/aigues_barcelona/sensor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/custom_components/aigues_barcelona/sensor.py b/custom_components/aigues_barcelona/sensor.py index 49c289c..cfd5906 100644 --- a/custom_components/aigues_barcelona/sensor.py +++ b/custom_components/aigues_barcelona/sensor.py @@ -206,17 +206,19 @@ async def _async_import_statistics(self, consumptions) -> None: ) # Retrieve the last stored value of accumulatedConsumption - last_stored_value = None + last_stored_value = 0.0 all_ids = await get_db_instance(self.hass).async_add_executor_job( list_statistic_ids, self.hass ) for stat_id in all_ids: if stat_id["statistic_id"] == self.internal_sensor_id: - if stat_id["sum"] and (last_stored_value is None or stat_id["sum"] > last_stored_value): + if stat_id.get("sum") and ( + last_stored_value is None or stat_id["sum"] > last_stored_value + ): last_stored_value = stat_id["sum"] stats = list() - sum_total = last_stored_value or 0.0 + sum_total = last_stored_value for metric in consumptions: start_ts = datetime.fromisoformat(metric["datetime"]) start_ts = start_ts.replace(minute=0, second=0, microsecond=0) # required