Most of my switches and lamps are WiFi connected and are programmed with ESPHome. Is there something in ESPHome that I can use to save the current state (each time it changes) and revert to that state when the device re-boots at power on?
Template text with restore_value on.
It can take a few seconds before the value is actually saved, so it can still fail on you
I was looking for the same issue. I think you might mean that you want to restore the previous state of your āentities,ā rather than your āautomations.ā At any rate, thatās what I want to do.
Stephen Mannās suggestion is the way Iām going to go, personally, except that Iām not going to use a constantly repeating time-based trigger. Instead, Iām going to use the UPS attached to my Synology NAS to trigger a power loss event. That UPS is currently set to run for an hour and perform a shutdown of my router, NAS and HA hub if the power is not restored during that hour. I can use that to trigger an autmation to save the current state of all lights and switches, and when the power is restored, that can be another trigger to run an automation to restore previous state, with a condition that if itās after midnight or if my phone says Iām in sleeping mode (Samsung modes), then it can instead force everything off regardless of previous state.
For anyone wondering why this is desired⦠Itās because I did not enjoy being awakened with lights on after the power had gone out in the night and came back on at 3 am. I have a mix of old and new devices, and some that donāt have power restore management features built in. This is a solution to make sure things stay off when Iām sleeping, but if they were on and in use when the power goes off, then theyāll restore automatically as soon as the power comes back on.
My house does have switches and things to manually operate, but that will not prevent me from being in a bad mood if a bright lamp suddenly shines on my face at 3am, or if I come out of my bedroom in the morning to find out every light in the house is on because there had been an outage in the night. Hence the need for this.
I like that approach, please keep us updated.
No, I did mean that I wanted to restore the state of my Automations.
For example, from the incident that prompted this query:
I have several automations that āfireā at sunset to turn on various lights around the house and other automations that turn them off at 23:00 . The other night, after sunset and after the automations had turned on the lights, we had a momentary power outage. But when the power returned (which was before 23:00), all the lights that had been turned on by automations, remained off.
What I want to be able to do is, when the power returns, to have all the automations checked and if the lights should be on, then turn them on again.
In other words, if the power is restored after sunset but before 23:00, then turn on the lights that would be on if the power hadnāt failed.
However, if the power is restored after 23:00, then leave the lights off.
Restoring the previous state of the entities could mean that lights come on again while we are asleep, so I donāt want that.
I hope that all makes sense?
The state of an automation is either on or off, which means āenabledā and ādisabledā respectively. So in HA language, ārestoring the state of your automations after restartā means āmaking sure the automations that were enabled prior to restart are enabled after restartā. And, of course, they already are.
What you are describing is a desire to trigger your automations upon restart. And that is simple to do, just add a homeassistant āstartā trigger to every automation that you want to run at startup.
However, you need to then ensure the logic in your automation will yield the results you want⦠For example:
If you have a simple automation that triggers at 10pm and has an action to turn on a light, if you just add a homeassisant start trigger, then the light will turn on every time you restart HA. So what you need to do is add your triggers as conditions. So you add a condition that it must be after 10pm, and that way regardless of whether the automation is triggered by the 10pm trigger or by HA startup, the light will only turn on if it is after 10pm.
Making robust automations that are insensitive to HA restarts can be a challenging process, but the capability is already there. It just takes the right knowledge to set them up appropriately.
No, as @mekaneck explained above, your automations are not disabled or off.
Most lights have a power_on_behaviour or a restore_mode. You need to tell your devices how to behave when power is cut and then restored.
If that is not an option for you, search the docs on how to create āscenes on the flyā. Have an automation that runs that action every x minutes for each of your affected devices. Add another automation with a homeassistant_start trigger which applies that scene.