Sunrise Sunset automation

Is the “offset: ‘+00:15:00’” referring to 15 minutes after sunrise/sunset?

Thanks for all this info. It, a long with the current automation helper in HA has helped me automate some settings.

I love HA, but wouldn’t an “on at sunset, off at sunrise” process be one of the most used automation out there? Why the “this or that” condition process. It would be much more straightforward to have:
trigger: sunset,
action turn on
trigger: sunrise
action turn off

I know it doesn’t work that way now but maybe it should.

Not real helpful putting in a picture instead of the code to help others out.

2 Likes

For those that want something to copy and paste.

alias: Outdoor lights ON/OFF based on sun
description: ""
trigger:
  - platform: sun
    event: sunrise
    id: sunrise
    offset: "-00:10:00"
  - platform: sun
    event: sunset
    id: sunset
    offset: "+00:18:00"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sunset
        sequence:
          - service: light.turn_on
            data:
              kelvin: 5000
              brightness_pct: 100
            target:
              entity_id: light.outdoor_lights
      - conditions:
          - condition: trigger
            id: sunrise
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.outdoor_lights
mode: single

Shorter alternative:

alias: Outdoor lights ON/OFF based on sun
description: ""
trigger:
  - platform: sun
    event: sunrise
    offset: "-00:10:00"
  - platform: sun
    event: sunset
    offset: "+00:18:00"
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.outdoor_lights
    data:
      kelvin: 5000
      brightness_pct: "{{ iif(trigger.event == 'sunset', 100, 0) }}"
mode: single
1 Like

I want to do the same thing with one change. I only want the outside lights to be on only when the temperature is below 60 degrees outside and the sun is down. The issue is it may not be 60 until well after sundown depending on the season. How can I combine make that happen?

Include triggers for both sundown and the temperature dropping below your desired value (numeric state trigger) and mirror those triggers in the conditions block.

Sunrise/Sunset condition
Numeric State condition

Sorry if I’m bumping the wrong topic, but I have a related question.

I would like to automate my outdoor lighting at sunrise. I’d like the lights to turn on at 06.30am and turn off 30 minutes after sunrise but with the condition that the lights are on for at least 15 minutes. How can i code this?

In summertime sunrise will be before 6.30am, will the automation stay off in that scenario, or would the code go haywire?

Thanks in advance!

ps. Bonus points if the suggested code also includes a different start time for weekdays (6.30am) and weekend days (7.00am) :slight_smile:

So if they cannot be on for at least 15 minutes then they should not be turned on at 06:30?

Correct! Then they should stay off.

I ran my question through ChatGPT and got the following bit of code for the minimum 15 minutes part:

triggers:
  - at: "06:30:00"
    trigger: time
conditions:
  - condition: template
    value_template: |
      {% set sunrise = state_attr('sun.sun', 'next_rising') %} {% if sunrise %}
        {% set sunrise_offset = (as_datetime(sunrise) + timedelta(minutes=30)) %}
        {{ (sunrise_offset - now()).total_seconds() >= 900 }}
      {% else %}
        false
      {% endif %}

That doesn’t prevent the lights from being turned on when your time trigger is after sunrise.

triggers:
  - alias: Weekdays On
    trigger: time
    id: "on"
    at: "06:30:00"
    variables:
      day_of_week: "{{ now().isoweekday() < 6 }}"
  - alias: Weekends On
    trigger: time
    id: "on"
    at: "07:30:00"
    variables:
      day_of_week: "{{ now().isoweekday() >= 6 }}"
  - id: "off"
    trigger: sun
    event: sunrise
    offset: "00:30:00"
conditions:
  - "{{ day_of_week }}"
  - or:
      - condition: trigger
        id: 'off'
      - condition: template
        value_template: |
          {% set sunrise_offset = state_attr('sun.sun', 'next_rising')|as_datetime + timedelta(minutes=30) %}
          {{ sunrise_offset.date() == trigger.now.date() 
          and trigger.now + timedelta(minutes=15) < sunrise_offset }}
actions:
  - action: light.turn_{{trigger.id}}
    target:
      - entity_id: light.outdoor

Thank you. Can you point out where “my” code failed in relation to yours?

Whether or not that code will ever fail for you depends on whether the Time triggers you use are set to a time that is later than sunrise ever occurs in your location.

The next_rising attribute updates at sunrise. It switches immediately from representing today’s sunrise time to tomorrow’s sunrise time. What you posted doesn’t control for that date shift.

So, let’s say today’s (2025-09-14) sunrise is at 06:25 and tomorrow’s is 06:26. At 6:30, when the time trigger fires, the breakdown of the variable values in the template you posted is as follows:

sunrise = "2025-09-15 06:26"
now =  "2025-09-14 06:30"
sunrise_offset= "2025-09-15 06:56"

So the final expression of the template would pass because the difference, in seconds, between sunrise_offset and now is 87600, which is more than 900.