Problem With Motion Sensor Automation

Im having an issue setting up an automation for one of my lights and motion sensor. So i have a Tuya branded zigbee PIR sensor and switch in my laundry area and it is connected via Zigbee2MQTT via a Sonoff Dongle. What im trying to achieve is setting up a basic “on if motion is detected/off is motion cleared” automation. Now the turning on part works fine, however i run into an issue of the lights not turning off. This peculiarity only occurs when i add the additional variable of duration in the “motion clear” section. If i set the automation with the variable of “if motion sensor state clear for 5 secs”, the light turns dont off. Without that variable, the automation runs fine and lights turn off.

In the logbook i get this output:

Laundry Motion triggered by state of Laundry Motion Sensor Motion
12:14:49 PM - Now

Laundry Motion Sensor Motion cleared (no motion detected)
12:14:49 PM - Now

Laundry Motion triggered by state of Laundry Motion Sensor Motion
12:13:39 PM - 2 minutes ago

Laundry Motion Sensor Motion detected motion
12:13:39 PM - 2 minutes ago

So i can see that the automation is being triggered, however the lights only turn on. The part of turning off the lights in the automation gets ignored for some reason and the light remains on.

In traces i see this output but to be honest i dont really understand it.

Executed: February 2, 2024 at 12:14:49 PM

if

Executed: February 2, 2024 at 12:14:49 PM
Result:

result: false

if/condition/0

Executed: February 2, 2024 at 12:14:49 PM
Result:

result: false

if/condition/0/entity_id/0

Executed: February 2, 2024 at 12:14:49 PM
Result:

result: false state: ‘off’ duration: ‘2024-02-02T01:14:44.470311+00:00’

This is the configuration of the automation. Would appreciate any help pointing me in the right direction to solve this. Thank you!

alias: Laundry Motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.laundry_motion_sensor_occupancy
condition: []
action:
  - if:
      - condition: state
        state: "on"
        entity_id: binary_sensor.laundry_motion_sensor_occupancy
    then:
      - service: homeassistant.turn_on
        data: {}
        target:
          entity_id: switch.laundry_right
    else: []
  - if:
      - condition: state
        entity_id: binary_sensor.laundry_motion_sensor_occupancy
        state: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 5
    then:
      - service: homeassistant.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.laundry_right
mode: restart

This is because you are triggering on state. The trigger will only occur once, when state changes to off (or on of course), then the condition for off will fail because 5 seconds have not passed yet.

Create 2 automations. One with trigger “on” and one with trigger “off” for 5 minutes.

1 Like

Or do this: Motion activated lights automation

3 Likes

Check the motion device to see whether it uses the Occupancy or Motion entityid (or both).

I had trouble with different versions of Hue and Aqara sensors. Observing them in the History panel showed me which sensors were using Motion or Occupancy (or both), and how long each trait would trigger.

It may be that your sensor detects for 10 seconds (could be longer), so your 5 second timeout wouldn’t trigger.

For many of my motions that use a few minute timeout, I use Wait For Trigger

alias: motion.Garage.lights
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.group_motion_garage
    to: "on"
condition: []
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 90
      transition: 3
    target:
      entity_id: light.group_garage_lights
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.group_motion_garage
        to: "off"
        for:
          hours: 0
          minutes: 4
          seconds: 0
  - service: light.turn_off
    metadata: {}
    data:
      transition: 4
    target:
      entity_id: light.group_garage_lights
mode: single
max_exceeded: silent

Not that I know better than Tom_I - heck, the only YAML I’ve done is to add that Max_exceeded line, which Tom suggested a few days ago. But, there, for whatever it’s worth. . . you go. :upside_down_face:

1 Like

Awesome thanks for pointing me in the right direction guys! I was pulling my hair the past couple of days trying to figure this out.

Thing is i have several mmwave occupancy sensors which i have set to display as motion sensors and i have set up automations exactly like this and they ran fine, so i assumed that this was the way to do it. Just when i thought i had home assistant figured out, something pops up to keep me humble. I guess theres so much more for me to learn.

Once again thank you for the help guys really appreciate it!