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?

Years later and this is still happening. But I figured out why. The trigger relies on:
triggers:

  • at: sensor.sun_next_dawn
    trigger: time

I checked the logs and this is not triggered by Home Assistant if it was rebooted that day (update, etc.)

See the attached screenshot. See the missing day? Home-assistant was rebooted (OS updated) that day.

I could not find any GitHub issue open about this. Home Assistant runs inside a Hyper-V machine using Home Assistant OS.

So you are saying that sensor.sun_next_dawn is not being restored after a restart?

Where does the sensor come from?

I changed my night automation to use sunset, and it never failed again. I guess I will also change this dawn automation to use sunrise with an offset as well.

For anyone else reading this, don’t confuse dusk and dawn as being part of the sun automation. They are not.

This was my mistake, it seems, assuming being new to home assistant they are part of the same sun triggers.

Not sure why Home Assistant is offering this if they are so prompt to fail. I can only guess dusk and dawn have some internal time sync which is reset when rebooted.

But why even have these options if they cannot be used in Home Assistant. Any automation that uses this will have issues when they decide not to run. I can only guess the many frustrations of people that have no idea why sometimes it runs and sometimes it does not. Because dusk and dawn seem very different from sunset or sunrise.

While the sunset or sunrise seem very reliable and this is why I assume nobody else had the issues I had because they used those with an offset.

Exactly. This is true for dusk and dawn. Both of them fail. I have tested this now for a very long time (more than a year). And they are extremely unreliable.

Sunset and sunrise always run, regardless if Home assistant was rebooted, updated, etc.

Where does the sensor come from?

Is it a template sensor?

Only triggered template sensors are restored after a restart.

They are built into Home Assistant. See picture:

it seems they are.
Must be the new way integrations create sensors instead of attributes.
But the attributes of sun.sun is still there.
That seems like a bug to me.

I have this issue for 2 years now. The first thing I did (was automating lights on and off) and this had nothing but issues because I picked those settings.

The second I changed the automations to sunrise, sunset they work perfectly, every time, every day.

I suspect the others, from the screenshot attached, must suffer from the same bug. I did not try then, but my guess is that Next midnight, Next noon, Next rising, have exactly the same bugs as well. If you have a power outage on the day, or decide to reboot the system, or update home assistant, they don’t run anymore until the next 24 hours.

I have asked, searched on GitHub, nobody else reported anything similar and the reason is nobody uses the other sunset settings it seems. They all use sunset and sunrise with an offset. Those just work. The others don’t.

I think all the bugged downs are the following:

  • Sun Next Dawn
  • Sun Next Rising
  • Sun Next Noon
  • Sun Next Setting
  • Sun Next Dusk
  • Sun Next Midnight