Retry an action in an automation when entity is not available

Hey there,

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 :rofl: (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’ :smiley:

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?

1 Like

Well …

Ask a stupid question …

Get the bleeding obvious answer
:rofl:

If I’d just stopped to think about it, Thanks

Hey there,

thank you for you advice.

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:

alias: Carport neu
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.000bd5699d4e7a_motion
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.000bd5699d4e7a_illumination
    below: '0.21'
action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.jeq0049593
  - delay: '0:00:30'
  - repeat:
      while:
        - condition: template
          value_template: '{{ not is_state(''switch.jeq0049593'', ''off'') }}'
      sequence:
        - service: switch.turn_off
          data: {}
          entity_id: switch.jeq0049593
mode: single


Two years later you can simply repeat until the device has the desired state with a maximum amount of retries and a delay :slight_smile: :

alias: Turn off garden lights when sun rises
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: 0
condition: []
action:
  - repeat:
      until:
        - condition: or
          conditions:
            - condition: device
              type: is_off
              device_id: 176d5f35f9dc52172802b832e1d2220f
              entity_id: light.twinkly_679fd1
              domain: light
            - condition: template
              value_template: repeat.index > 60
      sequence:
        - type: turn_off
          device_id: 176d5f35f9dc52172802b832e1d2220f
          entity_id: light.twinkly_679fd1
          domain: light
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
mode: single

2 Likes

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.

Thanks for this add on. Solved a problem I had with turning on/off a Levoit humidity sensor when the network was unstable.

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?


alias: VarmegolvOn0200
description: ""
trigger:
  - platform: time
    at: "02:00:00"
condition: []
action:
  - repeat:
      sequence:
        - type: turn_on
          device_id: a2bb44964c1b14fa64008c84e77cd786
          entity_id: d61165987eb611011535c9f3e477d87a
          domain: switch
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
      until:
        - condition: device
          type: is_on
          device_id: a2bb44964c1b14fa64008c84e77cd786
          entity_id: d61165987eb611011535c9f3e477d87a
          domain: switch
        - condition: template
          value_template: repeat.index > 60
          enabled: true
mode: single

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.