Lights on only after sunset and before sunrise

I’m trying to create an automation that turns on a light when the garage door opens but only after sunset and before sunrise.

Here’s the config, I think I don’t understand how the “condition” works. If the condition is TRUE then the automation won’t run? In this case, if we are before sunrise and after sunset, then condition is true and the automation will not run? I’ve tried it with before: sunset and after: sunrise and that doesn’t seem to work correctly either.

id: '162069225'
alias: Turn on back door light when garage door opens
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 7d2b95a331ebd37eef36f118ab0b
    entity_id: binary_sensor.garage_door_sensor_garage_door
    domain: binary_sensor
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - type: turn_on
    device_id: 37445c3137b211c56d0f87f85a0
    entity_id: switch.back_door_light
    domain: switch
mode: single

I have :

condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
3 Likes

Thanks, I added this condition - now waiting for sunset. :wink:

If the condition its true it will run. If false wont.

1 Like

Before sunrise is from midnight to sunrise.
After sunset is from sunset to midnight.

These conditions are AND logic when used together.

It is impossible to be both before sunrise AND after sunset at the same time.

Definitely use Tim’s suggestion above as it is simpler, but just for educational purposes, if you wanted before sunrise OR after sunset you would do this:

condition:
  - condition: or
    conditions:
      - condition: sun
        before: sunrise
      - condition: sun
        after: sunset
1 Like

To continue the educational drift, I made the same kind of mistake as a beginner with the before/after logic and this kind of issue pops up almost monthly. It is actually documented very well.

1 Like

Thanks for that link!

Thanks this worked great.

1 Like

I’ve submitted a MR to fix the ‘before sunrise or after sunset’ issue.

It needs reviewers to move it along…

1 Like