Motion Detector Blueprint help

Hi all, I have taken the delivered Motion Activated Light. blueprint and modified slightly for my needs but having a bit of an issue still. My need was to trigger scenes instead of a specific light entity or device. That part all works. The part I am struggling with is that I have a variable to see if a certain light in that room is already on to skip running altogether. I thought I had it all working, but have found that it still triggers the off scene after the timer expires. What am I not setting properly to have it skip altogether if the light is on?

blueprint:
  name: Motion-activated Light - All Day
  description: Turn on a light when motion is detected.
  domain: automation
  author: Home Assistant
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    scene_on_target:
      name: Scene On
      selector:
        target:
          entity:
            domain: scene
    scene_off_target:
      name: Scene Off
      selector:
        target:
          entity:
            domain: scene
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 120
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds
    light_to_ignore:
      name: Light Check
      description: Choose a light that if it is on, do not activate scene
      default:
      selector:
        entity:
          filter:
            - domain: light

# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input motion_entity
  from: "off"
  to: "on"

condition: 
  - condition: state
    entity_id: !input light_to_ignore
    state: "off"

action:
  - alias: "Activate On Scene"
    service: scene.turn_on
    target: !input scene_on_target
  - alias: "Wait until there is no motion from device"
    wait_for_trigger:
      platform: state
      entity_id: !input motion_entity
      from: "on"
      to: "off"
  - alias: "Wait the number of seconds that has been set"
    delay: !input no_motion_wait
  - alias: "Activate Off Scene"
    service: scene.turn_on
    target: !input scene_off_target

Hi christopher.hellweg,

I would look at the trace and see what it’s actually doing. I don’t see anything obvious.
Now I’m assuming you have saved this in the UI or reloaded automations if manually editing and the code you are showing is what is actually running, and that you are not testing using ‘run’ in the UI to test.

The logic of your automation seems flawed to me.

Imagine this sequence of events:

  1. You walk into the room, motion sensor activates, because the lights are currently off your actions execute up until wait_for_trigger.
  2. You walk out of the room, motion sensor deactivates, wait_for_trigger is completed and actions move on to the delay phase.
  3. You walk back into the room, motion sensor activates again, but because the lights are already on, your actions does not restart.
  4. The delay finishes, lights turn off, you end up in a dark room…

Easiest way to remedy this (least amount of code changed) would be to lose the conditions section entirely and convert it into an if / then around the initial scene.turn_on:

- if:
    - condition: state
      entity_id: !input light_to_ignore
      state: "off"
  then:
    - alias: "Activate On Scene"
      action: scene.turn_on
      target: !input scene_on_target