Closed Loop Counter to Lights Remembering State and Power Outage


My lights have this awesome feature where they remember their previous state between power cycles so that they don’t turn on and wake you up when sleeping during a power outage recovery.

I’d like to keep using that feature, but I also have to keep using basic light switches. If a light is set to off, then the switch is physically turned off, I’d like the light to come back on when the switch is turned back on. However, if the light is set to off and we lose power, I’d like the light to stay off when the power comes back on.

I’m part of the way there, but this automation isn’t complete. How can I edit it to do 3 more things?

  1. How can I prevent this automation from running when the house recovers from a power outage as opposed to a person turning the physical switch back on? I was thinking of using the HA Trigger but I’m not sure how to use it in this automation or if there’s a better way.

  2. How can I replace the for: in the trigger or delay: in the action (I’ve tried both) with something more closed loop. Without the for/delay, when HA sends the light.turn_on command after seeing the unavailable-to-off state trigger, the light must not actually be ready so it doesn’t respond. The for/delay fixes this, but is there a better way? My lights use ZigBee and since I haven’t moved on from my Philips Hue Bridge V2 yet, my diagnostics are limited.

  3. I’m relying heavily on the light changing state to unavailable which takes a very long time after the physical switch is turned off. I can see why the state change would be designed that way, but is there some other trigger better suited to what I’m trying to do?

Your help would be greatly appreciated :slight_smile:

alias: Light 3 Test
description: ""
  - platform: state
      - light.bedroom_3_light_e
    from: unavailable
    to: "off"
      hours: 0
      minutes: 0
      seconds: 1
condition: []
# - delay:
#     hours: 0
#     minutes: 0
#     seconds: 1
#     milliseconds: 0
  - service: light.turn_on
    data: {}
      entity_id: light.bedroom_3_light_e
mode: single

Once this is working, I’m going to turn it into a blueprint and apply it to all lights in my house.

Do you have a UPS? With the APC UPS integration you can create a template condition like this that is false until 60 seconds after the UPS switches back to mains.

I would put the condition after the delay statement in case the state change of the bulb coming back online may come before the state change of the UPS signaling the outage.

{{(now()|as_timestamp - states.sensor.ups_total_time_on_battery.last_changed|as_timestamp) > 60}}

1 Like

Thanks @eyager

I have a very old Powershield CompuGuard 650VA which isn’t listed as one of the NUT server supported devices. How much does that matter?

Either way, I’m going to move my HA server into a different room soon which means losing the UPS since I’ll still need that for my PC. I might have to try find a non-UPS solution if that’s possible.

Your delay comment gave me an idea. I’m going to remove the delay from the config in my initial post and add a for: seconds: 1 to the trigger. I think this might perform better, but I think my question still stands since this is still an open loop solution.

Is there a way to check when HA booted last? Then I could do something similar to what eyager suggested but without having a UPS.

Does anybody have any ideas? Or a better approach?