Light automation with motion sensors, sun elevation and manual switch control

Any suggestions how to stop the automation switching off the switch if switch has been turned on manually? My previous incarnation had the sun elevation condition at the start of the automation however this resulted in the switch staying on in the morning if the elevation threshold passed. Any thoughts appreciated.

I should also mention the ‘or’ condition is there as planning to add additional sensors to the automation.

  trigger:
  - platform: state
    from: 'off'
    to: 'on'
    entity_id: binary_sensor.alarm_zone_01_pir_first_floor_hall
  - platform: state
    entity_id: binary_sensor.alarm_zone_01_pir_first_floor_hall
    from: 'on'
    to: 'off'
    for: 00:10:00
  condition: []
  action:
  - choose:
    - conditions:
      - condition: or
        conditions:
        - condition: state
          entity_id: binary_sensor.alarm_zone_01_pir_first_floor_hall
          state: 'on'
      - condition: numeric_state
        entity_id: sun.sun
        below: '6.0'
        attribute: elevation
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.device_02_relay
    default:
    - service: switch.turn_off
      target:
        entity_id: switch.device_02_relay
 

That depends on how the switch might otherwise be turned on (other than manually)

If it is done thru another automation then you could have that other automation also turn on an input_boolean to denote it was turned on by an automation instead of by hand.

then in the above automation use that input boolean being on as a condition to let the automation turn off the light.

but you will also need another automation to turn off the input boolean if the light is turned off manually or thu an automation to set it up for the next time.

Thanks @finity, turns out I was overthinking my use case. For anyone who is interested see below for my final solution. This accounts for the following:

  • Automation triggers when motion detected by PiR sensor.
  • If automation triggers and if sun elevation below 6.0 then action option 1 is selected and the switch is turned on for 10 minutes.
  • If the automation triggers and the sun elevation is not below 6.0 then the default action option is selected and the switch turns off after 10 minutes.

This covers the following use cases:

  • Light switches on/off when motion detected and it’s dark.
  • Light switches off if turned on manually.
  trigger:
  - platform: state
    from: 'off'
    to: 'on'
    entity_id: binary_sensor.alarm_zone_01_pir_first_floor_hall
  condition: []
  action:
  - choose:
    - conditions:
      - condition: or
        conditions:
        - condition: state
          entity_id: binary_sensor.alarm_zone_01_pir_first_floor_hall
          state: 'on'
      - condition: numeric_state
        entity_id: sun.sun
        below: '6.0'
        attribute: elevation
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.device_02_relay
      - delay:
          hours: 0
          minutes: 10
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        target:
          entity_id: switch.device_02_relay
    default:
    - delay:
        hours: 0
        minutes: 10
        seconds: 0
        milliseconds: 0
    - service: switch.turn_off
      target:
        entity_id: switch.device_02_relay
  mode: restart
1 Like