Getting the number of minutes that a binary sensor has been on, then resetting to zero when turned off

Some background.

I have my washing machine connected to an energy monitoring plug. I’m using that to monitor the progress of a laundry cycle.

So far I have the following working:

  • Hourly energy consumption of the washing machine
  • Hourly cost to run the machine
  • Current state of the washing machine based on instantaneous consumption (e.g. wash, spin, dry, etc)
  • Notifications firing when the laundry cycle has finished

However, the last sensor I’d like to add is proving a little tricky. Basically I want to show how long the machine has been active for in its current cycle. I have a binary sensor that activates when energy consumption goes over a certain threshold (the machine starts its programme) and then turns off when it dips below (the machine is finished). I’d like to get the number of minutes that the sensor is ‘on’ since it was turned on, or otherwise zero.

My first thought was a history_stats sensor, but unless I’m missing something it looks like this only returns hours, not minutes, which is not granular enough. Below is the config I was using:

  - platform: history_stats
    name: "Laundry duration"
    entity_id: binary_sensor.washing_machine_in_use
    state: on
    type: time
    start: '{{ states.binary_sensor.washing_machine_in_use.last_updated }}'
    end: '{{ utcnow() }}'

This just seems to return zero all the time.

Is there any other way I can achieve what I want, perhaps with a template_sensor instead? Kind of stumped on this one.

1 Like

Here’s a couple of things I spotted, I have quotes around on and if you look at the sensor in Developer Tools State <> the sensor has a value attribute. In my testing I see the state as 0.17 and the value attribute contains 10m ( I think that means 10 minutes) Hope that helps

I can confirm that the History Statistics Sensor, configured the way you have it, didn’t work for me either.

I used it to monitor the on state of an input_boolean. Even with state: 'on', it failed to display the elapsed time. I’m not sure why it didn’t work because everything seems to be in order.

  - platform: history_stats
    name: Toggler duration
    entity_id: input_boolean.toggler
    state: 'on'
    type: time
    start: '{{ states.input_boolean.toggler.last_updated }}'
    end: '{{ utcnow() }}'

As an alternative, a Template Sensor can do the job (you also need sensor.time defined as well).

The following example uses sensor.time to update the Template Sensor every minute. It reports the elapsed hours and minutes since the input_boolean was turned on.

Because it only updates every minute (not every second), the accuracy is (worst case) +/- 1 minute (it depends on when sensor.time fires with respect to last_updated). If you must have accuracy down to the second, this isn’t the right solution for you.

For your application, just replace the input_boolean with your binary_sensor.

  - platform: template
    sensors:
      toggler_time:
        friendly_name: 'Toggler ON time'
        entity_id: sensor.time
        value_template: >-
          {% set t = 0 if states('input_boolean.toggler') == 'off' 
             else (utcnow() - states.input_boolean.toggler.last_updated).seconds %}
          {{ "{:02d}:{:02d}".format(t // 3600, (t % 3600) // 60 }}

It can display seconds but, again, it doesn’t update every second so the reported seconds only refresh every minute. Here’s the template for displaying H:M:S.

          {{ "{:02d}:{:02d}:{:02d}".format(t // 3600, (t % 3600) // 60, (t % 3600) % 60) }}
3 Likes

That did the trick! Thanks very much!