After rebooting or updating Home Assistant, history gets corrupted.
It started with core_2022.4.0.
Then I used sqlite.
I reinstalled HassOS, restored the configuration from a backup, but the problem persisted. Then I installed MariaDB, but the history still gets corrupted. Is there a way to fix the situation?
I have same behaviour ! (using mariaDB since…ever !)
I think this is related to new optimizations done on DB… As the state doesn’t change HA doesn’t store the state, instead, it makes a reference back to the previous state that was the same. And if you purge your DB the reference points to nowhere…
Yup, the history/period API endpoint is broken since the DB table layout changes in 2022.4. Quoting myself from the Github issue:
Some more info on this issue. Since the db updates in 2022.4, calling the history/period endpoint in the REST API has changed behavior compared to 2022.3.x and previous. It will not return the initial state anymore for state changes that happened before the requested time period, which leads to the empty spaces in the bar shown above, if HA was restarted after that initial state change happened.
So in other words, while HA runs the problem will eventually correct itself for new data recorded after the last HA restart. The moment HA is restarted, the whole problem reappears, even on data that seemed ‘fixed’ previously. It looks like initial state is pulled from an in-memory cache rather than from the DB, and that cache is cleared on restart.
This issue affects all code using the history/period endpoint - the builtin history panel as well as custom cards.
It doesn’t even need a purge. Simply doing a request for a time period, say the last hour, will not return the initial state that happened, say, 2 hours ago, if HA was restarted inbetween. When specifically requesting the last 2 hours, the state change will be returned.