Automation - Turn on lights at sunset but not before 18:00

Hi all,

I would like my lights to turn on at sunset but not before 18:00. Will this YAML mean my lights come on at sunset as long as it is after 18:00?

My concern is that the trigger will only work if the above statement is true and not if sunset is before 18:00.

Appreciate any help anyone can provide, thanks!

alias: Soffit Lights (ON)
description: Soffit Lights turn on after Sunset
trigger:
  - platform: sun
    event: sunset
condition:
  - condition: and
    conditions:
      - condition: time
        after: "18:00:00"
  - condition: or
    conditions:
      - condition: time
        after: "18:00:00"
action:
  - service: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.soffit_lights_switch_0
mode: single

Assuming you want them to come on at 18:00 when sunset is earlier, you can add a trigger for that. I don’t understand what you are going for with your conditions… you only need a single time condition:

alias: Soffit Lights (ON)
description: Soffit Lights turn on after Sunset
trigger:
  - platform: sun
    event: sunset
    id: sun
  - platform: time
    at: "18:00:00"
    id: time
condition:
  - or:
      - and:
          - condition: trigger
            id: time
          - condition: sun
            after: sunset
      - and:
          - condition: trigger
            id: sun
          - condition: time
            after: "18:00:00"  
action:
  - service: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.soffit_lights_switch_0
mode: single

EDIT: Added conditions to address later sunrise issue.

2 Likes

Thank you @Didgeridrew, this seems to sort it! Appreciate your time, sir.

That will trigger at 6pm every day, though…
Probably need just the sunset trigger then a choose statement, if time is before 6pm, wait until 6pm for action.

Also with the trigger at 6pm, can the condition ever be after 6pm? I don’t know the resolution there.

1 Like

I’m not familiar enough with home assistant or YAML just yet to confirm this unfortunately but will update tomorrow at 18:01 to confirm or deny :slight_smile:

Well it will trigger at 1800 and it will trigger at sunset, because those are the triggers.
The 1800 trigger might pass the condition, probably will but If I was writing that it would be a trigger at 18:01. (Or the condition be after 17:59)

Yep I missed that…

Yes the time condition will work fine… that’s how all my automations like this are set up. I used to offset them, but it’s not necessary.

@Milamber1

As SG pointed out, I left out the conditional logic to address the time trigger when sunset is later. I have addressed it in my post above.

1 Like

Appreciate the help guys, I have marked @Didgeridrew response as solution