History_stats gets unknown after host restart

I have configured history_stats sensor counting number of MQTT movement detection based on other sensor:

-   platform: mqtt
    name: "Cat toilet"
    state_topic: "e-hallway-cat-toilet/detection"
    expire_after: 86400
-   platform: history_stats
    name: "Cat toilet history"
    entity_id: sensor.cat_toilet
    state: "1"
    type: count
    start: '{{ now().replace(hour=8, minute=1, second=0) }}'
      hours: 24

It is working well - counting till 08:01 each 24h and then reset to zero.

The problem is that after host restart (called by hassio service actually at 04:04 AM), the status gets “unknown” till stats are discarded at 08:01 AM and then it is working from zero again correctly.

I’ve tried to reload stats by calling service “history_stats.reload” as an automation after Home Assistant boot, but it doesn’t change anything.

I have turned off “auto_purge” of my recorder and trigger it by an automation after sensor reset:

    purge_keep_days: 3
    auto_purge: false

Anyway, there should be data 3 days old which means it should not have any effect to my issue.

Is there any way how to restore the “history_stats” sensor value after host restart?

Thnaks a lot!

1 Like