My HA is UPS backed up but most of my lights are not. I’m looking for a way to turn lights back on on a resumption of electrical power for all things that are supposed to be on according to the time of day rules.
I’m thinking a script that runs every minute checking the current state of a device against what it should be according to the rules.
I should have mentioned that my UPS does not support a mechanism that HA can use to do as you suggested. Without that capability, the only thing I can think of is something that runs periodically.
Consider an intermediate way to detect a power outage. For example, a ping sensor that targets a device that doesn’t have UPS backup?
Your post says:
I’m thinking a script that runs every minute checking the current state of a device against what it should be according to the rules.
But is that underestimating the complexity? What format are the “rules” in? Do you use any smart switches, such that the expected state of a given light won’t necessarily be captured by rules? I point this out not to criticize, but to note that even if you are able to detect power outages, you should give some good thought to whether the automation is going to be able to restore the lights to a sensical state. It’s a very difficult problem to solve right–it’s probably harder than putting together a reasonably reliable kludge to detect power outages.
My “UPS” is a solar array, inverter and batteries that run the important loads on a completely separate system from the remainder that runs on grid power. There is no interconnect and there can’t be any due to the local laws (Caribbean Island).
Although I’m an EE and a professional software developer, I was hoping I wouldn’t have to write any code or cobble together my own solution as I figured this situation might have been solved previously.
The box running HA has plenty of resources to waste time checking every minute, 5 minutes, etc.
Connect any wifi, zigbee or zwave plug to the mains. Now you have a mains power monitor (and a switch for something you can control).
There could be a blueprint (I don’t use them) but it will likely need a way of detecting if mains power is present or not. And as I said this can be accomplished very easily.
I haven’t investigated the HA API set to determine what state information and capabilities are innately available. As you say, this may be a non trivial job. I was hoping someone else had already tackled the job.
All the ‘dead’ circuits on grid power have Zigbee Smart Plug devices that come back up in the off position when power is restored. I fail to see how either a Smart Plug lacking power or in the off state can be used as a detection/interrogation device. If I’m missing something, please explain.
The devices will go unavailable when they lose power.
triggers:
- trigger: state # monitor for return from mains power loss
entity_id: switch.foobar
from:
- unavailable
If your zigbee network is unreliable and the devices occasionally drop off the network then you could create a template sensor that monitors for all plugs going unavailable.
When power drops on a Smart Plug, HA notices the device is not available as an internal state. That can trigger an event to do something as your code seems to indicate since I’m not fluent on HA coding.
While the device is dead, there is nothing to do.
When the device comes back as ‘available’, an event can then do something which would be to see if it’s supposed to be on or off as the rules suggest.
As part of HA’s housekeeping of state information, is there an indicator of what the state (on or off) was before it went unavailable so that on/off indicator could be used to reset the circuit assuming nothing else as far as rules has changed?
Alternatively, is there a system call to ask HA what the state should be this instant and use that for the reset?
Perhaps your plugs go into an Unknown or Unavailable state when they’re not seeing power for a certain amount of time? It’s not perfect, but it could be a hint.
Also, I don’t know what your system looks like, but consider the following in case it applies: if you’re using Zigbee2MQTT, you might be able to message a mains-powered device and expect an immediate response. Lack of immediate response could be a good sign of an ongoing power outage.
Thanks for your suggestion. I’ll dig through the Zigbee info available for my devices to see if I can get them to return to their state before they lost power.
I guess I should also upgrade to the MQTT methodology.
There are other considerations in play–whether you go with ZHA or Z2M is a question that probably cuts deeper than merely the ability I alluded to in my post. Just wanted to throw the idea out there, though, since it seemed on point.
Maybe I’m thinking too simplistic here, but I’ll throw it out anyway:
Once you have a reliable (and close to realtime) indicator if your power is actually on or off (and the ‘restore’ settings for your Zigbee devices aren’t working), I’d suggest running an automation e.g. every 5 minutes, that creates/updates a scene which includes all the non-UPS lights, plugs, etc. as long as the power is on.
It stops when the power is off (that’s where the near realtime part comes in) and sets the devices back to the last saved scene when the power comes back on.