Calculating how long a device has been on

I’ve got an idea for a few entities where I want my TTS notifiers to tell me how long a device has been switched on for. I just need some help with the start: time for the sensor as the configuration I have below is for a whole day and I want it to reset every time the entity is turned off or changes state.

sensor:
  - platform: history_stats
    name: Name of Entity
    entity_id: entity.id
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

Is that possible?

Found a solution!

2 Likes