Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Home Assistant blueprint that can be used to have a motion sensor switch on (a) device(s), but allow the turning off procedure (after no motion is detected anymore) to be overridden by a switch/button. Useful if you need to activate a light upon entry, and turn off after no motion (most cases), but keep the option to force the light to stay on.
#Blueprint by Klaas Leussink / hnldesign.nl (c) 2022
blueprint:
name: Motion Light with override
source_url: https://gist.github.com/c-kick/32234ea474f6b854a861853b17b881ce
description: >-
This blueprint can be used to have a motion sensor switch on (a) device(s), but allow the turning off procedure (after no motion is detected anymore) to be overridden by a (ZHA) switch/button. Useful if you need to activate a light upon entry, and turn off after no motion (most cases), but keep the option to make the light stay on if you're planning on spending more time in the area (e.g. a shed).
Note: if you need to know what is happening, uncomment the debug message lines.
domain: automation
input:
motion_sensor:
name: Motion Sensor
description: This sensor will trigger the turning on of the target entity.
selector:
entity:
domain: binary_sensor
device_class: motion
multiple: false
target_entity:
name: Target entity
description: The light, switch, scene to turn on (or script to run) when motion is detected. Can be multiple entities.
selector:
entity:
multiple: true
no_motion_wait:
name: Turn off wait time (seconds)
description: Time in seconds to leave the target entity on after no further motion is detected anymore (clear).
default: 60
selector:
number:
min: 0.0
max: 600.0
unit_of_measurement: seconds
step: 1.0
mode: slider
switch_override:
name: Override switch (optional)
description: A switch (e.g. on/off switch) which is used to makes sure the target entity stays on even if no motion is detected anymore. It can also be used to manually switch the target entity on/off. Can be multiple switches.
default:
selector:
device:
integration: zha
#manufacturer: IKEA of Sweden
#model: TRADFRI on/off switch
multiple: false
override_boolean:
name: Override boolean (optional)
description: A boolean that is set on/off using the 'Override switch'. If active, it stops the target entity from turning off after the set 'Turn off wait time'. This is an external boolean, so you can even change it from other automations to interact with this one.
default:
selector:
entity:
domain: input_boolean
multiple: false
check_minutes:
name: Time to wait before check (optional)
description: Time to wait (minutes) before a check runs if the target entity is still on when turning off by motion has been overridden.
default: 60
selector:
number:
min: 0.0
max: 600.0
unit_of_measurement: minutes
step: 1.0
mode: slider
if_still_on:
name: Action(s) if target entity still on (optional)
description: If the target entity is still on when checking after the 'Time to wait before check' delay, run this/these action(s).
default: []
selector:
action: {}
mode: restart
max_exceeded: silent
#set up triggers and id's
trigger:
#event: motion detected
- platform: state
entity_id: !input motion_sensor
to: "on"
id: movement_detected
#event: no motion detected for <x> seconds
- platform: state
entity_id: !input motion_sensor
to: "off"
for:
hours: 0
minutes: 0
seconds: !input no_motion_wait
id: movement_clear
#event: override button off
- platform: event
event_type: zha_event
id: button_pressed
event_data:
device_id: !input switch_override
#handle trigger id's
action:
- variables:
override_state: !input override_boolean
action_still_on: !input if_still_on
- choose:
#override button pressed
- conditions:
- condition: trigger
id: button_pressed
sequence:
- choose:
- conditions: "{{ trigger.event.data.command == 'on' or trigger.event.data.command == 'off' }}"
sequence:
- service_template: "input_boolean.turn_{{ trigger.event.data.command }}"
entity_id: !input override_boolean
- service_template: "homeassistant.turn_{{ trigger.event.data.command }}"
entity_id: !input target_entity
#debug
#- service: notify.notify
# data:
# message: "Switch used, override is now {{ trigger.event.data.command }}"
#motion detected
- conditions:
- condition: trigger
id: movement_detected
sequence:
- service_template: "homeassistant.turn_{{ trigger.to_state.state }}"
target:
entity_id: !input target_entity
#debug
#- service: notify.notify
# data:
# message: "Movement detected! Override: {{ states(override_state) }}"
#motion clear for <x> minutes
- conditions:
- condition: trigger
id: movement_clear
sequence:
- choose:
- conditions:
#no movement, but override active
- condition: template
value_template: '{{ (states(override_state) == ''on'') }}'
sequence:
#debug
#- service: notify.notify
# data:
# message: "No motion detected, but override active, so don't turn off."
- delay:
hours: 0
minutes: !input check_minutes
seconds: 0
- choose:
- conditions: "{{ action_still_on|length > 0 }}"
sequence: !input if_still_on
- conditions:
#no movement, no override active
- condition: template
value_template: '{{ (states(override_state) == ''off'') }}'
sequence:
- service: homeassistant.turn_off
target:
entity_id: !input target_entity
#debug
#- service: notify.notify
# data:
# message: "No motion detected, override not active, so turn off."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment