This is a straight forward light automation.
It turns lights on when a motion is detected. It also turns lights OFF when there is no motion for a certain number of minutes (the number of minutes range from 1 to 10, and this is decided by the user).
blueprint:
name: Turn Light ON and OFF
description: Turn light on when motion is detected and 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 mintes
description: Number of minutes, afterwhich 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
condition: []
action:
- choose:
- conditions:
- condition: state
entity_id: !input motion_sensor
state: 'off'
for:
hours: 0
minutes: 0
seconds: 0
sequence:
- delay:
hours: 0
minutes: !input delay_time
seconds: 0
milliseconds: 0
- service: light.turn_off
data:
transition: 10
target: !input target_light
- conditions:
- condition: state
entity_id: !input motion_sensor
state: 'on'
sequence:
- service: light.turn_on
data: {}
target: !input target_light
default: []
mode: restart
At the moment the motion sensor turns off the State Trigger is triggered.
It skips the first conditions in choose because that’s for when the motion sensor turns on.
The second conditions in choose is for when the motion sensor turns off. However, it must have been off for at least X minutes (delay_time). Except I don’t see how it can meet that criteria because it just turned off now, not X minutes ago (and it won’t wait in conditions until X minutes pass).
So how exactly does it ever satisfy the second conditions in choose with that single State Trigger?
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:
Detect when motion sensor turns on
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.
Thank you for your feed back, I reviewed it and you were right, it was not working. I amended it little bit. It should be working now. Have a look at it and tell me what you think. Cheers.
You are very right. I amended the code.
Now, any change in the state of the motion sensor will tigger the automation.
Option 1, if there is motion, turn light on.
Option 2, if motion stops, wait for 5 min and turn lights off.
This works better Thanks for the feedback.
Check your log for any warnings about executing an automation that’s already running.
Let’s say the motion sensor has just turned off.
This triggers the automation and it executes the delay statement (before turning off the light).
While the delay is counting down, let’s say the motion sensor detects motion and turns on again.
This should trigger the automation again but it can’t because the automation is running in single mode and is still busy executing the delay.
The incident is reported in the log as a warning.
The light gets turned off after the delay expires (even though there was motion detected while it was counting down the delay).
You should know that there are several techniques to implement an auto-off light and most are already available as blueprints. The one I posted is just one of the ways to do it (successfully). You’re basically trying to reinvent the wheel using techniques that have already been proven to not to work.
Thank you for that, and I think I had concluded the same, the trouble is it is my complete nativity with YAML that is the problem, and despite having a go I cannot get my head around the coding for this.
I strongly recommend that you review the documentation links posted below because that’s so profoundly wrong that anything I provide is unlikely to make sense to you.