Last active
September 13, 2023 08:48
-
-
Save vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26 to your computer and use it in GitHub Desktop.
Home Assistant Blueprints - Automated PIR light with manual override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this on using multiple trigger entities
And here's where I wound up:
(Thanks for the challenge)