Skip to content

Instantly share code, notes, and snippets.

@rosmo
Last active February 8, 2025 17:17
Show Gist options
  • Select an option

  • Save rosmo/ab3fd63b95d2e3dc5dabb844684219da to your computer and use it in GitHub Desktop.

Select an option

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
- switch
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
retrigger_time:
name: Retrigger time
description: Do not trigger for this period of time after light was turned off automatically or manually.
default: 0
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds
mode: single
max_exceeded: silent
triggers:
- 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
retrigger_time: !input retrigger_time
no_motion_wait: !input no_motion_wait
# To enable debug logging, add HA config yaml the following:
# logger:
# logs:
# motion_light: debug
action:
#- alias: Debug logging
# service: system_log.write
# data:
# level: debug
# logger: motion_light
# #message: "Motion light debug: light: {{ light_target|to_json }}, motion_entity: {{ motion_entity|to_json }}, motion helper: {{ motion_helper|to_json }}, trigger: {{ trigger|to_json }}, motion entity state: {{ states(motion_entity)|to_json }}, motion helper state: {{ states(motion_helper)|to_json }}"
# message: "Motion light debug: light: {{ light_target|to_json }}, motion_entity: {{ motion_entity|to_json }}, motion helper: {{ motion_helper|to_json }}, last changed: {{ as_timestamp(states[motion_entity].last_changed) }}"
- 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') and (retrigger_time == 0 or ((as_timestamp(now()) - as_timestamp(states[motion_helper].last_changed)) >= retrigger_time)) }}"
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 the number of seconds that has been set"
delay: !input no_motion_wait
- repeat:
while: "{{ (motion_entity | select('is_state', 'on') | list | count) > 0 }} "
sequence:
- alias: "Wait the number of seconds that has been set"
delay: "{% if no_motion_wait > 0 %}{{ no_motion_wait }}{% else %}10{% endif %}"
- 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