Why doesn't sunset condition work

I have an automation that turns on the garage light when movement is detected on the Unifi doorbell, but I want it to do so only after sundown (and before sunset). So when it’s dark enough. It works perfectly without the condition, but as soon as I add the sun condition. It does nothing at all.
What am I doing wrong?
Yaml code below:

alias: Garage light
description: 
trigger:
  - platform: state
    entity_id:
      - sensor.garagedoor_detected_object
condition:
  - condition: sun
    after: sunset
    before: sunrise
action:
  - type: turn_on
    device_id: 25ad086816793c7c2c7aa881278d4f6e
    entity_id: light.garagedoor
    domain: light
    brightness_pct: 100
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 25ad086816793c7c2c7aa881278d4f6e
    entity_id: light.garagedoor
    domain: light
mode: single

Try remove in condition
before: sunrise

At least I have that setup and it works. It turns on light after sunset.

I think it is because the condition cannot span midnight.

Better to maybe use below_horizon or use or

condition:
    condition: or 
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
condition: state
  - entity_id: sun.sun 
    state: below_horizon

This is what I use

alias: MOTION - Garage lighting ON
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage_camera_smart_motion_human
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - type: turn_on
    device_id: 45066cfc3dbab5073405f0922e4acec2
    entity_id: switch.garage_lights
    domain: switch
mode: single
alias: MOTION - Garage lighting OFF
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage_camera_smart_motion_human
    to: "off"
    from: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - type: turn_off
    device_id: 45066cfc3dbab5073405f0922e4acec2
    entity_id: switch.garage_lights
    domain: switch
mode: single

On some of my other automations I double up on trigger and conditions to ensure it only runs at sunset or sunrise. If I changed it manually

alias: LIGHT - Front Yard-ON_Sunset
description: ""
trigger:
  - platform: state
    entity_id:
      - sun.sun
    to: below_horizon
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: state
    entity_id: light.front_lights_zha_group_0x0003
    state: "off"
  - condition: state
    entity_id: light.front_porch_lamp_level_on_off
    state: "off"
action:
  - service: light.turn_on
    data:
      brightness_pct: 15
    target:
      entity_id:
        - light.front_lights_zha_group_0x0003
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: light.turn_on
    data:
      brightness_pct: 15
    target:
      entity_id:
        - light.front_lights_zha_group_0x0003
mode: single
alias: LIGHT - Front Yard-Off_Sunrise
description: ""
trigger:
  - platform: state
    entity_id:
      - sun.sun
    to: above_horizon
condition:
  - condition: state
    entity_id: sun.sun
    state: above_horizon
  - condition: state
    entity_id: light.front_lights_zha_group_0x0003
    state: "on"
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.front_lights_zha_group_0x0003
        - light.front_porch_lamp_level_on_off
    data: {}
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id:
        - light.front_lights_zha_group_0x0003
        - light.front_porch_lamp_level_on_off
    data: {}
mode: single

You are thinking in rolling days.

In your mind the time tables look like this.

--> Your trigger spot after sunset and before sunrise
Day 1 - 06:00 - Sunrise
Day 1 - 18:00 - Sunset
--> Your trigger spot after sunset and before sunrise
Day 2 - 06:01 - Sunrise
Day 2 - 17:58 - Sunset
--> Your trigger spot after sunset and before sunrise

HA thinks in just one day

00:00 Start of day
06:00 Sunrise
18:00 Sunset
23:59 End of day

Your after sunset and before sunrise would then be here

00:00 Start of day
--> Your trigger spot before sunrise
06:00 Sunrise
18:00 Sunset
--> Your trigger spot after sunset
23:59 End of day

Since the condition merger is “and” then both will have to be true, but that state is impossible to reach.
Changing the condition merger to “or” will make it possible to be true and that is what you want.

use below_horizon instead

I’ve had the same issue as long as I’ve been on HA and I’ve just found ways to work around it. I understand what other people in here are saying and I’m definitely going to switch to above/below horizon. Thanks for posting this, I learned something.

2 Likes

this is the portion of my automation that controls my lights when it senses my phone on the wifi when I arrive home

 - condition: state
    entity_id: sun.sun
    state: below_horizon
  - service: switch.turn_on
    target:
      entity_id:
      - switch.outside_north
      - switch.outside_west
      - switch.laundry_west
  - delay: 00:10:00
  - service: switch.turn_off
    target:
      entity_id:
      - switch.outside_north
      - switch.outside_west
  mode: single

Thanks all. It works now :slight_smile:
Indeed HA doesn’t like both before sunrise and after sunset.

It does.
You just need to use a condition: or