After rebooting Home Assistant, history gets corrupted

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?

recorder:
  db_url: !secret mariadb_url
  commit_interval: 15
  include:
    entities:
    - sun.sun
    - sensor.sun_elevation
    - and beyond...

history:
  use_include_order: true
  include:
    entities:
    - binary_sensor.rpi_power_status
    - and beyond...

1 Like

Err, this shows how much I go to check this…

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…

Is there any hope that this will ever be fixed?

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.

1 Like

Same issue here with HA 2022.10.5 + PostgreSQL 12.7

Same here,
version: 2022.11.1, MariaDB.

After the new update, the simple history stats gets corrupted.

- platform: history_stats
  name: Fridge Working Hours
  entity_id: input_boolean.fridge_running
  state: "on"
  type: time
  start: "{{ 0 }}"
  end: "{{ now() }}"