Puzzled by sunset/sunrise lights on automation

I have an automation that turns the patio light on/off at sunset and sunrise. I originally used the Sun’s “sunrise” and “sunset” as triggered, and use “sun.sun” state with “above_horizon” and “below_horizon” as conditions to determine if I should turn on or off the light. It did not work. I found out that when the automation was triggered by “sunset”, the state of the “sun.sun” was still “above_horizon” and therefore, it will not turn the light on (and vice versa with the sunrise trigger).

Because of that, I changed to use of the state of the sun (“sun.sun”) as both the trigger and the condition to turn the light on/off. It works perfectly. However, something unexpected happened at around 3 am last night. I turned the light off before I went to bed. But the automation was triggered again by the sun’s state and the light was turned back on (because the state of the sun was “below_horizon”). Why did the sun’s state got changed/triggered in the middle of the night? If the value of the sun’s state hasn’t changed, why it triggered the automation? Please help me to understand what’s going on. I have attached the relevant log book entries at the end of this post. Thank you!

Here is my trigger snipplet:

  - platform: state
    entity_id: sun.sun

Here are my action conditions:

action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: above_horizon
        sequence:
          - service: light.turn_off
            data: {}
            target:
              device_id: 9b00a82f17d6b015292982f8a52bc54c
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: below_horizon
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 10
            target:
              device_id: 9b00a82f17d6b015292982f8a52bc54c

When you leave a State trigger open-ended it will trigger both on state changes and attribute changes. It’s also possible that your HA instance restarted and the change from “unavailable” to “below_horizon” triggered the automation. You can avoid these by defining to: and from: variables.

- platform: state
  entity_id: sun.sun
  to: 
    - above_horizon
    - below_horizon
  not_from: unavailable
1 Like

Thank you for the reply! I certainly did not think of that. I will add the not_from to the trigger and try it out tonight.

However, I did not restart my docker instance nor reboot the machine. The logbook did not register any entry for the sun’s state change, and the history shows that sun.sun is below_horizon during that period of time (pls see the attached image).

If the HA process itself restarted, I have no idea but I just added an “UpTime” integration sensor to help me find out if that is the cause.

CleanShot 2022-10-05 at 18.38.13

[edit]
I just read the State trigger documentation again. It said the trigger with entity_id only will be executed not just due to the state change but also attribute changes. To combat that situation, I need to use one of the to, from, not_from, or not_to in the trigger definition. If the sun’s attribute is the reason why it was triggered, the not_from should prevent that, as well as a restart of HA I guess. There are a few tests I am going to run and see which will solve the issue:

  1. use the not_from: unavailable tonight
  2. use the to: with the null value (according to the documentation, that will prevent the trigger caused by attribute change
  3. expand my state trigger to use to and from explicitly to implement the sunset/sunrise triggers

I will report back if any of these work.

[edit - on next day]

Yup that works. Thank you so much helping me!