Switch run time timer template

Tags: #<Tag:0x00007f326d4546f0> #<Tag:0x00007f326d4544e8>

Is there a way to track a switch’s ‘on’ state time? I have a humidifier and I know how long it takes for the tank to empty out. I have it on an automation for when the humidity drops so low it turns on then back off once it gets to a higher set point.

I want to receive a notification if the humidifier switch state has been ‘on’ at or over something like 6 hours. I would also have to make a button that press when I refill the tank to reset the run time back to 0.

If it helps any I have the home-assistant-variables installed so I can make custom variables. Also been looking at the history_stats.

- alias: Tank Reset
  trigger:
    platform: state
    entity_id: switch.humidifier
    to: 'on'
    for:
      hours: 6
  action:
    - service: notify.notify
      data:
        title: "Humidifier"
        message: "Refil me"

This wont survive through reboots. In fact, many solutions wont unless you record the turn_on event into an input_datetime for persistent storage and compare against that.

As for the button reset…I would instead track an input_boolean that is ‘on’ for 6 hours. This boolean just folows the switch.humidifier state. But a button press can turn the input boolean off/on without interrupting the humidifier and would restart the timer.


The other option is to just make a timer. then attach an automation to the timer expiring for the notification . A button press can simply restart this timer.

- alias: Tank Timer
  trigger:
    platform: state
    entity_id: switch.humidifer
    to: 'on'
  action:
    - service: timer.restart
      entity_id: timer.tank_timer

I figured something out using the home-assistant-variables to keep track of runtime for each humidifier.

I created 2 variables.

var.humidifier_up_on_at = epoc timestamp
var.humidifier_up_runtime = runtime in seconds.

I have 2 automations that trigger when the humidifier is turned on and off.

- alias: Humidifier Up Turned On
  mode: single
  trigger:
    - platform: state
      entity_id: switch.eb71aa9a76450cd925ijoh
      to: 'on'
  action:
    - service: var.set
      data:
        entity_id: var.humidifier_up_on_at
        value: '{{ now().strftime("%s") | int }}'

- alias: Humidifier Up Turned Off
  mode: single
  trigger:
    - platform: state
      entity_id: switch.eb71aa9a76450cd925ijoh
      to: 'off'
  action:
    - service: var.set
      data:
        entity_id: var.humidifier_up_runtime
        value: >-
          {{ ((now().strftime("%s") | int) - (states('var.humidifier_up_on_at') | int)) + (states('var.humidifier_up_runtime') | int) }}

When the humidifier is turned on it sets var.humidifier_up_on_at to the current epoc time. When the humidifier is turned off it takes the current epoc and subtracts var.humidifier_up_on_at from that then adds var.humidifier_up_runtime.

That was the tricky part. Now when var.humidifier_up_runtime goes over a set value of something like 21,600 (6 hours in seconds) it will send the notification. I didn’t include that code as I haven’t added it yet.

Why did you choose not to use the History Stats sensor? It can easily do what you want. I use it to track how long my furnace runs every day.

It would look something like this:

- platform: history_stats
  name: Humidification Today
  entity_id: switch.eb71aa9a76450cd925ijoh
  state: 'on'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'
1 Like

I looked at that and couldn’t see how it would fit into what I want. The tracking I need can be random as I have the humidifiers turn on/off based on the rooms humidity. I also need a way to reset the runtime when I refill the tanks. I might have to refill them 2 times a day or I might have to refill them once every 4 days depending on how much my furnace runs.