Operating hours counter - prevention of decreasing values

Hey,

I want to realize an operation hours counter. The counter itself seems to be working by the “on”-state.

sensor:
  - platform: history_stats
    name: operational hours light 01
    entity_id: light.down_01
    unique_id: cfa3c4e1-a3a5-41ac-xxxx-da4xxxxe1ded
    state: "on"
    type: time
    start: "{{ 0 }}"
    end: "{{ now() }}"

But:

The value is decreasing sometimes. How to prevent this? I think this relates to recorder. Is there any option to keep the values or another simple way to create counter for lots of devices?

Make a template sensor that is 1 if the light is on, and 0 if the light is off.

Pass that into a riemann sum/integral helper, set to hours. That will give you a lifetime count of the amount of hours the light is on, and won’t use the recorder at all.

History stats is limited to the amount of data in your recorder, and can only really tell you how long it was on for the last ~10 days or whatever you set it to.