Automation failing periodically

Hey all,
I have this automation that turns on my front door light and landscape lights at sunset, then off at sunrise. however, periodically, they won’t be turned off at sunrise. I can’t seem to figure out what causes it to fail as it only happens every once in a while (once every 10 days or so). Here’s my config:

alias: Lights on at sunset
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: 0
condition: []
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.tp_link_under_stair_outlet_landscape_lights
        - light.front_door_light
        - light.front_yard_outlet_front_yard_outlet_top
    data: {}
  - alias: Wait for 22:00
    wait_for_trigger:
      - platform: time
        at: "22:00:00"
  - alias: Turn off Peace Sign Box
    service: light.turn_off
    data: {}
    target:
      entity_id: light.front_yard_outlet_front_yard_outlet_top
  - wait_for_trigger:
      - platform: time
        at: sensor.sun_next_rising
    alias: Wait for Sunrise
  - service: light.turn_off
    target:
      entity_id:
        - light.tp_link_under_stair_outlet_landscape_lights
        - light.front_door_light
    data: {}
    alias: Devices to turn off at Sunrise
mode: single

I have an outlet (peace sing/front yard outlet top) that i have turn off at 22:15. is that causing the issue?

the trace timeline is:


Not sure why those lights/outlets are being turned on at 22:00 instead of the peace sign outlet being turned off…

Is there a better/more accepted way to turn things on and off at certain times? Thanks!

It’s inadvisable to create an automation that lingers in its action for any extended period of time.

Why not? Because whenever Home Assistant is restarted it terminates any automations that are “in progress”.

Your automation remains “in progress” from sunset until sunrise. Throughout those many hours, it is vulnerable to being terminated by a restart.

Whether or not a restart is the actual cause of the automation’s periodic failures, it’s simply not a good practice to remain “in progress” for so long.

The following automation triggers at sunrise, sunset, and 22:00. When it triggers, depending on the value of the variable command it will either turn on or off the lights listed in the lights variable.

alias: Lights on at sunset
description: ""
trigger:
 - platform: sun
   event: sunset
   offset: 0
   variables:
     command: 'on'
     lights:
       - light.tp_link_under_stair_outlet_landscape_lights
       - light.front_door_light
       - light.front_yard_outlet_front_yard_outlet_top
 - platform: time
   at: "22:00:00"
   variables:
     command: 'off'
     lights:
       - light.front_yard_outlet_front_yard_outlet_top
 - platform: time
   at: sensor.sun_next_rising
   variables:
     command: 'off'
     lights:
       - light.tp_link_under_stair_outlet_landscape_lights
       - light.front_door_light
condition: []
action:
 - service: "light.turn_{{ command }}"
   target:
     entity_id: "{{ lights }}"
mode: single
5 Likes

Awesome thanks!
Still learning the best way to do these things. Greatly appreciated!

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.