Automation - the sunset/sunrise event needs some fixiing

Hi,
I’ve this automation that is working (partially) and it puts an outdoor LED light to ON whenever the garage side door (reed switch) is opened.

- id: '1656863867735'
  alias: 'Automation: Outside Light Timer - Sideyard'
  description: This automation turns ON the outside light when door is OPENED & turns
    OFF the light when door is CLOSED after few seconds
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.window_b10
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: input_boolean.outside_lights_automation
      state: 'on'
    - condition: sun
      after: sunset
  action:
  - type: turn_on
    device_id: 2ccdc1687b78569a1d259801e333a128
    entity_id: switch.basic02_relay
    domain: switch
  - service: timer.start
    data:
      duration: '10'
    target:
      entity_id: timer.timeroutsidelight_sideyard
  - wait_template: '{{ is_state(''binary_sensor.window_b10'', ''off'') and  is_state(''timer.timeroutsidelight_sideyard'',
      ''idle'') }}'
    continue_on_timeout: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - type: turn_off
    device_id: 2ccdc1687b78569a1d259801e333a128
    entity_id: switch.basic02_relay
    domain: switch
  mode: restart

Yesterday I came back late (~ 1 AM) and the light did not come ON. I am guessing that it is likely that the date changed over at midnight and the test “after sunset” likely failed.

Is this correct and would adding “before sunrise” condition fix it? Asking here because I do not want to stay out late just for testing automation :grinning:

Thanks.

“after sunset” is the time between sunset and midnight.
“before sunrise” is the time between midnight and sunrise.

If you need to execute it at some time between sunset and sunrise, then you will need to add an OR condition with 2 separate conditions: after sunset OR before sunrise

1 Like

Just FYI, there is supposed to be code in place to override that condition and use tomorrow’s sunrise. However it does not presently work. It may be fixed in future:

Use below horizon instead as that is what most people are looking for, although initially it confuses most the after sunset and before sunrise works exactly as it supposed, so you need to use one or the other or both depending on your needs.

Above or below horizon has no relevance to midnight and therefore achieves what most people are looking for without any confusion of if, or, and

condition: state
entity_id: sun.sun
state: below_horizon
2 Likes

For more fancy stuff, you can also look at the sun2 integration but not sure if you need this :slight_smile:
pnbruckner/ha-sun2: Home Assistant Sun2 Sensor (github.com)

How about you replace “after sunset” with sun.sun state below_horizon?
That way your automation would not care midnight or next day or otherwise.

1 Like

@vingerha 2 posts up :wink:

sun2 is pretty cool and fancy - thanks for sharing!

Thanks everyone for their pointers, adding “state: below_horizon” fixed the problem for me!

2 Likes