Log how long something is on

Hi,

I want to log how long a switch is turned on and display that in the frontend.
How can I do that?

Regards

History Stats Integration

1 Like

I have added a test sensor but its not working:

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: history_stats
    name: Test History
    entity_id: switch.stecker_1
    state: 'on'
    type: time
    start: '{{ 0 }}'
    end: '{{ now() }}'
  - platform: template
    sensors:
      test_laufzeit:
        friendly_name: 'Test Laufzeit'
        value_template: "{{ states('sensor.test_history') }}"

As soon as I activate the switch it creates this entry:
Test