Time elapsed counter

I have a sensor on a dryer that tells home assistant when it is on and off. I’d like to add a card to my lovelace that shows me how long the dryer has been on. I’m looking for it to display in HH:MM:SS. I know I could use the timer component to count down, but I want one that counts up. I looked at the history stat sensor but it was only showing me the amount of time the dryer had run in a 24 hour period. I want the elapsed time to reset every time the dryer turns on.

Can this be done?

Here’s one way that might work for you. (I tested it and it seems to work ok. Obviously replace input_boolean.dryer with your dryer on sensor.) Don’t forget to exclude sensor.dryer_on_time from the recorder, otherwise it will “spam” your database. :slight_smile:

input_boolean:
  dryer:
input_datetime:
  dryer_start:
    has_date: true
    has_time: true
automation:
  - trigger:
      platform: state
      entity_id: input_boolean.dryer
      to: 'on'
    action:
      service: input_datetime.set_datetime
      entity_id: input_datetime.dryer_start
      data_template:
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
  - trigger:
      platform: time_pattern
      seconds: '*'
    condition:
      condition: state
      entity_id: input_boolean.dryer
      state: 'on'
    action:
      service: homeassistant.update_entity
      entity_id: sensor.dryer_on_time
sensor:
  - platform: template
    sensors:
      dryer_on_time:
        value_template: >
          {{ (as_timestamp(now())
              - state_attr('input_datetime.dryer_start', 'timestamp'))
             |timestamp_custom('%H:%M:%S', false) }}
6 Likes

Can you please clarify the code for the automation? I’m having trouble with the two triggers as I’ve never stacked them like that before. Could I have it this way instead as that passes the code checker.

  - alias: 'Soldering Iron Timer'
    trigger:
      - platform: state
        entity_id: switch.soldering_iron
        to: 'on'
      - platform: time_pattern
        seconds: '*'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.soldering_iron_start
        data_template:
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
      - service: homeassistant.update_entity
        entity_id: sensor.soldering_iron_on_time

[EDIT] My bad. Didn’t realise these were two separate automations. Working a treat now thank you!

  - alias: 'Soldering Iron Timer'
    trigger:
      - platform: state
        entity_id: switch.soldering_iron
        to: 'on'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.soldering_iron_start
        data_template:
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

  - alias: 'Update Soldering On Time'
    trigger:
      platform: time_pattern
      seconds: '*'
    condition:
      condition: state
      entity_id: switch.soldering_iron
      state: 'on'
    action:
      service: homeassistant.update_entity
      entity_id: sensor.soldering_iron_on_time

Furthermore, if you want to use just the ‘minutes on’ for a TTS notification as I do, you can use this.

          {{ (as_timestamp(now())
              - state_attr('input_datetime.soldering_iron_start', 'timestamp'))
             |timestamp_custom('%M', false) | int }}

That would just show the minute part which would reset to zero every hour. I think you’d want this instead:

          {{ ((as_timestamp(now())
               - state_attr('input_datetime.soldering_iron_start', 'timestamp'))
             / 60) | int }}
1 Like

Thanks for the tip. In my instance, I’m only interested in time between 0 and 30 minutes but that will be good to know for other appliances that may be on longer.

Thanks, pnbruckner! This worked!

1 Like

A couple follow ups.

Is there a way to keep the 2nd automation from spamming my logbook? It looks like it shows it triggers roughly every second.

I have an automation that sends an ios notification when the dryer is done.

.....
  action:
  - data:
      data:
        action_data:
        push:
          badge: 5
          category: fullautodryeroff
          sound: fire-truck-air-horn_long-and-loud.wav
      message: FULL-AUTO MODE The dryer turned off at {{ now().strftime("%I:%M:%S%p")
        }}
    service: notify.ios_mikes_iphone_11

This tells me what time the dryer turned off. Instead, could I have the notification include the amount of time the dryer ran by sending the state of the entity sensor.dryer_on_time? If so, how would I do that?

I would think you’d want to prevent the information from being recorded at all. If so see:

Otherwise, if you just want to prevent them from being shown in the Logbook, see:

Wouldn’t that just be:

      message: FULL-AUTO MODE The dryer was on for {{ states('sensor.dryer_on_time') }}

I added this in my configuration to stop the recorder, but it’s not stopping the logbook.

recorder:
  exclude:
    entities:
     - sensor.dryer_on_time

I also spoke too soon on this working. I tested this on my personal HA and it worked but it’s not working on the different HA that I’m trying to add this to. Not sure if it matters, but the different HA is running version 0.88.2. I plan to update that eventually, but can’t update it now.

Here is my config and automation file on the HA it’s not working on. I can see both automations trigger in the logbook, but the state of sensor.dryer_on_time is not changing. It just shows “unknown”.

binary_sensor.five is the sensor that tells me if the dryer is on or off.

Configuration.yaml.

sensor:
#Provides sensor for dryer elapsed time counter
  - platform: template
    sensors:
      dryer_on_time:
        value_template: >
          {{ (as_timestamp(now())
              - state_attr('input_datetime.dryer_start', 'timestamp'))
             |timestamp_custom('%H:%M:%S', false) }}

#Dryer Elapsed Time Countup
input_boolean:
  dryer:
input_datetime:
  dryer_start:
    has_date: true
    has_time: true

Automations.yaml

- id: '1571921141280'
  alias: Dryer Elapsed Time1
  trigger:
      platform: state
      entity_id: binary_sensor.five
      to: 'on'
  action:
    service: input_datetime.set_datetime
    entity_id: input_datetime.dryer_start
    data_template:
      datetime: '{{ now().strftime(''%Y-%m-%d %H:%M:%S'') }}'
- id: '1571921296108'
  alias: Dryer Elapsed Time2
  trigger:
    platform: time_pattern
    seconds: '*'
  condition:
    condition: state
    entity_id: binary_sensor.five
    state: 'on'
  action:
    service: homeassistant.update_entity
    entity_id: sensor.dryer_on_time

Then you probably need to exclude it from the Logbook, too. See the second link above.

Ha ha ha ha ha ha! :laughing: :laughing:

Sorry, but couldn’t resist. Do you know how much has changed since then?! I suspect the time_pattern trigger didn’t exist yet. And maybe not the homeassistant.update_entity service either. If you want help with that old of a release, you might be on your own here.

Well shoot, that’s kind of what I figured. I’ll find a time to update that HA then try again. The HA is installed about an hour away at our farm and handles some automations for our harvest. We are in the middle of harvest and behind schedule. The last thing I want to do is update and cause issues that will slow us down.

Hi!
Adding my two cents to this older thread as it popped up in my search.
I was trying to make something that showed me the minutes my TV has been on.

https://www.home-assistant.io/integrations/history_stats/ seems to be doing the trick for me :slight_smile:

sorry to bump an old thread but I managed to get this working, sort of. I can get the time on to display in minutes when the ‘input_boolean’ is switched on, but when the same ‘input_boolean’ is switched off it just keeps on counting up. i used @xbmcnut’s example. i feel like there’s something straightforward that I’m missing here…

EDIT: nvm, found what i needed in this thread: How long switch has been on for? - #2 by 123

Hi,
Sorry to revive this old thread again but I’ve faced a similar use case and I wanted to share my approach by using the “new way” of templating sensors instead of using the “legacy” version.

Preamble:

  • I have a Shelly PM mini that measures the power of my “dummy” washing machine

Code:

  • I’ve created a binary_sensor that tells me if the washing machine is on/off based on instantaneous power consumption
- binary_sensor:
    - name: Washing Machine
      unique_id: washing_machine
      icon: mdi:washing-machine
      delay_off:
        minutes: 5
      state: >
        {{ states('sensor.pm_washing_machine_power')|float > 0.0 }}

The delay (as per the example on the official documentation) is to avoid triggering on/off continuously at the latest stages.

Then I created a helper sensor that updates every minute but checks if the previous binary sensor is on. If true, keep counting the minutes, otherwise it remains still. Every time the sensor changes again from off to on, the timer resets.

- trigger:
   - platform: time_pattern
     seconds: "*"
 sensor:
   - name: Washing Machine Duration
     unique_id: washing_machine_duration
     device_class: "duration"
     unit_of_measurement: "min"
     icon: mdi:update
     state: >
       {% if is_state('binary_sensor.washing_machine', 'on') %}
         {{ ((now().timestamp() - states.binary_sensor.washing_machine.last_changed.timestamp()) /60) |int(0) }}
       {% else %}
         {{ this.state |int(0) }}
       {% endif %}


In the image, the helper sensor was in seconds just for debugging, even if it’s written ‘minutes’.

Hope to have helped someone!

1 Like