Help - count the number of times an entity change to a certain value (like ON or OFF)

I have an entity that allows me to turn a light on and off. I want to count the number of times the lamp is turned ON. how can i get this done the easiest way?
I would prefer to see the number of times per day / week and month.

Thanks!

Hello,

You can use the History Stats sensor for that purpose.
The exemple needs only to adjust to get the count instead of time, like so:

sensor:
  - platform: history_stats
    name: Lamp ON today
    entity_id: light.my_lamp
    state: "on"
    type: count
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

EDIT: You can use the given exemples in the link to adjust for time periods (current month, current week, …)

4 Likes

That would count the time when it is on, not the number of switches from off>on which I believe is what the OP asks for.
My 2 cts…use an automation that triggers on off>on and update a helper (input_number) via service: input_number.increment

It is counting one “on” when it comes from “off”.
I’ll do it in my config to check if it is working the way it should.

Then I might be mistaken but I have a history stats setup for when my son’s PC is ‘on’ and it counts time…not the switch off>on.
edit:

  - platform: history_stats
    name: PC Game on
    entity_id: binary_sensor.desktop_gaming_power_status
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

image
That is after turning the switch on/off 4 times, as expected :slight_smile:

Ah…I see the difference now …TYPE COUNT
Thanks :slight_smile:

great, exactly what I was looking for. thank you both!! @vingerha @Olivier1974

Can you mark my post as the solution, so it will close the topic. Thx