Motion Detector Disable by Light Switch

Hello,

I came from Insteon world and an ISY994i so I’m still trying to figure out the equivalent in HA. In the past, I would use state variables and integer variables where’d I’d need them. 0 = off, 1=on light swtich, 2=motion on, etc. In HA, everyone mentions input boolean (toggle?) as a solution.

After trying a couple of automations… my input boolean directly follows the light switch. So physical pressing the light switch throws the input boolean and also when the motion sensor turns on the light switch…disabling the automation and never turning off. And thats why I’m posting it here. So I thought to disable the motion automation instead.

Hardware:
Lutron Caseta Light Switch (Relay, non dimming)
Aqara Motion Sensor P1
Aqara M2 Hub
I believe its an Homekit Integration inside HA.

Here’s how I want it to work:
Light Switch On - Disable Motion
Light Switch Off - Enable Motion

alias: Master Bedroom Closet - Light On / Off - Disable Motion Automation
description: Master Bedroom Closet - Light On / Off - Disable Motion Automation
trigger:
  - platform: device
    type: turned_on
    device_id: 26979b996f37747861da6497bcd54245
    entity_id: d732cd82d3630a17d358848644dc17bd
    domain: light
    id: light-on
  - platform: device
    type: turned_off
    device_id: 26979b996f37747861da6497bcd54245
    entity_id: d732cd82d3630a17d358848644dc17bd
    domain: light
    id: light-off
condition: []
action:
  - if:
      - condition: trigger
        id:
          - light-on
    then:
      - service: automation.turn_off
        data:
          stop_actions: false
        target:
          entity_id: automation.master_bedroom_closet_motion_sensor_light_on_off
  - if:
      - condition: trigger
        id:
          - light-off
    then:
      - service: automation.turn_on
        data: {}
        target:
          entity_id: automation.master_bedroom_closet_motion_sensor_light_on_off
mode: single
alias: Master Bedroom Closet - Motion Sensor - Light On / Off
description: Master Bedroom Closet - Motion Sensor - Light On / Off
trigger:
  - type: motion
    platform: device
    device_id: 84b1fc9f4142d4d19e888b8f24c004fb
    entity_id: 30ded091ab9a67677ec8063be70b760e
    domain: binary_sensor
    id: motion-detected
  - type: no_motion
    platform: device
    device_id: 84b1fc9f4142d4d19e888b8f24c004fb
    entity_id: 30ded091ab9a67677ec8063be70b760e
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: motion-stopped
condition: []
action:
  - if:
      - condition: trigger
        id: motion-detected
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.master_bedroom_closet_light
    else: []
  - if:
      - condition: trigger
        id:
          - motion-stopped
    then:
      - type: turn_off
        device_id: 26979b996f37747861da6497bcd54245
        entity_id: d732cd82d3630a17d358848644dc17bd
        domain: light
mode: single

You can simplify this massively by just having an automation that triggers on your motion sensor but with a condition that the light is off. That way, the automation will only run if motion is detected and the light is off.

You also dont need any input boolean either just condition of the light device being off.

EDIT: i would also rexommend using the automation UI for this as it is a fairly simple automation.

Heres a similar example to help you on your way.

description: "Turn on off light based on presence sensor"
mode: single
trigger:
  - type: present
    platform: device
    device_id: 3d8a26df590a11c98ec9da3ef89d9bdb
    entity_id: 3f1125a8cfa710773b4307b229b7ce8a
    domain: binary_sensor
    id: motion
  - type: not_present
    platform: device
    device_id: 3d8a26df590a11c98ec9da3ef89d9bdb
    entity_id: 3f1125a8cfa710773b4307b229b7ce8a
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: no-motion
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - motion
          - condition: state
            entity_id: light.lounge_lamps
            state: "off"
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.lounge_lamps
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - no-motion
          - condition: state
            entity_id: light.lounge_lamps
            state: "on"
    then:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.lounge_lamps

Its testing great except for one issue.

Motion On - Light On = perfect
Motion Stopped - Light Off = perfect
Light switch On / Off - perfect.

However
Light switch On - motion detected - motion stopped (after 30 sec) - Light Switch Off.

I don’t want that to happen. If the wife turns on the light, I want it to stay on until she turns it off. Manual On & Manual Off.

Now is where I could do an Input Boolean - Toggle. Correct?
Light Switch On - & Motion clear - then toggle Input Boolean On … in a separate automation?
then a (3rd) condition (Input Boolean = On)

Then the question is…how & where do I toggle (off) the Input Boolean.
Maybe a Light Switch Off - Motion Clear (45 seconds) then toggle Input Boolean off

The light swtich would have to be pressed before walking into the closet and setting off the motion detector.

Again, i dont think you need an input boolean. Try creating a 3rd trigger that is light turned on with a trigger id of light_manual.

In your actions have if triggered by light_manual, turn on light, then wait for light to turn off. As the mode is set to single, it will not trigger again until the first run has finished. As such, when the light turned on waiting for turn off, it will not run on presence until a manual turn on is turned off. I would set delay to maybe 30 secs on the loop. Like this…

description: Turn on off light based on presence sensor
mode: single
trigger:
  - type: present
    platform: device
    device_id: 3d8a26df590a11c98ec9da3ef89d9bdb
    entity_id: 3f1125a8cfa710773b4307b229b7ce8a
    domain: binary_sensor
    id: motion
  - type: not_present
    platform: device
    device_id: 3d8a26df590a11c98ec9da3ef89d9bdb
    entity_id: 3f1125a8cfa710773b4307b229b7ce8a
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: no-motion
  - platform: state
    entity_id:
      - light.lounge_lamps
    to: "on"
    id: manual_light
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - motion
          - condition: state
            entity_id: light.lounge_lamps
            state: "off"
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.lounge_lamps
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - no-motion
          - condition: state
            entity_id: light.lounge_lamps
            state: "on"
    then:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.lounge_lamps
  - if:
      - condition: trigger
        id:
          - manual_light
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.lounge_lamps
      - wait_for_trigger:
          - platform: state
            entity_id:
              - light.lounge_lamps
            to: "off"

EDIT: actually, this may loop if turned on by presence. Maybe add a condition to the manual light that presence is not detected

Actually thinking about this more, it should probably have 1 trigger for turning on light manually and 1 trigger for presence. Then depending how the process started (manual or auto) wait for the off state to happen, either light turned off again or no presence. This will ensure automation will not trigger again until the end action is completed based on start action. Like this…

description: Turn on off light based on presence sensor or manual
mode: single
trigger:
  - platform: state
    entity_id:
      - light.lounge_lamps
    to: "on"
    id: manual_light
  - type: present
    platform: device
    device_id: 3d8a26df590a11c98ec9da3ef89d9bdb
    entity_id: 3f1125a8cfa710773b4307b229b7ce8a
    domain: binary_sensor
    id: motion
condition: []
action:
  - if:
      - condition: trigger
        id:
          - manual_light
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.lounge_lamps
      - wait_for_trigger:
          - platform: state
            entity_id:
              - light.lounge_lamps
            to: "off"
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - motion
          - condition: state
            entity_id: light.lounge_lamps
            state: "off"
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.lounge_lamps
      - wait_for_trigger:
          - type: not_present
            platform: device
            device_id: 3d8a26df590a11c98ec9da3ef89d9bdb
            entity_id: 3f1125a8cfa710773b4307b229b7ce8a
            domain: binary_sensor
            for:
              hours: 0
              minutes: 0
              seconds: 5
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.lounge_lamps
1 Like

Wow. Thank You! It works perfect. I came up with a solution like your post 2nd to the last. It worked, but with two automations and a helper. Now down to one automation and no helper! Super grateful!

One question. So with these Aqara motion sensors, after the default 30 timeout setting in each P1 motion sensor, they actually turn off the light briefly and then the light comes back on even when constant motion is happening. 30 seconds is acceptable and no need to alter that. My thinking is to add a delay/wait/timeout into your automation recipe. I tried it at the “Wait for Master Bedroom Closet Motion stopped detecting motion”. From 00, tried 05 & 35 seconds. No luck. “Continue On Timeout” seems important to have on. I added a “Wait” for 5 seconds before the very last Call A Service Light Turn Off. Then the light stayed off for an amount of time.

Thanks,

Show me your yaml for your automation

Here it is…

alias: Master Bedroom - Closet Light Motion & Manual (On / Off)
description: Master Bedroom - Closet Light Motion & Manual (On / Off)
trigger:
  - platform: device
    type: turned_on
    device_id: 26979b996f37747861da6497bcd54245
    entity_id: d732cd82d3630a17d358848644dc17bd
    domain: light
    id: manual_light
  - type: motion
    platform: device
    device_id: 84b1fc9f4142d4d19e888b8f24c004fb
    entity_id: 30ded091ab9a67677ec8063be70b760e
    domain: binary_sensor
    id: motion_on
condition: []
action:
  - if:
      - condition: trigger
        id:
          - manual_light
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id:
            - light.master_bedroom_closet_light
      - wait_for_trigger:
          - platform: device
            type: turned_off
            device_id: 26979b996f37747861da6497bcd54245
            entity_id: d732cd82d3630a17d358848644dc17bd
            domain: light
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - motion_on
          - condition: device
            type: is_off
            device_id: 26979b996f37747861da6497bcd54245
            entity_id: d732cd82d3630a17d358848644dc17bd
            domain: light
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id:
            - light.master_bedroom_closet_light
      - wait_for_trigger:
          - type: no_motion
            platform: device
            device_id: 84b1fc9f4142d4d19e888b8f24c004fb
            entity_id: 30ded091ab9a67677ec8063be70b760e
            domain: binary_sensor
        timeout:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 0
      - service: light.turn_off
        data: {}
        target:
          entity_id:
            - light.master_bedroom_closet_light
mode: single

Just thinking. If on the Light Switch shutting Off after the motion clears… would be excellent to wait or delay the automation from a couple of seconds, then it would say exit the IF THEN loop.

Or wait for motion = clear for 5/10/15 seconds and then shut the light off.

Motion Detected / Motion Clear / Motion Detected every 30 or so seconds. Light goes fully off and then back on almost immediately.

I think I figured it out. Still more testing needed. I went the variables approach. Call Service Input Number Set. 0 is off (motion & manual) & 1 is on manual & 2 is motion on.

One thing I would like, if its on motion and I pressed the light switch, would be awesome if it switched to “1” manual on.

I changed the Automation mode to “Restart” too.

alias: Utility Room Test
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 8b513b5ee82c3952067ac1826326528e
    entity_id: bf9a358483c616b25e37b0f43a2bff8b
    domain: light
    id: manual_on
  - platform: device
    type: turned_off
    device_id: 8b513b5ee82c3952067ac1826326528e
    entity_id: bf9a358483c616b25e37b0f43a2bff8b
    domain: light
    id: manual_off
  - type: motion
    platform: device
    device_id: f957e6a3a2358ffae32f191135156740
    entity_id: 41cd0527a251a1e3e7847e3cb1edca0d
    domain: binary_sensor
    id: motion_on
  - type: no_motion
    platform: device
    device_id: f957e6a3a2358ffae32f191135156740
    entity_id: 41cd0527a251a1e3e7847e3cb1edca0d
    domain: binary_sensor
    id: motion_off
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - manual_on
          - type: is_no_motion
            condition: device
            device_id: f957e6a3a2358ffae32f191135156740
            entity_id: 41cd0527a251a1e3e7847e3cb1edca0d
            domain: binary_sensor
    then:
      - service: input_number.set_value
        data:
          value: 1
        target:
          entity_id: input_number.utility_motion_number
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - motion_on
          - condition: device
            type: is_off
            device_id: 8b513b5ee82c3952067ac1826326528e
            entity_id: bf9a358483c616b25e37b0f43a2bff8b
            domain: light
    then:
      - service: input_number.set_value
        data:
          value: 2
        target:
          entity_id: input_number.utility_motion_number
      - type: turn_on
        device_id: 8b513b5ee82c3952067ac1826326528e
        entity_id: bf9a358483c616b25e37b0f43a2bff8b
        domain: light
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - manual_off
    then:
      - service: input_number.set_value
        data:
          value: 0
        target:
          entity_id: input_number.utility_motion_number
  - if:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - motion_off
    then:
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - if:
          - type: is_no_motion
            condition: device
            device_id: f957e6a3a2358ffae32f191135156740
            entity_id: 41cd0527a251a1e3e7847e3cb1edca0d
            domain: binary_sensor
        then:
          - type: turn_off
            device_id: 8b513b5ee82c3952067ac1826326528e
            entity_id: bf9a358483c616b25e37b0f43a2bff8b
            domain: light
          - service: input_number.set_value
            data:
              value: 0
            target:
              entity_id: input_number.utility_motion_number
mode: restart