How to know how long a binary_sensor has been ON?

I would like to be able to track how long a given binary_sensor has been ON during the day. I was hopeful that I could make an automation that caught the change from ON to OFF, then I could add the difference between the current timestamp and the binary_sensor’s last_updated or last_changed timestamps to a hidden input_number. However, the entity’s timestamps change (and rightly so) by the time the automation runs.

At the moment, I’m firing a rule every 5 minutes that adds 5 minutes to a hidden input_number, under the condition that the given binary_sensor is ON. This is probably close enough for my solution, but it feels awkward and imprecise :slight_smile: I don’t like the idea of a rule that runs every 5 minutes when I feel like I could catch the state transitions instead.

Am I missing something obvious, or am I working with an edge case here?

Hi @Human, you can use the History Statistics Sensor for this.

1 Like

I have no idea how I missed that, but that looks ideal! Thank you!