How long switch has been on for?

I want to have a sensor for displaying how long my coffee machine has been on for. I have used the following code with the history_stats field:

  - platform: history_stats
    name: Coffee On
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'  

But this just totals up the amount its been on for the entire day. My coffee machines takes a minimum of 20 minutes to warm up, so just want to query how long its been on for at any time. If it’s currently off then the sensor should say ‘off’.

How would I do this?

Try the following Template Sensor. You will need to define sensor.time using the Time & Date integration.

  - platform: template
          - sensor.time
        value_template: >
          {% set t = 0 if states('') == 'off' else now().timestamp() - %}
          {{ t | timestamp_custom('%H:%M', false) }}

The sensor’s template will be evaluated whenever sensor.time changes state (every minute) or changes state (on or off). It simply subtracts the switch’s last_changed from the current time (but only if the switch is currently on, otherwise it simply reports 0). The result is a time value in seconds which is converted so it appears in HH:MM format.

Corrected typo in template’s last line.

Thank you for this. I have just turned the switch on but get this as the sensor result

01:03, false

Not sure what this means!

Oops! The final line of the template has a typo. I’ll correct the post to prevent misleading other users.

Change this:

          {{ t | timestamp_custom('%H:%M, false') }}

to this:

          {{ t | timestamp_custom('%H:%M', false) }}

Let me know if it works the way you want it to. If not, I’m sure it can be adjusted to meet your requirements.

1 Like

Sorry to revive an old thread but will this work over more than one day? I want to keep a record of how long my heating switch has been on for over the course of a month and then reset at say 1am on the first day of each month but struggling to find a way to do it!

Theoretically, yes, it can work for periods greater than a day. Practically, no, because when you restart Home Assistant, all entities get their last_updated property reset to the current time.

I want to keep a record of how long my heating switch has been on for over the course of a month

The technique shown above would not be the way to do that. You want cumulative time the switch was on over the course of a month. That’s one of the things that the History Statistics sensor can do. It analyzes the historical data in the recorder database. However, by default, it is configured to record a maximum of 10 days worth of data.

If you extend it to a month, the database’s size may become substantial (it records everything and not in the most efficient manner). It was never designed for (efficient) long-term storage. For that purpose, it’s recommended to use alternative storage solutions such as InfluxDB.

Ah ok, I’ve had a brief read of the influxdb page and the recorder page so that makes sense. Influxdb looks a bit above my skill level so I don’t think I’ll be going that route! Looking at the recorder integration though could I limit the amount of things recording to it (basically just thermostats, switches and lights, approx 15 entities) and then extend the length it keeps them to 30 days? Or would this still lead to a massive database size? I’m running HA on a raspberry pi 4, 2gb with a 32gb SD card if that helps?

You can try that strategy and after a week you can estimate how large the database will grow in a month.

Things you should know:

  • When you create a full snapshot, it includes the database, and is stored on your 32Gb SD card. Keep that in mind when estimating how much ‘headroom’ is available on the SD card.
  • I’ve seen reports of database corruption (usually, and unfortunately, when a snapshot is restored). The standard ‘fix’ is to simply delete the database and allow Home Assistant to create a new (empty) one. That’s probably not comforting news given that you want to preserve a month’s worth of data.