Light/switch controlled by motion sensor and with manual override

tom_l shared a great example how to automate light based on a motion sensor. I’m sharing another version that supports human override. This automation behaves like this:

  1. When motion sensor is triggered off -> on, turn on the light only if it’s off.
    • If the light is already on, it’ll stay on regardless of whether motion is sensed or not.

  2. Wait for the motion sensor to change back to off for more than 1 minute, and then turn off the light
  3. If the light is manually turned off while the motion is still going on, the automation stops immediately.
##========== Auto backyard light ==========##
- id: auto_backyard_light
  alias: "Auto backyard light"
  trigger:
    - platform: state
      entity_id: binary_sensor.backyard_motion # CHANGE THIS
      from: "off"
      to: "on"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: light.backyard_light # CHANGE THIS
        state: "off"
      # Add any other condition you would like, e.g., is_day_time
  action:
    - service: light.turn_on
      target:
        entity_id: light.backyard_light # CHANGE THIS
    # Wait for the motion to stop for 1 minute
    # or the light is manually turned off
    - alias: "wait"
      wait_for_trigger:
        - platform: state
          entity_id: light.backyard_light # CHANGE THIS
          from: "on"
          to: "off"
        - platform: state
          entity_id: binary_sensor.backyard_motion # CHANGE THIS
          from: "on"
          to: "off"
          for:
            seconds: 10
    # Stop execution if the light is turned off manually
    - condition: template
      value_template: "{{ wait.trigger.idx == '1' }}"
    - service: light.turn_off
      target:
        entity_id: light.backyard_light # CHANGE THIS

Hi, is this the complete config or do I need to add extra?
I have the above copied into YAMAL (with my own updated entities) but light doesnt turn on upon motion.
Thanks

Yes it’s the complete automation. You may need to trim the condition section to add/remove more conditions for this automation to run/not run. Also don’t forget to change the entity ids.

1 Like

That’s an awesome idea!

If the light is manually turned off while the motion is still going on, the automation stops immediately.

Do you a way to make that condition like

If the light is manually changed (brightness, scene, turned off), the automation stops.

I have a list that is switched on with minimum brightness on motion. Often, that is enough. Sometimes, I require more brightness and use the wall switch to change. This also means, I do not want it to be switched off anymore.

Do you see a way to achieve that?

You can change more conditions to the wait_for_trigger list. See how to trigger on attribute change

I was able to achieve it using that automation, based on your ideas:

alias: Bewegungssensor Küche
description: ""
trigger:
  - type: motion
    platform: device
    device_id: c42cc5888a0c7349d8425023efdedac7
    entity_id: 28b1c57400b2c8bc5930b8889505edcb
    domain: binary_sensor
condition:
  - condition: device
    type: is_off
    device_id: 60d046de365503c23541fa3495052a69
    entity_id: d82817412c5a9edad36dee61f005e857
    domain: light
  - type: is_illuminance
    condition: device
    device_id: c42cc5888a0c7349d8425023efdedac7
    entity_id: 5b8a7b8c728871a277993dbcd55fe26d
    domain: sensor
    below: 10
action:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.kueche_nachtlicht
  - delay: 
      # this delay is required, otherwise the state 
      # change trigger below is fired immediately
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: c42cc5888a0c7349d8425023efdedac7
        entity_id: 28b1c57400b2c8bc5930b8889505edcb
        domain: binary_sensor
        for:
          hours: 0
          minutes: 2
          seconds: 0
      - platform: state
        # triggers on any change, i.e. also when changing from the Hue app.
        entity_id:
          - light.kueche_decke
  - condition: template
    value_template: "{{ wait.trigger.idx == '0' }}"
  - type: turn_off
    device_id: 60d046de365503c23541fa3495052a69
    entity_id: d82817412c5a9edad36dee61f005e857
    domain: light
mode: single

it uses devices, rather then entities. Looks better in the visual editor, awful in the in yaml. Not sure what the effective difference is.