Home Assistant 2023.11.1 running via a docker container. Standard sqlite database.
I have created some history_stats sensors, one of which ended up with duff data due to a typo. So I thought I would just delete the sensor definition, delete the entity and then remove the associated statistic. However, as soon as the sensor is recreated it seems to have remembered the previous value rather than starting from zero, can anyone explain why?
Commented out history_stats sensor from the yaml configuration
Rebooted Home Assistant
Go to settings / entities - sensor showing no longer being provided. Deleted sensor.
Go to developer / statistics - no state for the sensor. Fix issue and remove.
Rebooted Home Assistant.
Download the database and opened
From the states_meta table I find the metadata_id for the sensor - 1099
From the states table I find the states associated with 1099 - there is one entry listed, the state is NULL. Attribute id is 462421 and there’s one entry in the state_attributes table.
From the statistics_meta table I find no entry for sensor.
So, in theory the sensor has been deleted, although I’m surprised to see any entry in the states table? However, if I then create the sensor again its starting value is exactly the same value as it was before the delete rather than starting at zero. Can anyone explain what’s going on and why the sensor hasn’t started from zero? There must be a hidden value somewhere that I’m missing?
I’m loathed to start directly altering the database but as I can’t find any data pertaining to the issue I’m not sure what I would delete?
Can someone please shed some light on what’s going wrong.
The history stats sensor obtains its state from the history of the source sensor stored in the database.
It is not clear from your description which entity’s history you deleted, but from the result you are getting I’m guessing it was not the history of binary_sensor.oil_boiler_running
I was deleting the monthly_boiler_usage data but I can see now that recreating it will immediately scan through the historic data for the oil_boiler_running, so I’ve been looking in the wrong place…
Thank you, I couldn’t see the wood for the trees! I will take a look at the binary_sensor.oil_boiler_running as there must be some duff data there.
Just to follow this up I think there’s an issue with history_stats after a HA restart. I cleared out the oil_boiler_running data and then recreated the monthly_boiler_usage template sensor and the value was then correct. However, after a HA restart the value has gone wayward again.
No I gave up using the history stats sensor, it has all sorts of weirdness especially during restarts, there are several bugs reports on GitHub including mine…
The data was being sourced from an ESPhome sensor so I coded the history stats functionality there instead and it has been 100% reliable.