This is my first post so please forgive me if this is not the right forum for this.
I was hoping someone could look over the first automation that I created using the GUI and let me know if they see any problems with it or perhaps a more efficient way to create the same automation.
The switch controls towel warmers in our bathrooms.
I was wondering what the best way to handle the case if one of the off triggers doesn’t fire (to turn the switch off) for some reason, such as HA goes off line for a minute or two but then comes back online a few minutes later. Should I add some sort of automation that when HA starts it makes sure the Towel warmer switch gets turned off if its state is “on”?
Any help or comments/improvements on the below automation yaml would be appreciated:
alias: 'Towel Warmers automation '
description: Turn on and off towel warmers based on time and day.
trigger:
- platform: time
at: '05:30:00'
id: tw_turn_on_weekday
- platform: time
at: '07:30:00'
id: tw_turn_off_weekday
- platform: time
at: '08:00:00'
id: tw_turn_on_weekend
- platform: time
at: '10:00:00'
id: tw_turn_off_weekend
condition: []
action:
- choose:
- conditions:
- condition: trigger
id: tw_turn_on_weekday
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
sequence:
- service: switch.turn_on
target:
entity_id: switch.1000539b1b
- conditions:
- condition: trigger
id: tw_turn_off_weekday
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
sequence:
- service: switch.turn_off
target:
entity_id: switch.1000539b1b
- conditions:
- condition: trigger
id: tw_turn_on_weekend
- condition: time
weekday:
- sat
- sun
sequence:
- service: switch.turn_on
target:
entity_id: switch.1000539b1b
- conditions:
- condition: trigger
id: tw_turn_off_weekend
- condition: time
weekday:
- sat
- sun
sequence:
- service: switch.turn_off
target:
entity_id: switch.1000539b1b
default: []
mode: single
It’s not in the right forum (but easily fixed). “Share your Projects!” is for sharing the (functional) project you have created. You have a question about an automation, not a project to share, so please take a moment to change the topic’s category to “Configuration”.
Regarding your question, you may be interested in reviewing this post which explains how to make an automation more robust (i.e. properly handle missed events).
The following automation triggers at four scheduled times of the day and at startup.
When triggered, it computes two variables:
The variable t is the current time expressed as a tuple. For example, (7,30) is 07:30.
The variable mode is the switch’s desired mode based on whether the current day is a weekday or weekend and if the current time is within the appropriate time range.
The automation’s action simple sets the switch to the computed mode.
alias: 'Towel Warmers automation '
description: Turn on and off towel warmers based on time and day.
trigger:
- platform: time
at:
- '05:30:00'
- '07:30:00'
- '08:00:00'
- '10:00:00'
- platform: homeassistant
event: startup
condition: []
action:
- variables:
t: '({{ now().hour }},{{ now().minute }})'
mode: >
{% if 1 <= now().isoweekday() <= 5 %}
{{ 'on' if (5,30) <= t < (7,30) else 'off' }}
{% else %}
{{ 'on' if (8,0) <= t < (10,0) else 'off' }}
{% endif %}
- service: 'switch.turn_{{ mode }}'
target:
entity_id: switch.1000539b1b
mode: single
The template that computes mode works like this:
If the current day is between 1 and 5 inclusively, it’s a weekday, otherwise it’s a weekend.
If it’s a weekday, report on if the current time is equal or greater than 05:30 and less than 07:30, otherwise report off.
If it’s a weekend, report on if the current time is equal or greater than 08:00 and less than 10:00, otherwise report off.
EDIT
If you are uncomfortable with the practice of representing time as a tuple, it can be easily redesigned so that time is represented as a string.