Store the power state of *all* lights and Z2M devices without individual input helpers and restore those states?

I’m struggling to find the right terms to search for but I feel like this should be possible…

Onus: We had a 45 second power cut last night. My server, Zigbee coordinator, Hue hub, and main router are all on a UPS so weren’t affected, but once the power came back on we were all woken up by the Hue connected Innr lights coming on. Additionally when we got up I had to go around the house figuring out what state our various smart plugs should be in as they’d all defaulted to off.

I’ve built a simple automation that toggles a “UPS Power” helper on and off via webhook events from my NAS, and that works fine. What I’d really like to do with that is:

When power goes out, run an automation that stores the last-known power state of all bulbs and any devices connected via Z2M - without me having to make helpers in advance for each and every device - and then it would wait for the power to come back on before setting everything back to its previous state.

I know HA has support for doing for each loops against arrays, so I’d assume it can make and store these arrays but that’s the point at which I come unstuck. I’ve done some automations using simple variables and have some “stored variables” using helpers, but that’s about the height of my knowledge for these things.

I hope that all makes sense! Is what I’m asking within the realm of HA’s capabilities? And if so could I be pointed in the right direction of things to look into?

Thanks everyone!

I have no proof, but my pure guess would be some artifact of the Hue bridge getting incorrect data from the cloud instead of using a regular zigbee coordinator where HA has control.

What Z2M coordinator are you running?

Hue connected Innr lights coming on

Why are the Innr lights connect to the Hue Hub vs the Z2M?

At a higher level why run the Hue Hub and a Z2M coordinator?

The hue bulbs have an option to return to last state when power up after a power failure.
It can be set in the hue app.

This setting will be applied before the bulb is even connected to the hue hub or other coordinator.

In HA you can probably set up an automation to run with intervals that store all the states you want in a helper.
The way is it stored is up to you. It is possible to design a storage format that can compress multiple states into a single data object, like a string.
An alternative could be using scenes, but large scenes have a tendency to miss some settings on restore, due to communication saturation. A solution may be to store several smaller scenes, like one per integration or one per room. Smaller scenes can be restored with a little delay in between to avoid the issue with communication saturation.

Some communication method needs to be controlled tightly whatever method you use though, especially old time RF/IR.

Echoing what Wally said.

If you’re using Z2MQTT, all the Innr bulbs all seem to have a power_on_behaviour entity with the possibility to choose previous setting. | Zigbee2MQTT

Some zigbee smart plugs also have this option, though you might need to test it out since not all devices actually support it.

Worst case scenario is you’ll only have to worry about finding a solution for your plugs - your bulbs will handle themselves once you enable power_on_behaviour

I actually have a mix of some bulbs connected to Hue and some directly to Z2M via a Sonoff stick. It all depends on the use case, eg. Hue lets me setup a colour cycle effect in the kids’ rooms that they use as a nightlight. Also my wife isn’t at all interested in my “hobby” and prefers the simplicity of Hue. So all of the Sonoff connected bulbs are entirely automated ones where there’s no need to control them manually.

The power-state restore is proving a mixed bag. I spent a while yesterday moving the Innr bulbs over to Z2M so that I could set this (can’t be done in Hue itself with non-Hue bulbs) but when I moved them back to Hue they’d reset to defaults. Similarly, while using ZHA I could set the state for my Woolley smart plugs, the option isn’t available in Z2M (on the flip side Z2M has power monitoring for these plugs while ZHA doesn’t).

So really I’m trying to design a method of not needing to rely on settings that may or may not be supported across the different systems. HA can see the on/off state of all of these devices at any time so being able to store those states when the power goes out and re-apply them when it comes back, while not perfect, would be better than nothing.

I could do it fairly easily with a helper for each device but that’ll get very messy very quickly! I was hoping there would be a way to have HA do it itself. Figure out which devices have on/off states in the first place and store those states for later use.

Power cuts are rare enough where I live for it not to be a huge issue, but when they do happen it’s invariably in the middle of the night!

I hope that puts it into some better context. I have a feeling it’s possible but that I’m going to need to learn a lot more before I revisit the idea.

1 Like

I think that might be too late, by the time you fire your automation to save the states, they might be already unavailable.