Skip to content

Instantly share code, notes, and snippets.

@vanceb
Last active September 13, 2023 08:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26 to your computer and use it in GitHub Desktop.
Save vanceb/b7402b9d1f66a6a2fb5c8e6de2a39b26 to your computer and use it in GitHub Desktop.
Home Assistant Blueprints - Automated PIR light with manual override
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
@vanceb
Copy link
Author

vanceb commented Nov 19, 2021

Thoughts and troubleshooting:

  1. Your kitchen light switch - Is this a standard wall switch? i.e. it physically turns the power off to the kitchen light, or is it a "Pushbutton", or light in the UI, that controls the kitchen light through Home Assistant? If it is a physical switch then this isn't going to work because as far as home assistant is concerned all that has happened is that the kitchen light was "Unavailable" for a short period, not that it was "Turned off".
  2. For troubleshooting - go through each scenario and see whether the PIR automation gets turned off or not [Configuration -> Automations]. (My guess is that it doesn't. If it were off then it wouldn't turn off the light after the timer finishes).

@healeydave
Copy link

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.

@uSlackr
Copy link

uSlackr commented Jan 3, 2022

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?

@vanceb
Copy link
Author

vanceb commented Jan 4, 2022

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:

  1. Duplicate the "motion_entity" part of the "Input" section, but give it a different name
  2. 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.

@uSlackr
Copy link

uSlackr commented Jan 4, 2022 via email

@uSlackr
Copy link

uSlackr commented Feb 11, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment