Restore state of all entities after power lost

Hello, I have some devices that are not able to recover their status after a loss of power, such as sonoff zbmini, gledopto lights and others.

After a long time of searching and combining different codes, I managed to make a snapshot of all the entities through a scene which must be restored after starting home assistant.

While home assistant is running it works perfectly, but when restarting it has a problem.

Apparently home assistant deletes that snapshot when restarting, so it doesn’t restore.

I put here the code of the two automations in case someone comes up with a solution.

alias: Save state of switches and lights every minute
description: ""
  - platform: time_pattern
    minutes: "1"
condition: []
  - service: scene.create
      scene_id: estado_luces
      snapshot_entities: |
        {{ expand(states.light) | map(attribute='entity_id') | list }}
  - service: scene.create
      scene_id: estado_enchufes
      snapshot_entities: |
        {{ expand(states.switch) | map(attribute='entity_id') | list }}
mode: single
alias: Restore state swtiches and lights after starting HA
description: ""
  - platform: homeassistant
    event: start
condition: []
  - service: scene.turn_on
      entity_id: scene.estado_enchufes
    metadata: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: scene.turn_on
      entity_id: scene.estado_luces
    metadata: {}
mode: single

Thanks in advance.

1 Like

A dynamic scene like that won’t work as its cleared on restart as you noticed. that is be design.

I see, maybe is possible to do it through mqtt, but idk.

That points to a potential problem with the integration(s) that support those lights (because I have not experienced this problem with the integrations supporting the lights I have).

Which integration(s) is/are involved?


MQTT Scene simply allows you to define a scene based on an existing MQTT topic (that already controls multiple devices). You can’t directly specify the individual membership of an MQTT Scene.

zigbee2mqtt, wled, Philips Hue…

I have Philips Hue bulbs and their power-on behavior can be specified using the Philips Hue app. The Philips Hue integration correctly reports their state after the Hue Bridge has re-established communications with the rebooted bulb. If you’re not seeing any of this behavior then something isn’t nominal.

I don’t use zigbee2mqtt or wled so I can’t comment on them.

Philips hue supports more devices than just its light bulbs.
For example, as is my case, osram plugs and GLEDOPTO lights.
GLEDOPTO lights normally always turn on after a loss of light and plugs like sonoff zbmini stay off if they were on.
The only device that I have that allows you to restore your settings are Philips Hue bulbs, but as you will understand, I have more zigbee devices and unfortunately they do not have this function, that is the reason for this post.

Then that’s the extent of your ability to set a device’s power-on behavior (if it’s a non-Hue device communicating via the Hue Bridge).

It’s possible that zigbee2mqtt might offer more flexibility but I don’t know for sure (if it does then you would have to unpair the device from the Hue Bridge and pair it with whatever ZigBee coordinator you’re using with zigbee2mqtt).

No, the idea is to do it in HA and not in all other integrations.

I use Philips Hue for for apps like Hue Essentials so moving to zigbee2mqtt is not feasible
In addition, I also use other integrations such as WLED, broadlink, etc.

I have seen that it is possible to do what I want using fake switches, but I see it as too tedious a job to have to go device by device.

It would be nice if just home assistant keep the scene snapshot settings.

Seems like you have imposed very specific constraints on how you want to resolve this issue. Good luck.

1 Like

Isn’t there also the risk of a race condition between the two scenes? Can the time_pattern trigger before the restoration automation runs (or completes)? For example, since startup often takes more than a minute? Sorry: I am not sure of there is some sequencing logic I am unaware of.

I have been trying to get my plugs to return to on after power failure for some time without success. It’s terrible if I am not home because I have smart outlets/plugs on everything, including the router/modem. These are used to troubleshoot and cycle power to these devices from HA if it detects there is no internet. But if a power failure happens, these plugs remain off and I can’t remote in to trigger them from HA since there is no internet as those plugs didn’t return to their state either, etc etc. Thankfully, power rarely ever fails here but still, it happened while I was away from home once and I have been determined to remedy that ever since. No luck thus far.

I think the outlets/plugs themselves need to be from a specific ecosystem (TP-Link, KASA, etc) that offers it within their own app thus it is likely integrated into the switch/plug itself. I guess none of mine offer this option since they are all Zigbee or Matter. They are simply a plug connected to HA relying on HA for any features beyond turning them on or off from their individual manual switches.

The ideal method is to buy hardware which supports controlling the power-up behavior. How the device communicates (WiFi, Zigbee, zwave, thread, etc.) doesn’t have any impact on this. This way, whether HA is running or not, the device always powers on in the state that you want it to be.

If you need HA to change the device state upon power-up, it is quite easy if you know that you always want the device to be in a specific state on power-up (as opposed to “whatever state it was when it shut off”):

Create an automation that has a trigger of HA startup (using the UI with 2024.1 or newer it is Add Trigger → Other Triggers → Home Assistant, and choose “start”). In the actions, you can first add a delay if you want to ensure all integrations are booted up. For the next action you can choose Switch → Turn on and then choose all your switch entities.

If you really need to capture the state of certain devices and restore that state on startup, I’d probably opt for a trigger-based template sensor that stores a dictionary of entities and their associated states. It could be triggered on a time interval or by state changes if you only have a few devices you are monitoring. It would also have to stop monitoring state changes for a few minutes during startup. Then you could add a startup automation to iterate through that sensor’s dictionary and change all those states.

Hello Jonatan, ( @jonatanpradasn )
In my configuration I have also several devices that do not remain their status after restart of Home Assistant; ex my Aqara Shutter always start open after restart HA even when it was closed before the restart.

I wonder, have you found a solution for your siutuation?
I did follow the Video Tutorial you referred to, but without success with my Shutter motor.