Sun condition not working correctly

For some reason the below automation is still being processed after sunrise and I can’t see what is wrong to cause that. The condition should be stopping it. This is one of my few Automations created using the GUI editor (I just switched to YAML to post it here) so perhaps I’m missing some indentation or something?

code
alias: Back Yard Festoon Lights Auto
description: Turn the back yard festoon lights on and off at night based on motion
trigger:
  - platform: state
    entity_id:
      - binary_sensor.paradox_z6_alfresco_pir
    to: "on"
    id: "on"
  - platform: state
    entity_id:
      - binary_sensor.paradox_z6_alfresco_pir
    to: "off"
    for:
      hours: 0
      minutes: 20
      seconds: 0
    id: "off"
condition:
  - condition: sun
    after: sunset
    after_offset: "00:30:00"
    before: sunrise
    before_offset: "01:30:00"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: "22:00:00"
                    weekday:
                      - sun
                      - mon
                      - tue
                      - wed
                      - thu
                      - fri
                      - sat
                sequence:
                  - service: light.turn_on
                    data: {}
                    target:
                      entity_id: light.back_yard_festoon_lights_1
                  - delay:
                      hours: 0
                      minutes: 2
                      seconds: 0
                      milliseconds: 0
                  - service: light.turn_off
                    data: {}
                    target:
                      entity_id: light.back_yard_festoon_lights_1
              - conditions:
                  - condition: time
                    before: "22:00:00"
                    weekday:
                      - sat
                      - fri
                      - thu
                      - wed
                      - tue
                      - mon
                      - sun
                sequence:
                  - service: light.turn_on
                    data: {}
                    target:
                      entity_id: light.back_yard_festoon_lights_1
                  - delay:
                      hours: 0
                      minutes: 15
                      seconds: 0
                      milliseconds: 0
                  - service: light.turn_off
                    data: {}
                    target:
                      entity_id: light.back_yard_festoon_lights_1
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.back_yard_festoon_lights_1
trace:
  stored_traces: 40
mode: single

Can anyone see what I’ve missed?

Hmm. That looks correct.

https://www.home-assistant.io/docs/scripts/conditions/#sunsetsunrise-condition

Taking the 6am sunrise and 6pm sunset from the example above, your condition is true from 6:30pm to midnight and true from midnight to 7:30am.

Are your time zone, location, and less importantly height above sea level set all correctly?

To have it stop prior to sunrise your before-offset should be negative.

1 Like

All those are correct. I have another automation (created directly in YAML) which has a similar sun condition and it works perfectly fine.

So this morning (when the trace is from), the automation turned the lights on well after sunrise which was a couple of hours earlier… What I do notice in the trace text is “confirm sun after sunset offset 00:30:00 before sunrise” where the offset is not described… but again, 07:39 was well after sunrise.

What time was sunrise?

Drew’s point above actually makes your time range true from 6:30pm to midnight and true from midnight to 7:30am for a 6pm/6am sunset/rise.

I’ll give that a try… to me that seems counter-intuitive, I mean the ‘before’ should preclude any need to make the value negative…

about 06:17.

I’ve changed the offset to a negative value and will try that. I’ll check what I have in my other YAML based automations.

EDIT: yep, my other automations use a negative value for the ‘before’ offset.

Yeah it’s what Drew said. 06:17 + 01:30 = true until 7:47. You triggered at 07:39.

Yeah, the verbal logic of the condition has always seemed weird… The before is more like shorthand for “from midnight until the target” and the offset moves the target around the reference point of sunrise or sunset. It would probably be helpful if the graphic included at least one negative offset…