What to do when the electricity goes down?

I am looking for a script or an automation to toggle off all the lights that go on after a crash of the electricity. As you know when the power goes off and then goes on, all the wifi lights are powered again and, if this happens in the middle of the night (as occured to me just last night) it’s very annoying as i must wait the router goes on again, HA is restarted and then power off all the lights from the frontend.
Is there someone who created a script or an automation to make this process automatic aa the router and HA are up again?

How to…?

… Not write a good title. Please see point 7 here: How to help us help you - or How to ask a good question

This is all I use, but my router and home assistant server are on a UPS:

- id: mains_power_on_alert
  alias: 'Mains Power On Alert'
  initial_state: true
    platform: state
    entity_id: binary_sensor.power_fail
    to: 'on'
  - service: notify.telegram_system
      title: '*ALERT*'
      message: "Mains power is on."
  - service: switch.turn_off
    entity_id: switch.rack_fan
  - condition: state
    entity_id: binary_sensor.dark_outside
    state: 'off'
  - service: light.turn_off
    entity_id: light.all_lights

Don’t use toggle.
What state are all the lights in?
Toggle will just flip that

You need to check the status of each light following your power cycle
And then list the as ones that should be off and turn them off

Edit: simultaneous post to Tom
Note: he does not use toggle
I echo Tom’s thoughts on the title, not useful to anyone

Yes, you’re right but my poor english doesn’t allow me for better solution…

My HA and router are also on UPS but when the power is off in all the condominium the fiber goes off the same…

Thanks for the tips but can you share more infos about your two binary_sensors ?

Your English is very good. Just put key works like “lights off when power on” in the title.

binary_sensor.power_fail Is a 240 relay connected to mains power. When the power fails the contacts open. When the power is on the contact close. The contacts are sensed by a pi running Flyte’s gpio to mqtt server.

binary_sensor.dark_outside is a template binary sensor triggered at a certain Lux level. The Lux level comes from a BH1750 connected to an ESP8266 running ESPHome.


Hope you don’t mind a slight off topic here, but I wanted to just say I thoroughly enjoy your posts @tom_l - they are always so helpful, provide oodles of information and you are always happy to expand when people ask. I know there are many excellent members of this community, but your posts always stand out to me for some reason, and I’ve been meaning to say thank you for ages now - so thank you!


I don’t use it for the purpose you want (but I might start now after this thread… :slightly_smiling_face: ) but @pnbruckner has created a python script to save & restore the state of lights in HA.

You can find it here: https://github.com/pnbruckner/homeassistant-config/blob/0682cd8b69e92177942cbc3a9deb48e231eff68b/docs/light_store.md

then you can create an automation that runs periodically (every minute would be reasonable?) to continuously update the “light store” and then you can call the restore script to return the lights to their previous state on a HA restart event.

Like I said I may just implement this myself and see how it works. We don’t have many power outages to worry about but I can see it being handy if we did.

Not so fast, pardner!! :cowboy_hat_face:

That script writes the states to the State Machine, which is a volatile store. Meaning, when HA restarts, all that data goes away. (It doesn’t survive a HA restart.)

well, crap! I thought I was on to something there.


1 Like