Automation fails after reboot

Hi everyone, I’m running HA in Hyper-V for some months now and not sure if this is normal behavior.

I have a simple automation that turns the light on at night and then off again in the morning based on the sun’s position and dusk.

I noticed that sometimes (not always) when I update HA or reboot the PC that runs HA, at night, after the lights turned on already, they will not turn off again in the morning.

The automation works perfectly fine for months, unless I reboot the system or the power goes out for a few minutes and then comes on again. Otherwise, it works fine. It seems that when I reboot the system for updates, or upgrade HA or another reason, for a reboot or update, it then somehow skips the automation that turns the light off again. For this reason, now I tried to avoid updating HA at night or rebooting the system unless it’s on the day and the automation to turn the light on has not run yet. The strange thing is that both automations are separated, so they are not supposed to depend on each other for the state.

I cannot actually replicate this exactly, since its not always true but I noticed this only happens on reboots as it HA loses it state somehow. I noticed the lights on at the morning way past its time and had to turn them off manually at least 4 or 5 times in the past month, and every time it was because I either updated HA or rebooted the system.

If I reboot the system or update HA at a day, it works fine, lights always turn on and then off again and this only seems to happen when I am rebooting HA or the VM running it.

I’m thinking of creating a second automation turn off at the morning automation after one hour or 2 as fail check or maybe another way to re-check if the automation was executed?

When this happens, I do see in the history that the automation does not run. This seems a bit redundant to add a second automation again just in case, but I’m curious why this happens only when HA is restarted. Maybe someone with more experience can tell me how to debug this or what I can do to make sure automation does run even if I reboot the system.

my thinking is If you have a delay turn off your automation then when HA reboots that automation has been stopped so it won’t turn off

can you show us the automation? in yaml

so we can see what happen

1 Like

Can you please post the automation configuration.

Sure, here is the code:

alias: Lights Front ON
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: 0
condition: []
action:
  - type: turn_on
    device_id: 3254818bbaa0dd45f762e67eb32f1b18
    entity_id: light.0xf0d1b8be2408085f
    domain: light
  - type: turn_on
    device_id: e7df4de8a8e29f6518ef58b50f6f18d0
    entity_id: light.0xf0d1b8be2408056d
    domain: light
  - type: turn_on
    device_id: 35afa53f0e5aa03796cef625fe8ee6b8
    entity_id: light.0xf0d1b8be24080573
    domain: light
  - type: turn_on
    device_id: 69f37a2e187b6eb8ed50d44e9791cd59
    entity_id: light.0xf0d1b8be24080700
    domain: light
mode: single

And this is what turns them off:

alias: Lights Front OFF
description: ""
trigger:
  - platform: time
    at: sensor.sun_next_dawn
condition: []
action:
  - type: turn_off
    device_id: 3254818bbaa0dd45f762e67eb32f1b18
    entity_id: light.0xf0d1b8be2408085f
    domain: light
  - type: turn_off
    device_id: e7df4de8a8e29f6518ef58b50f6f18d0
    entity_id: light.0xf0d1b8be2408056d
    domain: light
  - type: turn_off
    device_id: 35afa53f0e5aa03796cef625fe8ee6b8
    entity_id: light.0xf0d1b8be24080573
    domain: light
  - type: turn_off
    device_id: 69f37a2e187b6eb8ed50d44e9791cd59
    entity_id: light.0xf0d1b8be24080700
    domain: light
mode: single

Here is my sunrise / sunset automation, it has never failed. I’ve made a group with all the lights that are to be included in this automation.

alias: LIGHT - outdoor lights
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: 0
    id: sunrise
  - platform: sun
    event: sunset
    offset: 0
    id: sunset
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sunrise
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.outdoor_lights
      - conditions:
          - condition: trigger
            id: sunset
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              entity_id: light.outdoor_lights
mode: single

It never fails either unless I reboot the machine. And sometimes not even then. This is why it is so hard to replicate.

Example: If I upgrade HA or reboot the VM or the machine that has the VM which of course also reboots the Hyper-V service, and then later the lights turn on at night it works fine.

But if I do this after the lights have already turned on then the next morning, they don’t turn off again. And I cannot replicate this precisely, either. In the logs there is nothing, the automation to turn the lights off again basically does not trigger at all, as if it does not even exist.

And since both are separated automation, they don’t rely on each other. I can only assume that using the sun_next_dawn is the problem or the bug. Something in that sensor probably loses it calculation state when the system is rebooted.

That is how I had it initially, but was not working very well for me, this is why I changed the lights off to sun_next_dawn which is what works better than just sunset or sunrise.

I want the lights to be turned off at dawn, not when the sun is already up.

Can’t you use the offset for that?