Simple Light automation to turn light ON and OFF according to motion in a room

FYI, I took the time to test the blueprint and it failed to turn off the light after the specified delay_time.

The blueprint doesn’t work for the reason I mentioned in my previous post. A State Condition with a for option doesn’t wait for the specified period to occur (that’s how a State Trigger with a for works but not a State Condition).

If you’re interested, the following blueprint does work. It uses two State Triggers:

  1. Detect when motion sensor turns on
  2. Detect when motion sensor turns off and remains off for the duration specified by delay_time.

It employs choose to determine which of the two State Triggers triggered and then performs the associated action.

blueprint:
  name: Turn Light ON and OFF
  description: Turn light on when motion is detected, and turn it off when motion is not detected.
  domain: automation
  input:
    motion_sensor:
      name: Motion Sensor
      description: Choose the motion sensor to control the lights.
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    target_light:
      name: Target Light
      description: Choose the lights that you want to control.
      selector:
        target:
          entity:
            domain: light
    delay_time:
      name: Number of minutes 
      description: Number of minutes, after which the lights will turn off.
      selector:
        number:
          min: 0
          max: 10
          step: 1
          unit_of_measurement: "minutes"

trigger:
  - platform: state
    entity_id: !input motion_sensor
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: !input motion_sensor
    from: 'on'
    to: 'off'
    for:
      minutes: !input delay_time
condition: []
action:
  - choose:
    - conditions: "{{ trigger.to_state.state == 'on' }}"
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 100
        target: !input target_light
    default:
      - service: light.turn_off
        data: {}
        target: !input target_light
mode: single

A small enhancement would be to add a State Condition to prevent turning on a light if it’s already on.

1 Like