Need help with motion-triggered lights using Sonoff Zigbee sensor

I’m trying to tie a Sonoff SNZB-03 Zigbee PIR motion sensor into my bathroom lights, so that when motion is detected, the lights stay on for 5 minutes after the motion sensor indicates “no motion”.

The sensor works a little non-intuitively as it’s battery powered:

  • When motion is detected, report binary_sensor state “On” to Home Assistant
  • When no motion is detected for 60 seconds, report state “Off”
  • After reporting no motion, wait some time before checking for motion again

I created two automations.
The first triggered in sensor state “On” and only turned the lights on.
The second triggered on a state change to “Off”, waited 2 minutes, then turned the lights off.
This worked - except that if the motion sensor fired an “Off” state, then an “On” state during the 2-minute timer, the “Off” automation would turn the lights off anyway.

I’m trying to figure out how to cancel any running “switch lights off” automations when the “On” automation runs. Failing that - is there a better way to achieve what I want?

I’m very new to writing automations - so far the only one I’ve got working was one which turned my house side-lights on at dusk and off at 1am, again with two automations…

Thanks!

I’m going to answer my own question … I figured it out!

The answer was to break out the “turn the light off after a delay” event into a script which the automation could start and stop:

alias: Bathroom lights - off after delay
sequence:
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: REMOVED
    entity_id: switch.tasmota_REMOVED_switch_relay_1
    domain: switch
mode: restart

Then rework the automation –

  • Mode is “Restart” (running the script blocks the automation)
  • Trigger on the motion sensor’s state changing (no from/to constraints)
  • Action is an Option:
    • When the motion sensor detects motion, turn the lights on and stop the script
    • Default action, run the script
alias: Bathroom lights - motion trigger
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_motion
condition: []
action:
  - choose:
      - conditions:
          - type: is_motion
            condition: device
            device_id: REMOVED
            entity_id: binary_sensor.bathroom_motion
            domain: binary_sensor
        sequence:
          - type: turn_on
            device_id: REMOVED
            entity_id: switch.tasmota_REMOVED_switch_relay_1
            domain: switch
          - service: script.turn_off
            target:
              entity_id: script.bathroom_lights_off_after_delay
    default:
      - service: script.bathroom_lights_off_after_delay
mode: restart

This fixes the bug I mentioned – where the light would turn off after two minutes even though the sensor was detecting motion.

Thanks for the details on this, I am having exactly the same problem but not quite sure how to impliment your solution. If I dont fix this soon I think my wife will lose the plot when the kitchen lights go off again.
Is there any chance you could detail how you have set theis up as it seems to reference scripts?
Thanks