Help Building Light Automation

Hi Friends, I need some help building a light automation.

I have two automations today: one that turns on the external lights at 5pm and another that turns them off at 2am. BUT, if there is a power failure for example at 8pm, when everything turns back on, the lights won’t turn on, since the automation was to turn them on at 5pm.

How do I make an automation not to only turn the lights on at 5pm but to check if time is >= 5pm and < 2am, make sure the light is on?

This is what I have today:

- id: '03'
  alias: TurnOnLights
  trigger:
  - at: '17:00:00'
    platform: time
  action:
  - entity_id: light.iluminacao_externa
    service: light.turn_on
- id: '04'
  alias: TurnOffLights
  trigger:
  - at: 02:00:00
    platform: time
  action:
  - entity_id: light.iluminacao_externa
    service: homeassistant.turn_off

Thanks in advance!

There’s a bunch of ways to do it. If your home assistant server is not on a UPS then add a home assistant started trigger and a time window condition.

If your home assistant server is on a ups you’re going to need a power fail sensor. Trigger on return of power and use the same time window condition.

The HA Server is on a UPS, but the lights aren’t, so, even with a trigger on HA start, I would need something that keeps checking the lights and keeps trying to turn them on, so, when power is restored and lights are connected, then they receive a command to turn on…

All you need is a power fail sensor. A DC plugpack with a matching relay for voltage free contacts is how I do it.

You might even be able to trigger on any one of the lights going from unknown or unavailable to off.

You could just run a time trigger every x minutes, and conditions, between 5pm and 2am, and lights are off. Run action turn on.

Time pattern triggers will work but they are never the optimal solution.

Neither are lights that don’t turn back on with event triggers :slight_smile:

I don’t use them frequently, but sometimes they work the best, and a time trigger every few minutes is hardly going to raise the CPU and certainly easier than building voltage sensors.

Does the UPS have a USB or network management port you can use? I monitor status and voltage through mine, and use that to trigger network shutdowns during an extended failure. When UPS goes from OnBattery->Online for x seconds, if lights are off and time is between x and y, turn on lights.

The other thought it whether the lights have a way of setting them to return to their former state. I know some zigbee/zwave lights support sending commands for them to turn on/off or previous state after a power failure.

Which was why I offered two solutions that don’t time pattern triggers.