History stats count only the last state duration

Hi there,
I’m using a generic thermostat to control my heating.
I’d like to show in Lovelace in text form (Mushroom title card) how long the switch was active (on) for.
Example: “Heating ran for 3h23min”.

The heating could run more than once a day, so I’d want to know the time of only the last time, not a sum of a whole day nor a timeframe.

I think the history stats might be the correct approach but I can’t figure out how to display the information in a value template.

You can try using a similar method to whats described in this thread, but based on the last changed property. Keep in mind that using last_changed won’t be 100% accurate because it’s not stable across restarts.

  - platform: history_stats
    name: "Heater Last Runtime"
    entity_id: climate.your_heater
    state: 'heat'
    type: time
    start: >
      {{ states.light.behind_tv.last_changed - timedelta(minutes=1) }}
    end: "{{ now() }}"

Thanks for the suggestion, I’ll give it a try.
Is there another approach you would recommend besides the history stats?
How to “survive” incase of a restart?