Automation to check if non UPS lights are supposed to be on after an outage

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.

Does this exist?

That sounds very inefficient.

You only have to check when the power is resumed, which your UPS should be able to tell you. So trigger an automation off that.

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.

Is your UPS integrated into HA using the NUT integration and add-on?

What model?

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.

Do you use Z2M or ZHA?

I’m using the native ZHA.

Correct me if I’m understanding this wrong.

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?

No but it would be very easy to set up a triggered template sensor (for each device) to record this.

template:
  - triggers:
      - trigger: state
        entity_id: switch.foobar
        to:
          - unavailable
    sensor:
      - name: Foobar previous state
        state: "{{ trigger.from_state.state }}"

However I’m not sure you need this. All my smart plugs expose a power on behaviour option. You can set it to restore its previous state.

image

This is a screen shot of Zigbee2mqtt, not sure how it is set using ZHA as I don’t use it.

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.

Thank you for your time and explanations. I’ll have to dig into this further on my own. It’s been a few years since I looked at HA.

1 Like

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.

Thank You very much.

1 Like

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.

For the second time, that is inefficient and not required if you have

There is no need to poll things when you can automation on state events.

I see that I’m going to have to reacquaint myself with HA internals after years of not looking at it.