Power outage recovery - automation trigger when switch is OFF on boot

Hey, I have a multiple Xiaomi Aqara walls witches working in decoupled mode driven by zigbee2mqtt software. The only problem with the setup is that after power outage decoupled switches requires me to turn their relays on so the Home Assistant can control wifi enabled lights.

To solve this problem I have the automation that enables all relays when they turn off but it’s not enough:

- alias: "Force turn on switch.corridor_wall"
  trigger:
    platform: state
    entity_id:
      - switch.corridor_wall
    to: "off"
  action:
    - delay: "00:00:01"
    - service: switch.turn_on
      data:
        entity_id: "switch.corridor_wall"

It doesn’t work after boot because the sitch is OFF and does not “change to OFF”. How can I solve this? Is there a trigger that works when the switch is OFF? Or can I somehow check if switch is OFF at boot and set it as a trigger?

You can trigger when HA starts and use a condition that the switch is off.

- alias: "Force turn on switch.corridor_wall"
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: switch.corridor_wall
      state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.corridor_wall

I’m guessing you probably don’t need the delay, too.

You can also combine this with the original automation if you like.

- alias: "Force turn on switch.corridor_wall"
  trigger:
    - platform: state
      entity_id: switch.corridor_wall
      to: 'off'
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: switch.corridor_wall
      state: 'off'
  action:
    - delay: '00:00:01'
    - service: switch.turn_on
      entity_id: switch.corridor_wall

This will trigger when either event happens.

1 Like

That’s great solution, thank you! I am still learning :slight_smile:

I had to add for: "00:00:10" in condition. Apparently it takes few seconds for HA to get the current value and it triggered even if switch was already on. This is my finished automation for one switch: The 30 seconds delay is required for the smart Yeelight lamp to connect to Wi-Fi network.

Is there a better way to wait until an entity is online?

# force turn on switches
- alias: "Turn on switch.corridor_wall on boot and turn off smart lamp"
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: switch.corridor_wall
      state: "off"
      for: "00:00:10"
  action:
    - service: switch.turn_on
      entity_id: switch.corridor_wall
    - delay: "00:00:30"
    - service: switch.turn_off
      entity_id: light.corridor_yeelight_lamp
- alias: "Force turn on switch.corridor_wall"
  trigger:
    platform: state
    entity_id:
      - switch.corridor_wall
    to: "off"
  action:
    - service: switch.turn_on
      data:
        entity_id: "switch.corridor_wall"

The for in the condition does not delay the trigger. Your condition says that the switch must have been off for at least 10 seconds when HA starts. But the switch cannot have been anything for 10 seconds when HA starts, because the time doesn’t start until, at least, after HA starts.

You can delay the homeassistant start event as follows. I do this in my system for automations that need to wait for initialization to complete.

- trigger:
    platform: homeassistant
    event: start
  action:
    - delay: 30
    - event: delayed_homeassistant_start

Now you can use this new event to trigger your automation:

- trigger:
    - platform: event
      event_type: delayed_homeassistant_start
    - platform: state
      entity_id: switch.corridor_wall
      to: 'off'
  condition:
    condition: state
    entity_id: switch.corridor_wall
    state: 'off'
  action:
    ...

Sounds logic to me, thank you. This is my current solution:

# force turn on switches
- trigger:
    platform: homeassistant
    event: start
  action:
    - delay: "00:00:10"
    - event: delayed_homeassistant_start

- trigger:
    - platform: state
      entity_id:
        - switch.office_wall_left
      to: "off"
    - platform: event
      event_type: delayed_homeassistant_start
  condition:
    - condition: state
      entity_id: switch.office_wall_left
      state: "off"
  action:
    - event: office_wall_left_lamp
    - service: switch.turn_on
      data:
        entity_id: "switch.office_wall_left"

- trigger:
    - platform: event
      event_type: office_wall_left_lamp
  condition:
    - condition: template
      value_template: "{{ states('light.office_yeelight_lamp') != 'on' }}" 
  action:
    - delay: "00:00:60"
    - service: light.turn_off
      entity_id: light.office_yeelight_lamp

If the switch (relay) turns off for no reason (it happened few times) I turn it on and check if the Wi-Fi lamp was on, if not then I wait 60 seconds for the lamp to reconnect and then turn it off.
After reboot, I do the same. Setting the condition template like this works even if the light is unavailable.

I would love to wait till the lamp is online but don’t know how to do it. For now 60 seconds delay is good enough.

Edit: I have just found wait_template. I will play with it.
Thanks again @pnbruckner!

You can combine the last two automations into one because scripts (which the action part of an automation is) can contain condition steps.

- trigger:
    - platform: state
      entity_id:
        - switch.office_wall_left
      to: "off"
    - platform: event
      event_type: delayed_homeassistant_start
  condition:
    - condition: state
      entity_id: switch.office_wall_left
      state: "off"
  action:
    - service: switch.turn_on
      data:
        entity_id: "switch.office_wall_left"
    - condition: template
      value_template: "{{ states('light.office_yeelight_lamp') != 'on' }}" 
    - delay: "00:00:60"
    - service: light.turn_off
      entity_id: light.office_yeelight_lamp

Now it’s perfect! (I hope :slight_smile:):

- trigger:
    - platform: state
      entity_id:
        - switch.office_wall_left
      to: "off"
    - platform: event
      event_type: delayed_homeassistant_start
  condition:
    - condition: state
      entity_id: switch.office_wall_left
      state: "off"
  action:
    - service: switch.turn_on
      data:
        entity_id: "switch.office_wall_left"
    - condition: template
      value_template: "{{ states('light.office_yeelight_lamp') != 'on' }}"
    - wait_template: "{{ states('light.office_yeelight_lamp') == 'on' }}"
      timeout: "00:03:00"
      continue_on_timeout: "false"
    - service: light.turn_off
      entity_id: light.office_yeelight_lamp
1 Like