I need to make some manual switches in fronted and I need to to manual turn them on and after x minutes to automatically turn off. Right now I have 2 automations one to turn them on and one turn them off.
Can this be done with one automation (given the fact that there are now some more capabilities) or should I continue to apply 2 automations for each switch?
right now I have the the following switch (it is a basic sonoff)
and I have this code
- id: Bathroom Heater Off Time
alias: Bathroom Heater Off Time
trigger:
entity_id: switch.sonoff_10001bb126
platform: state
to: 'on'
for:
minutes: '15'
action:
service: switch.turn_off
entity_id: switch.sonoff_10001bb126
Ideally I would like to have 3 switches
water heater 15min (when I manually switch it on to turn off after 15 minutes)
water heater 30min (when I manually switch it on to turn off after 30 minutes)
water heater 60min (when I manually switch it on to turn off after 60 minutes)
So I have to make a script to turn on the water heater - then delay - then turn off after 15 min for the first script. (and after 30 min the second script etc)
it makes sense I think. ?
You can use an input_number to set the delay. So your automation would look like this:
- id: Bathroom Heater Off Time
alias: Bathroom Heater Off Time
trigger:
entity_id: switch.sonoff_10001bb126
platform: state
to: 'on'
action:
- delay: "{{ states('input_number.minute_delay') | multiply(60) | int }}"
- service: switch.turn_off
entity_id: switch.sonoff_10001bb126
this looks very good! Never used input number before.
so I added the following input number
and updated my automation to this
- id: Bathroom Heater Off Time input Number
alias: Bathroom Heater Off Time input Number
trigger:
entity_id: switch.sonoff_10001bb126
platform: state
to: 'on'
action:
- delay: "{{ states('input_number.water_heater_minutes') | multiply(60) | int }}"
- service: switch.turn_off
entity_id: switch.sonoff_10001bb126
I think it is even better than I thought
@123
for the particular automation I manually switch it on depending of how many users will take a bath
**
in the above example where input number is 25 I get 1500 as a result in developer tolls template.
so the 1500 is the delay in seconds I guess. right?
FWIW, you can also use a template in the for option.
- id: Bathroom Heater Off Time input Number
alias: Bathroom Heater Off Time input Number
trigger:
entity_id: switch.sonoff_10001bb126
platform: state
to: 'on'
for: "{{ states('input_number.water_heater_minutes') | multiply(60) | int }}"
action:
- service: switch.turn_off
entity_id: switch.sonoff_10001bb126
You should also know that restarting Home Assistant during any countdown will cancel the countdown.
For example, if a timer is counting down, or a for, or a delay, and you restart Home Assistant, upon startup the countdown will not resume from where it was interrupted (the countdown is canceled). Another way to describe this behavior is that a timer, for, and delay, “don’t survive a restart”.
Similarly, some templates calculate time duration by subtracting and entity’s last_changed from the current time. For example, this checks if the front door’s state changed more than 3 hours ago.
{% if now() - states.binary_sensor.front_door.last_changed > timedelta(hours=3) %}
However, when you restart Home Assistant, an entity’s last_changed is set to the time of the restart (i.e. the value it had is lost). Obviously, that affects the accuracy of calculating a time duration.
I would have learned it in the hard way sometime. Now I will keep it in my mind. I will also add an automation to turn off the water heater above 60 degrees.
Thanks again.
If you REALLY need a “timer” to survive the restart, the best method I’ve seen with built-in Home Assistant Tools is to set an input_datetime to when the “timer” should stop. Then use a time trigger on that input_datetime to do your “timer end” actions.
It takes SOOO many more lines of YAML, but, you can replace a delay, a “for” in a trigger, a builtin “timer” entity, etc with a series of automations that use this pattern surrounding an input_datetime. With tools like AppDaemon, PyScript, and Node-RED, you can make the automation logic for this timer repeatable, which helps a lot. Home Assistant has a new feature to be announced during the conference called “blueprints” that will make “repeatable automation patterns” like this quite a bit easier to … well… repeat, assuming all the features are there. I guess we’ll see.