Sun Sensor Next Sunset and Below Horizon are not in sync?

I’ve had an automation running just fine for months that turns on some decorative lights 15 minutes before sunset, and turns them off at 11pm.

Additionally since we have frequent power outages where I live, the automation also handles the case where home assistant is started anyime between 15 minutes before sunset and 11pm.

Today my lights turned on as per usual and then I happened to restart home assistant to install 2025.12.3 a short while before sunset. To my suprise my lights turned off, and I went digging to see why, and it seems that I hit a small window where sensor.sun_next_setting had rolled over to tomorrow’s sunset, but is_state('sun.sun', 'below_horizon') was false!

Here is the complete automation for anyone curious enough to take a look:

- id: f86d1f80-2a76-4c15-bc29-26706793ffd5
  alias: Decorative Light Automation
  mode: restart
  description: ""
  trigger:
    - platform: sun
      event: sunset
      offset: -00:15:00
      id: Sunset
    - platform: time
      at: "23:00:00"
      id: Bedtime
    - platform: homeassistant
      event: start
      id: Start
  variables:
    sunset: "{{ states('sensor.sun_next_setting') | as_datetime | as_local }}"
    sunset_start: "{{ (sunset | as_datetime | as_local) - timedelta(minutes=15) }}"
    sun_setting: "{{ (sunset_start | as_datetime | as_local).time() <= now().time() and now().time() <= (sunset | as_datetime | as_local).time() }}"
    # using sun.above_horizon etc causes problems because its update time seems to be slightly offset from the sun_next_setting time
    sun_set: "{{ now() > today_at('12:00') and now() < today_at('23:00') and is_state('sun.sun', 'below_horizon') }}"
    turn_on_lights: "{{ sun_setting or sun_set }}"
    turn_off_lights: "{{ not turn_on_lights }}"
  action:
    - choose:
      - conditions:
          - condition: trigger
            id:
              - Sunset
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              entity_id: light.decorative_lights
          - service: switch.turn_on
            target:
              entity_id: switch.christmas_tree_lights
      - conditions:
          - condition: trigger
            id:
              - Bedtime
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.decorative_lights
          - service: switch.turn_off
            target:
              entity_id: switch.christmas_tree_lights
      - conditions:
          - condition: trigger
            id:
              - Start
          - condition: template
            value_template: >-
              {{ turn_off_lights }}
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.decorative_lights
          - service: switch.turn_off
            target:
              entity_id: switch.christmas_tree_lights
      - conditions:
          - condition: trigger
            id:
              - Start
          - condition: template
            value_template: >-
              {{ turn_on_lights }}
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              entity_id: light.decorative_lights
          - service: switch.turn_on
            target:
              entity_id: switch.christmas_tree_lights

Here’s the trace of the last execution of this automation which was at December 12, 2025 at 4:13:28 PM:

this:
  entity_id: automation.decorative_light_automation
  state: 'on'
  attributes:
    id: f86d1f80-2a76-4c15-bc29-26706793ffd5
    last_triggered: '2025-12-12T20:56:49.263911+00:00'
    mode: restart
    current: 0
    friendly_name: Night Light Automation
  last_changed: '2025-12-12T21:13:15.917814+00:00'
  last_reported: '2025-12-12T21:13:15.917814+00:00'
  last_updated: '2025-12-12T21:13:15.917814+00:00'
  context:
    id: 01KCA6JBCDM9SGEA98G02DYN0X
    parent_id: null
    user_id: null
trigger:
  id: Start
  idx: '2'
  alias: null
  platform: homeassistant
  event: start
  description: Home Assistant starting
sunset: '2025-12-13 16:11:57-05:00'
sunset_start: '2025-12-13 15:56:57-05:00'
sun_setting: false
sun_set: false
turn_on_lights: false
turn_off_lights: true

The logs for the sun.next_setting entity are as follows:

Sun Next setting changed to December 12, 2025 at 4:11 PM
4:11:43 PM - Yesterday
Sun Next setting changed to December 13, 2025 at 4:11 PM
4:12:21 PM - Today

Meanwhile for the sun.sun entity the logs are as follows:

set
4:14:21 PM - 4 hours ago

By looking at the Supervisor logs I can see the system was restarting between roughly 4:11pm and 4:13pm.

If anyone has any short suggestions on how to cleanup this automation, I’m all ears!

All I want to do is turn lights on at 15 minutes before sunset, and turn them off at 11pm, with the caveat that it should work if home assistant happens to not be running at either of those trigger times.

Bonus points if I could make those times configurable somehow so that they aren’t duplicated in the trigger section & the variables section.

It seems like a bug in the integration that the sun.sun entity changes state at a slightly different time from what is set by sensor.sun_next_setting.

But your automation shouldn’t be using two different methods to check if the sun has set. Your conditions should match your triggers. Why not simplify to the following:

alias: Decorative Light Automation
mode: restart
description: ""
triggers:
  - event: sunset
    offset: "-00:15:00"
    trigger: sun
  - at: "23:00:00"
    trigger: time
  - event: start
    trigger: homeassistant
actions:
  - choose:
      - conditions:
          - condition: sun
            after: sunset
            after_offset: "-00:15:00"
          - condition: time
            before: "23:00:00"
        sequence:
          - data:
              brightness_pct: 100
            target:
              entity_id: light.decorative_lights
            action: light.turn_on
          - target:
              entity_id: switch.christmas_tree_lights
            action: switch.turn_on
    default:
      - data: {}
        target:
          entity_id: light.decorative_lights
        action: light.turn_off
      - target:
          entity_id: switch.christmas_tree_lights
        action: switch.turn_off
        data: {}