How do I recover all automations after a power outage?

Last night we had a brief (about a minute long) power outage of the whole house. When the power returned, my Home Assistant server recovered without issue and all the WiFi, BLE, Matter and Zigbee devices eventually reconnected to HA.

But, all the automations that had run earlier in the evening turning on various lights and devices did not restore at all. They all defaulted to their off state, which I guess, is to be expected.

That made me think, is there any way to save the status of an automation so that if there is a power outage, Home Assistant can check which automations were active just before the power outage? And thus when HA has recovered and settled down, each automation can be activated back to the state it was just before the power outage?

I am guessing that the simple answer is “yes, of course”. But I have no idea where to even start. Suggestions would be very welcome.

Look through you automations and manually trigger based upon season, month, day, hour, etc. that should get you caught back up somewhat. Other automations should trigger based on other sensors should react as programmed.
Make sure the HA instance has the correct date/time from a NTP server also.

What do you mean by active automations?
You mean the where on? Or running?
Do you have long delays in your automations?

That’s what I ended up doing, but I am looking for a more sophisticated solution. Which probably involves another automation?

For example, I have an automation that turns the floor-lamp and the table-lamp on at sunset. After the power outage, both lamps stayed off. I manually turned them on again.

But what I want to do is have (another automation?) that is triggered by HA re-booting and then, after a suitable delay, refers to a table that lists the devices that were on (just before the power outage) as a result of earlier automations, and turns them back on again. All automagically, because I may not be home at the time.

In other words, I want the state of everything to be exactly as it was before the power outage.

I don’t understand what you mean here?

I mean automations that have run earler (before the power outage) and, for example, the lamp should be still on.

Yes, the automation had run and completed earlier and the lamp was on at the time of the power outage, and should come back on when the power is restored.

No, no delays in my automations.

Some smart bulbs has a restore option, switches does probably not.
But in short it seems you want a scene that is created just before power out and then restored again.
You could (I believe) create a scene every minute and on start up load this scene.
Issues that could be is that the power out is longer and everyone is asleep and then the scene loads and turns on the lights.

Best solution: get a small UPS. Just big enough to run your HA server and network equipment for your average power outage time + a bit.

2 Likes

The UPS and HA will not turn on the lights by itself.
Something more is needed.
All switches and bulbs will go off and even if HA stays alive then it will just reconnect to the lights when they get back and HA will report off.
Or am I missing something?

Well for a start you can detect the power outage. How you deal with it is up to you.

My lights have power restore so I don’t have to worry about it.

Your automation that turns on something could also turn on a helper to indicate it should be on.

Your off automation also turns off the helper.

A new automation that triggers on HA start check helpers and run the automations that have the corresponding helper turned on.

This should work but might be alot of work if you have many automations.

Thanks for all the comments and suggestions … :slightly_smiling_face:

It looks like it could be a lot of work for little benefit and since I have only experienced one short power outage in the last 4.5 years, then it is probably not warranted.

However, given all the smarts and clever code that is already in Home Assistant, I was surprised that there wasn’t already something that restores Home Assistant to exactly the same state after an outage.

Now I understand why - it is not that simple … and the benefit is probably not that great.

Before or after the power outage? Automations trigger on changes, not states. If the outage was after sunset, the automation won’t trigger until the next sunset; the next change. You could trigger the lamp on every minute with the condition that the sun state is “sunset”. But this may not be the result you expect.

To accomplish what you ask for, you would have to write an automation to save the state of everything periodically, (triggered by time), and on startup reset all devices to the pre-fail state. You will quickly find that this would not be a trivial task. Of course, if power fails while the automation is updating the state memory, the result would be questionable.

đź’ˇ Sensor Light - Motion Sensor - Door Sensor - Sun Elevation - LUX Value - Scenes - Time - Light Control - Device Tracker - Night Lights Has built-in support for dealing with Home Assistant restarts. That should cover power outages as well. You could look at the blueprint to see how it does it.

I’ll admit I haven’t tested this much as my install is on a UPS!

Yes, you are quite correct - it is not a trivial task. There is also the complication that when the power resumes, HA would have to check if the current time means that the lights should still be on. For example, at the time that the power resumes (which could be hours later), an automation to turn the lights off may have already passed.

Thanks for the pointer - I will study that blueprint.

Your house needs to function with HA completely broken. Think about that need before you worry about spending 30 seconds a few times a year to turn on lights after a power outage.

Once the use becomes large, HA ideally be on a small UPS, more for power fluctuations than a full long outage.

A lot of suggestions here, but nobody is pointing out the best practice:

Don’t create long-running or “stateful”automations

By that, I mean your goal should be that all your automations execute and complete in fractions of a second. If you use long delays, or wait for triggers, or anything else that keeps your automation active for a long time, then you are building a very fragile home automation experience.

A reboot or a power outage shouldn’t affect your automations because they shouldn’t be hanging out in partial states of completion.

Yes, my house does function with HA completely broken - everything has manual control available.

I don’t think that a small UPS would help the scenario that I have described. HA recovered fine, but when all the WiFi controlled switches, sockets and bulbs came back on, they resumed their default OFF state, and everything that was on before the power outage, was now off.

I agree totally. None of my automations are long-running or “stateful”automations.

Maybe I didn’t explain myself very well.

All the automations that I am referring to ran earlier in the evening - most were triggered by the time of sunset. So all the relevant lamps were on around the house. And the automations were done.

Then we had a momentary power failure and when the power was restored, HA re-booted and all the devices re-connected, but all the lamps stayed off. Which is to be expected. So I manually turned them on again.

But that made me wonder if I could make HA realise that there were lamps on before the power failure, that should be turned back on once the power was restored. Hence my query.

Two options:

If you have hardware that supports it, you can set the power-on state. Most good-quality Zigbee and zwave light switches and power plugs let you choose this. (Probably WiFi too but I’ve never used them). You can select ON, OFF, or Previous State. This works great for short power outages, but I’ve been bitten by this when power is out for a long time, then you get woken up when power is restored in the middle of the night and your lights are at 100%.

Your other option is to set “home assistant start” as a trigger for your automations, and let the logic guide what behavior you want.

Usually my automations have a number of triggers, sometimes using trigger ID’s, and then the actions are in a “choose” statement with various options.