DarwinsBuddy / WienerNetzeSmartmeter

A home-assistant integration supporting WienerNetze Smartmeters as sensors
143 stars 15 forks source link

Change statistics_sensor to measurement and set it to 0 kWh at the end #123

Closed DarwinsBuddy closed 10 months ago

DarwinsBuddy commented 1 year ago

After reading https://github.com/DarwinsBuddy/WienerNetzeSmartmeter/issues/119 I wondered what the difference in set up is for live vs. statistics sensor.

I remember, that @reox had troubles setting the state of the statistics entity of a smart meter to something not None. This presumably leads to the reported warning.

In order to fix this I tried out setting the statistics sensor to measurement (since it's not quite total_increasing) and set its value to 0 kWh, s.t. it has an entry in the entity registry of home assistant (which was not the case prior to that).

The warning in the energy dashboard is gone.

@reox Can you potentially confirm that I didn't break anything else with this change?

codecov[bot] commented 1 year ago

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (49c0cc2) 29.60% compared to head (45064dc) 29.33%.

Files Patch % Lines
custom_components/wnsm/statistics_sensor.py 0.00% 5 Missing :warning:
custom_components/wnsm/base_sensor.py 0.00% 3 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #123 +/- ## ========================================== - Coverage 29.60% 29.33% -0.27% ========================================== Files 12 12 Lines 652 658 +6 Branches 109 111 +2 ========================================== Hits 193 193 - Misses 449 455 +6 Partials 10 10 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

reox commented 1 year ago

interesting! maybe that works, but I have to test that... The interesting thing is that we also import the state statistics, but maybe they are not overwritten if the state is zero? Eventually I know too little about the HA internals :D

DarwinsBuddy commented 1 year ago

interesting! maybe that works, but I have to test that... The interesting thing is that we also import the state statistics, but maybe they are not overwritten if the state is zero? Eventually I know too little about the HA internals :D

I can't answer that, but if the change does not break anything. (i.e. the statistics sensor keeps importing data successively), the warning would be gone and thus potential future issues :upside_down_face:

reox commented 1 year ago

Nothing to complain about the code, I switched to that branch now and let it run for a few days. But I'll guess it's fine!

reox commented 1 year ago

I get this error now:

Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: homeassistant.helpers.entity Source: custom_components/wnsm/statistics_sensor.py:81 Integration: WienerNetzeSmartmeter (documentation, issues) First occurred: 01:57:11 (6 occurrences) Last logged: 06:57:11

Update for sensor.xx_statistics fails
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 554, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 784, in async_device_update
await coro
File "/config/custom_components/wnsm/statistics_sensor.py", line 81, in async_update
_sum = Decimal(last_inserted_stat[self._id][0]["sum"])
TypeError: conversion from NoneType to Decimal is not supported

Not sure how the sum got None all of a sudden? Maybe the switch of the sensor type caused that? Looks like this fix is not backwards compatible with old measurements - or @DarwinsBuddy did that worked for you and my setup is just a bit weird?

reox commented 1 year ago

@DarwinsBuddy I removed the intgration and deleted the statistics from the database to start from scratch. In that case, it works and the _statistics sensor shows 0kWh. Do you understand what's going on when there is already data and the sensor is switched? Otherwise the only chance would be to remove the statistics and start all over. With the import feature that would not be a great loss though.

DarwinsBuddy commented 1 year ago

I think removing and regenerating the history is definitely an option. though I am not sure how to achieve this after the upgrade. I know of that one service built in to hassio which is easy to call at least: purge entities.

but I'd have to research if we could call it from within our custom component. if not we can directly use the recorder I guess?

still what's left is too check somehow when it's been updated to the next version and do it only then.

reox commented 1 year ago

I got the error again - this time on the freshly imported data. I guess setting the state to anything else than None is a bad idea :(

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 554, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 784, in async_device_update
    await coro
  File "/config/custom_components/wnsm/statistics_sensor.py", line 81, in async_update
    _sum = Decimal(last_inserted_stat[self._id][0]["sum"])
TypeError: conversion from NoneType to Decimal is not supported

My suspicion is, that the statistics module overwrites something when the state is set to zero and thus the last state set by the add statistics is overwritten...