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.
- 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.
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.