Skip to content

Instantly share code, notes, and snippets.

@rosmo
Last active September 13, 2024 15:26
Show Gist options
  • Save rosmo/ab3fd63b95d2e3dc5dabb844684219da to your computer and use it in GitHub Desktop.
Save rosmo/ab3fd63b95d2e3dc5dabb844684219da to your computer and use it in GitHub Desktop.
motion-activated-lights.yaml
# Adapted from: https://community.home-assistant.io/t/motion-activated-light-but-with-conditions/466228
#
# Works better with presence sensors by using a helper boolean
blueprint:
name: Motion-activated Light for Presence Sensors
description: Turn on a light when motion is detected.
domain: automation
input:
motion_entity:
name: Motion Sensor
selector:
entity:
multiple: true
filter:
domain: binary_sensor
# device_class: motion
light_target:
name: Light
selector:
entity:
filter:
domain: light
light_brightness:
name: Brightness
description: Brightness of the light (0-255)
default: 255
selector:
number:
min: 0
max: 255
motion_helper:
name: Motion helper boolean
description: Used to determine if the on action was actuated by the automation.
default: []
selector:
entity:
filter:
domain: input_boolean
additional_conditions:
name: Additional conditions
description: |
Extra conditions you may want to add to this automation
(Example: Home occupied, TV on, etc)
default: []
selector:
condition:
no_motion_wait:
name: Wait time
description: Time to leave the light on after last motion is detected. Note that some sensors have a fading time, which essentially serves as wait time.
default: 120
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds
mode: single
max_exceeded: silent
trigger:
- platform: state
entity_id: !input motion_entity
- platform: state
entity_id:
- !input light_target
condition:
- alias: User pick
condition: !input additional_conditions
variables:
motion_helper: !input motion_helper
light_target: !input light_target
motion_entity: !input motion_entity
action:
- choose:
- conditions: "{{ trigger.entity_id in motion_entity and trigger.to_state.state == 'on' and is_state(motion_helper, 'off') and is_state(light_target, 'off') }}"
sequence:
- alias: "Turn on the light"
service: light.turn_on
data:
brightness: !input light_brightness
target:
entity_id: !input light_target
- alias: "Turn on the helper boolean"
service: input_boolean.turn_on
target:
entity_id: !input motion_helper
- conditions: "{{ trigger.entity_id in motion_entity and trigger.to_state.state == 'off' and is_state(motion_helper, 'on') }}"
sequence:
- alias: "Wait until all motion sensors are off"
wait_template: |
{{ (motion_entity | select('is_state', 'on') | list | count) == 0 }}
- alias: "Wait the number of seconds that has been set"
delay: !input no_motion_wait
- alias: "Turn off the light"
service: light.turn_off
target:
entity_id: !input light_target
- alias: "Turn off the helper boolean"
service: input_boolean.turn_off
target:
entity_id: !input motion_helper
- conditions: "{{ trigger.entity_id == light_target and trigger.to_state.state == 'off' and is_state(motion_helper, 'on') }}"
sequence:
- alias: "Turn off the helper boolean"
service: input_boolean.turn_off
target:
entity_id: !input motion_helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment