-
-
Save vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26 to your computer and use it in GitHub Desktop.
blueprint: | |
name: PIR-activated Light | |
description: Turn on a light when motion is detected on PIR, but only after sunset. | |
domain: automation | |
source_url: https://gist.github.com/vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26 | |
input: | |
motion_entity: | |
name: Motion Sensor | |
selector: | |
entity: | |
domain: binary_sensor | |
device_class: motion | |
light_target: | |
name: Light | |
selector: | |
target: | |
entity: | |
domain: light | |
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 | |
# 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: template | |
value_template: "{{ state_attr('sun.sun', 'elevation') < 0 }}" | |
action: | |
- service: light.turn_on | |
target: !input light_target | |
- wait_for_trigger: | |
platform: state | |
entity_id: !input motion_entity | |
from: "on" | |
to: "off" | |
- delay: !input no_motion_wait | |
- service: light.turn_off | |
target: !input light_target |
blueprint: | |
name: Override PIR-activated Light | |
description: Override PIR automated switching of light when light is manually turned on | |
domain: automation | |
source_url: https://gist.github.com/vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26 | |
input: | |
light_target: | |
name: Light | |
selector: | |
entity: | |
domain: light | |
auto_script: | |
name: Light PIR Script | |
selector: | |
entity: | |
domain: automation | |
reenable_after: | |
name: Re-enable Automation After | |
description: How long after manual turn on to turn off light and re-enable automated behaviour. | |
default: 3 | |
selector: | |
number: | |
min: 0 | |
max: 12 | |
unit_of_measurement: hours | |
# we restart the script, so we can tuen the light off and re-enable automated PIR action | |
mode: restart | |
max_exceeded: silent | |
variables: | |
light_target: !input 'light_target' | |
auto_script: !input 'auto_script' | |
reenable_after: !input 'reenable_after' | |
trigger: | |
# Any change to the state of the light (on or off) | |
# Made more specific to stop trigger by attribute changes | |
- platform: state | |
entity_id: !input light_target | |
from: "on" | |
to: "off" | |
- platform: state | |
entity_id: !input light_target | |
from: "off" | |
to: "on" | |
action: | |
- delay: 1 # Avoid race condition | |
- choose: | |
# Light turned on manually | |
- conditions: > | |
{{ trigger.to_state.state == "on" and | |
is_state_attr(auto_script, "current", 0) }} | |
sequence: | |
- service: automation.turn_off | |
entity_id: !input 'auto_script' | |
- delay: "{{ (reenable_after | int) * 3600 }}" | |
- service: automation.turn_on | |
entity_id: !input 'auto_script' | |
# Light turned off after being turned on manually | |
# PIR script not enabled, so re-enable | |
- conditions: > | |
{{ trigger.to_state.state == "off" and | |
is_state(auto_script, "off") }} | |
sequence: | |
- service: automation.turn_on | |
entity_id: !input 'auto_script' | |
# Light turned off while PIR script is still running | |
# Maybe turned off automatically by PIR script or manually | |
# No way of telling (Race condition), so need to reset script | |
- conditions: > | |
{{ trigger.to_state.state == "off" and | |
is_state_attr(auto_script, "current", 1) }} | |
sequence: | |
- service: automation.turn_off | |
entity_id: !input 'auto_script' | |
- service: automation.turn_on | |
entity_id: !input 'auto_script' | |
# All other trigger states need no action |
The lights are connected to a Shelly 1PM and the physical wall switch is connected to the SW input of the Shelly.
The PIR is an ex-alarm PIR connected via a Konnected board.
I could have sworn it was working but I'm doubting myself now. I'm going to shorten the timeouts and do some controlled testing now I have the house to myself.
This is beautiful work. Thanks. One thing for me is due to the size and layout of the kitchen, I use two motion sensors as potential triggers. Is there a way to do that here?
I don't have a setup or time to modify myself, but I think it should be pretty simple. Have a go yourself!
I think you would only need to modify the "Trigger" elements of the first script, no changes to the second script should be needed:
- Duplicate the "motion_entity" part of the "Input" section, but give it a different name
- Change the "trigger" section to add the second "motion_entity", but use the new name you have given it - This needs to be done as a "list" in YAML (It needs the dashes before the item) See an example in the second script from lines 36-46
Remember to reload blueprints and re-create the automation from the modified blueprint every time you modify it and before you test.
Found this on using multiple trigger entities
And here's where I wound up:
(Thanks for the challenge)
blueprint:
name: PIR-activated Light GM
description: Turn on a light when motion is detected on PIR, but only after sunset.
domain: automation
source_url: https://gist.github.com/vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26
input:
motion_entity_a:
name: First Motion Sensor
selector:
entity:
domain: binary_sensor
device_class: motion
motion_entity_b:
name: Second Motion Sensor
selector:
entity:
domain: binary_sensor
device_class: motion
light_target:
name: Light
selector:
target:
entity:
domain: light
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
# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent
trigger:
- type: motion
platform: state
entity_id:
- !input motion_entity_a
- !input motion_entity_b
from: "off"
to: "on"
condition:
condition: template
value_template: "{{ state_attr('sun.sun', 'elevation') < 0 }}"
action:
- service: light.turn_on
target: !input light_target
- wait_for_trigger:
platform: state
entity_id:
- !input motion_entity_a
- !input motion_entity_b
from: "on"
to: "off"
- delay: !input no_motion_wait
- service: light.turn_off
target: !input light_target
Thoughts and troubleshooting: