Help with automating light after power outage

Guys, after a power outage, all smart lamps turn on for me, if this happened at night it is very unpleasant. Please help me make an automation that will turn off the llamas that turned on themselves after the electricity was first turned off, and then turned on.

Most devices have a power on setting that you can say “turn back to last state”.

What kind of outlets/lamps do you have?

There’s no way for home assistant to know if there was a power outage. You can have it turn off all lights after it starts, but that would be just as annoying should you ever restart it normally.

1 Like

Xiaomi Smart Bulb E27 and NOUS Smart Wifi Bulb P1

Hmm, yes. I see the problem.

Smart Bulbs are all designed to turn on when power is applied. This is so if someone uses them with a normal light switch, they will work as expected.

The hardest thing here will be trying to figure out a power outage vs normal restart. What you could do is capture the home assistant stop event and set an input_boolean to true when you see that. Then on startup, you would clear that.

If on startup that wasn’t set, then home assistant rebooted unexpectedly…probably from a crash or server stopping. You won’t be able to tell the difference.

Finally, you have to make sure all of these devices are back online before running this automation. Assuming a power outage, your router will restart, etc.

You would have to create an input boolean.ha_stop_normal with no initial state.

- alias: HA Stop Normal
  trigger:
    platform: event
    event_type: homeassistant_stop
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.ha_stop_normal


- alias: Check For Power Outages
  trigger:
    platform: event
    event_type: homeassistant_start
  action:
    # If this is on, no need to wait. Just continue and turn it off. 
    # If this is off, will wait here for 3 minutes to give time for other
    # entities to come back to life. 
    # Might have to tweak this timer...
    - wait_template: "{{ is_state('input_boolean.ha_stop_normal', 'on') }}"
      timeout: "00:03:00"
      continue_on_timeout: true
    - choose:
      - conditions:
          # On power outage, this flag won't be set. So do everything we need here assuming
          # a power outage. 
          - "{{ is_state('input_boolean.ha_stop_normal', 'off') }}"
        sequence:
          - service: light.turn_off
            entity_id: light.light1, light.light2, light.light3

    # In all cases, turn this off if it's on. If it's already off, oh well....turn it off again. 
    - service: input_boolean.turn_off
      entity_id: input_boolean.ha_stop_normal
    

That should be a start. The annoying thing is the lights are going to turn on for a little bit of time, then turn off. There is no stopping that really. They are going to turn on as soon as they get power…well before home assistant has a chance to monitor their state (since they can’t report state without power).

4 Likes

How likely is it that during ‘normal operation’ every single light will be on at 100%?

If it’s very very unlikely you could set a binary sensor to turn on if all the lights are at 100%, and trigger an automation to turn them off.

Maybe you can just monitor the transition of the lamp state from “Unavailable” to “On”?

Is you HA on a UPS or does it also reboot?

Do you have any way to know you lost power, such as a UPS status, saying on batt or something?

It is easy to solve this if you have some way to know.

the same reboots

Hi there. I am also searching for a solution to this.

Whenever there is a power outage and the power comes back, my bedroom bedside lamps (Yeelight 1SE) turn on and if this happens in the middle of the night it is quite annoying. Apart from that, if the outage happens and we are not home then the lights will be also on for no reason.

I know that there is a setting for them to not power on after power loss but I want to have this enabled because my wife sometimes turns her lamp on with the physical switch instead of the Sonoff button I have installed right next to it (old, hardwired habits. What can you do?)

I think the only way to go forward with this is with a UPS plugged in Home Assistant. I do have a CyberPower UT850 for my router and the RPi4 where HA runs from and I will start experimenting with this idea.

Perhaps an automation to check light states when the UPS goes from ‘Online’ to ‘Offline’ (and then back ‘Online’ maybe?)

The real trick will be to check for the lights state after the power is back online and for enough time for the lights to connect back to the router and get their local connection.

Already sounds complicated and I will have to think this through.

Any suggestions and ideas are welcome!

Edit: I see that there are many topics in the community forum about this issue so I don’t know if this thread is the best one to go on with.

My solution would be to disable the physical switch via bypass or lockout