"Time on" counter of an entity/device state

Hi Group,
I’m trying to figure out for some days, but with no success. Anyhow I want to calculate and display on my dashboard some simple counters. Like - how long was a living room light on, how long were front door open, etc. But I somehow don’t manage. I tried using sensor in my config template as below, but the counter is not counting. I would appreciate if someone could point me to the solution!
Many thanks!

sensor:
  - platform: history_stats
    name: Living Room Main Light On Current Month
    entity_id: light.yeelight_color4_0x13f24667
    state: "On"
    type: time
    start: '{{ now().replace(month=1, day=1, hour=0, minute=0, second=0) }}'
    end: "{{ now() }}"

Snimka zaslona 2022-10-05 132755

Change

state: "On"

To:

state: "on"

Also this start time is not correct for the current month:

start: '{{ now().replace(month=1, day=1, hour=0, minute=0, second=0) }}'

That is since January (month 1). You want this for the current month:

start: '{{ now().replace(day=1, hour=0, minute=0, second=0) }}'
1 Like

@tom_l This works! Thank you so much! If I knew I would have asked few days back!

1 Like