YAML Butler
← All guides

Sunset and sunrise automations that follow the seasons

Choose a sun event, an offset, or a measured brightness signal for dependable outdoor lighting.

A fixed evening clock time gets inconvenient as daylight changes. A sunset trigger follows the season automatically, while sunrise can provide a natural switch-off point. Before writing YAML, decide whether you mean an astronomical event or simply the moment a particular space becomes dark. Those are related, but buildings, weather, and trees can make them feel quite different.

Verify your home location

Home Assistant calculates sun events from its configured location and time zone. Check those settings before adjusting offsets to compensate for a schedule that seems wrong. A misplaced home location can produce plausible but inaccurate times. Look at the sun integration entities to compare the next expected events with your local sunrise and sunset. If the installation moved or was restored from another home, location is especially worth reviewing.

Start with one clear sun event

The example turns a porch light on twenty minutes before sunset. A negative offset runs before the event; a positive offset runs after it. Keep the offset as a quoted duration string. Use a separate sunrise automation to turn the same light off, or combine both events with trigger IDs and choose branches. Separate rules are often easier to understand when the on and off requirements differ.

A starting-point automation

Every entity ID below is an example. Replace it with your actual entity ID and review all settings before use.

alias: Porch on before sunset
description: Turn on the porch light twenty minutes before local sunset.
triggers:
  - trigger: sun
    event: sunset
    offset: "-00:20:00"
conditions: []
actions:
  - action: light.turn_on
    target:
      entity_id: light.porch
mode: single

Distinguish triggers from conditions

A sunset trigger starts a run at that event. A sun condition checks whether the current time meets a daylight rule when some other trigger fires. If you want a motion light only after dark, motion should usually be the trigger and darkness a condition. Adding a sunset trigger to that rule would also turn the light on even if nobody moved, unless the actions deliberately handle that separate case.

Consider elevation and actual brightness

An offset is easy to explain, but the length of twilight changes through the year and with latitude. A sun elevation threshold can express a particular stage of twilight more closely. A lux sensor can reflect local clouds and shade. Whichever signal you choose, inspect its history first. Add some separation between on and off thresholds if noisy readings cause rapid switching, and avoid exposing the sensor to the very light it controls.

Handle missed events deliberately

A sunset event that happens while Home Assistant is offline is not replayed automatically after startup. If the porch should always be on during a certain night interval, add a recovery rule that checks current conditions when Home Assistant starts. Decide whether that rule should respect a manual override. A scheduled event and a maintained state are different requirements, so say which one you intend before adding more triggers.

Test behavior across the night

Check the action manually on one lamp, then wait for a real sun event to confirm trigger behavior. Review the trace and local time shown there. Include late-night guests, seasonal changes, and a manual switch-off in your expectations. Avoid turning off lighting that provides required access without another reliable way to illuminate the route. Keep the final entity list narrow and write a description explaining the chosen offset.

Give on and off rules clear ownership

List any other rules or device schedules that control the same porch light. A timer in a smart plug or a separate motion automation may turn it off soon after sunset, making a correct sun trigger look broken. Decide which rule has priority and whether motion should increase brightness temporarily or simply switch the light on. Write that relationship in the descriptions. When the intended behavior is documented, you can inspect traces from both rules instead of treating the most recent change as the likely cause.

Further reference: official Home Assistant documentation.

Common questions

Why does sunset seem too late for my room?

Local shade can make a space dark earlier. Try a negative offset, sun elevation, or a lux sensor.

Will a missed sunset run after a restart?

No. Add a startup recovery rule with current night conditions if you need the state restored.

Put your own idea into YAML.

Describe your devices, timing, and exceptions. YAML Butler will give you a starting point to review.

Generate your automation ↗