If Else Automation Help

I would like to configure my automation so that if its any time before 30 minutes prior to sunset, my lights turn on and off when triggered by motion or a door opening. If its withing the 30 minute window or after sunset, then only turn on and off the stairway lights, as the hallway light will already be on from another automation.

Not sure what I’m doing wrong but its not working as I would like, this automation currently turns both lights off after sunset.

- id: '1653420314161'
  alias: Hallway Motion Triggered Lights
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 77d39428abe145aebee509b8f7a9cc03
    entity_id: binary_sensor.third_reality_inc_3rms16bz_iaszone
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 67c71e4256c239940d1ce93d2dea9cba
    entity_id: binary_sensor.front_door
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 180b3575c189246a006588db1642c9b4
    entity_id: binary_sensor.house_garage_door
    domain: binary_sensor
  condition: []
  action:
  - if:
    - condition: sun
      before: sunset
      before_offset: -00:30:00
      after: sunset
    then:
    - service: homeassistant.turn_on
      data: {}
      target:
        entity_id:
        - light.hallway
        - switch.stairway_lights
    - delay:
        hours: 0
        minutes: 2
        seconds: 0
        milliseconds: 0
    - service: homeassistant.turn_off
      data: {}
      target:
        entity_id:
        - switch.stairway_lights
    else:
    - service: homeassistant.turn_on
      data: {}
      target:
        entity_id:
        - light.hallway
        - switch.stairway_lights
    - delay:
        hours: 0
        minutes: 2
        seconds: 0
        milliseconds: 0
    - service: homeassistant.turn_off
      data: {}
      target:
        entity_id:
        - light.hallway
        - switch.stairway_lights
  mode: single

The way you have set your Sun condition is the issue… I would split it up and move the offset to the “after” so that the current time needs to be within the overlap of the two conditions:

- id: '1653420314161'
  alias: Hallway Motion Triggered Lights
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 77d39428abe145aebee509b8f7a9cc03
    entity_id: binary_sensor.third_reality_inc_3rms16bz_iaszone
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 67c71e4256c239940d1ce93d2dea9cba
    entity_id: binary_sensor.front_door
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 180b3575c189246a006588db1642c9b4
    entity_id: binary_sensor.house_garage_door
    domain: binary_sensor
  condition: []
  action:
  - if:
      - condition: sun
        before: sunset
      - condition: sun      
        after: sunset
        after_offset: -00:30:00
    then:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id:
          - switch.stairway_lights
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id:
          - switch.stairway_lights
    else:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id:
          - light.hallway
          - switch.stairway_lights
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id:
          - light.hallway
          - switch.stairway_lights
  mode: single

Also, be aware that delays do not survive restart or reloading the automation, if you need your automation to be resilient through these processes, you will need to use a different method.

Wouldn’t that make it do the same action any time before sunset though? Don’t I want it after sunset? I was confused on this logic as well.

What do you mean about delays after a restart? Are you saying within that moment, or if the unit restarts then the delays will never occur again?

Made a few changes to try and have it make more sense to me…

- id: '1653420314161'
  alias: Hallway Motion Triggered Lights
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 77d39428abe145aebee509b8f7a9cc03
    entity_id: binary_sensor.third_reality_inc_3rms16bz_iaszone
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 67c71e4256c239940d1ce93d2dea9cba
    entity_id: binary_sensor.front_door
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 180b3575c189246a006588db1642c9b4
    entity_id: binary_sensor.house_garage_door
    domain: binary_sensor
  condition: []
  action:
  - choose:
    - conditions: []
      sequence:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id:
          - light.hallway
          - switch.stairway_lights
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id:
          - light.hallway
          - switch.stairway_lights
    - conditions:
      - condition: sun
        before: sunset
        after: sunset
        after_offset: -00:30:00
      sequence:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id: switch.stairway_lights
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id: switch.stairway_lights
  mode: single

No, it would only perform the action when both conditions are true.

I don’t know, do you? My understanding from you original post was you wanted a special behavior just for the 30 minute window before sunset. If that is not the case, please describe your goal clearly.

That’s not going to work. If you have an action sequence that doesn’t have conditions set it as a default option… don’t just leave the condition key as an empty set.

  - choose:
    - conditions:
      - condition: sun
        before: sunset
        after: sunset
        after_offset: -00:30:00
      sequence:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id: switch.stairway_lights
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id: switch.stairway_lights
    default:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id:
          - light.hallway
          - switch.stairway_lights
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id:
          - light.hallway
          - switch.stairway_lights
  mode: single

If you restart Home Assistant or reload the automation while it is in the middle of a delay, the actions that would have happened after that delay will not be executed. In your example, the lights will not be turned off.

I’ve got two lights, one is a hallway light that turns on 30 minutes before sunset and stays on until I go to bed.

If its 30 minutes prior to sunset or after sunset, when I go down the stairs (motion sensor) or open one of the two doors, I want the stairway light to turn on, wait two minutes, then turn off, but keep the hallway light on.

If its not on (during the day, 1 hour before sunset, etc) then I want both the hallway and stairway light to turn on, delay for 2 minutes, then turn back off.

What should happen between midnight and “during the day”? How do you define “during the day”?

What should happen if motion is sensed or one of the doors open again during the delay period?

During the day is any time 30 minutes after sunrise through until 30 minutes before sunset.

I don’t really care if motion is detected or a door is opened during the delay period as I only needed the lights to be on for that 2 minute period…

- id: '1653420314161'
  alias: Hallway Motion Triggered Lights
  description: ''
  trigger:
    - type: motion
      platform: device
      device_id: 77d39428abe145aebee509b8f7a9cc03
      entity_id: binary_sensor.third_reality_inc_3rms16bz_iaszone
      domain: binary_sensor
    - type: opened
      platform: device
      device_id: 67c71e4256c239940d1ce93d2dea9cba
      entity_id: binary_sensor.front_door
      domain: binary_sensor
    - type: opened
      platform: device
      device_id: 180b3575c189246a006588db1642c9b4
      entity_id: binary_sensor.house_garage_door
      domain: binary_sensor
  condition: []
  action:
    - choose:
      - conditions:
        - alias: "When it's not 'during the day' " 
          condition: sun
          before: sunrise
          before_offset: +00:30:00
          after: sunset
          after_offset: -00:30:00
        sequence:
          - service: homeassistant.turn_on
            data: {}
            target:
              entity_id:
                - switch.stairway_lights
          - delay:
              hours: 0
              minutes: 2
              seconds: 0
              milliseconds: 0
          - service: homeassistant.turn_off
            data: {}
            target:
              entity_id: switch.stairway_lights
      default:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id:
            - light.hallway
            - switch.stairway_lights
      - delay:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id:
            - light.hallway
            - switch.stairway_lights
  mode: single

I’ll give that a shot tonight and let you know. I had most of it correct, except for the sunrise / sunset part, that variable always messes me up…

Wanted to report back that it seems to be working. Thank you.

I think I had most of it right…but that darn sunset timings…

1 Like