I use HA now for quite a lot of days and now it’s time to get deeper in it.
I made an automation to switch off two lights in the garden at 11p.m…
Sometimes it happens that one of the lights (a hue bulb) is not available at 11p.m. but for example 15 minutes later.
How could I edit my automation so that homeassistant checks if the light is available and if it is not retry the switch off every 30 seconds until it is available again.
This is my automation so far:
alias: Turn off the lights at 11 pm
description: ''
trigger:
- at: '23:00'
platform: time
condition: []
action:
- wait_template: '{{ not is_state(''light.hauszuweg_haus'', ''unavailable'') }}'
- wait_template: '{{ not is_state(''light.hauszuweg_strasse'', ''unavailable'') }}'
- device_id: 3268f94475e840013694d71264c28015
domain: light
entity_id: light.hauszuweg_haus
type: turn_off
- device_id: 4c41362263a2489f83b7db487493770b
domain: light
entity_id: light.hauszuweg_strasse
type: turn_off
mode: queued
max: 10
What you have is probably not too bad, but I’d recommend at least combining the two waits into one:
- wait_template: '{{ not is_state(''light.hauszuweg_haus'', ''unavailable'') and not is_state(''light.hauszuweg_strasse'', ''unavailable'') }}'
But it would probably make more sense to test if the lights are not off, and if so, try turning them off and then checking again repeatedly until they are both off, probably with some sort of timeout in case they’ve somehow failed completely. Maybe something like this:
alias: Turn off the lights at 11 pm
description: ''
trigger:
- at: '23:00'
platform: time
condition: []
action:
- repeat:
while:
- "{{ not is_state('light.hauszuweg_haus', 'off') or
not is_state('light.hauszuweg_strasse', 'off') }}"
- condition: time
before: '23:30'
sequence:
- device_id: 3268f94475e840013694d71264c28015
domain: light
entity_id: light.hauszuweg_haus
type: turn_off
- device_id: 4c41362263a2489f83b7db487493770b
domain: light
entity_id: light.hauszuweg_strasse
type: turn_off
Lastly I would not recommend using queued mode for this automation.
I appreciate such tips, but most people would like to have a bit more meat on these bones (me included in this case) though I would not have ever considered using queued here, but have no fully formed argument as to why not.
Thanks in advance.
PS I take it, work has dialed back a bit from ‘manic’
Well, in my second suggestion it wouldn’t really matter, because there’s no way for the automation’s actions to still be running when 23:00 rolls around again.
But in the OP, what would be the point in queuing up another attempt to wait for the lights to become available if it’s still waiting for that to happen from the previous day’s trigger event?
Just another but related question:
I want to switch off another light 30 seconds after motion is detected. But same game: sometimes the switch is unavailable. what do you think about this code:
Thanks, needed this. I have an automation to turn on my aircon to warm up the house on cold mornings, and today it failed because my AC unit’s web server wasn’t responding when the automation ran.
Great, however, after I now try the action manually which turns on a switch I no longer can turn it off again, it is turned on every minute.
Any ideas?
How soon are you trying to turn off the switch? I think if you don’t wait for a full minute, then the delay will still be active and when it ends and the condition is checked, the switch is off, so it will try again.