Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rattletat/215b48b355738816d6d4676828e55efa to your computer and use it in GitHub Desktop.
Save rattletat/215b48b355738816d6d4676828e55efa to your computer and use it in GitHub Desktop.
Motion-activated light/switches with added condtions.
blueprint:
name: Motion-activated Light/Switch with Conditions
description: Turn on a light or switch when motion is detected and conditions are fulfilled.
homeassistant:
min_version: 2023.8.0
domain: automation
source_url: https://community.home-assistant.io/t/presence-sensor-light-automation/564590
input:
motion_entity:
name: Motion Sensor
selector:
entity:
filter:
device_class: motion
domain: binary_sensor
device_target:
name: Light/Switch
description: Light or switch device
selector:
target:
entity:
- domain:
- light
- switch
bright_percentage:
name: Bright Percentage
description: Brightness level that the light will turn into once is turned on. Not applicable to switches.
default: 100
selector:
number:
min: 0.0
max: 100.0
unit_of_measurement: percentage
mode: slider
step: 1.0
no_motion_wait:
name: Wait time
description: Time to leave the light/switch on after last motion is detected.
default: 0
selector:
number:
min: 0.0
max: 3600.0
unit_of_measurement: seconds
mode: slider
step: 1.0
off_transition:
name: Transition time
description: Time in seconds of transition turning off. Not applicable to switches.
default: 0
selector:
number:
min: 0.0
max: 60.0
unit_of_measurement: seconds
mode: slider
step: 1.0
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:
trigger:
platform: state
entity_id: !input motion_entity
from: 'off'
to: 'on'
condition:
- condition: !input additional_conditions
action:
- choose:
- conditions:
- condition: template
value_template: "{{ 'light' in expand(states.light) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
sequence:
- service: light.turn_on
target:
entity_id: "{{ expand(states.light) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
data:
brightness_pct: !input bright_percentage
- conditions:
- condition: template
value_template: "{{ 'switch' in expand(states.switch) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
sequence:
- service: switch.turn_on
target:
entity_id: "{{ expand(states.switch) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
- alias: Wait until there is no motion from device
wait_for_trigger:
platform: state
entity_id: !input motion_entity
from: 'on'
to: 'off'
- alias: Wait the number of seconds that has been set
delay: !input no_motion_wait
- choose:
- conditions:
- condition: template
value_template: "{{ 'light' in expand(states.light) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
sequence:
- service: light.turn_off
target:
entity_id: "{{ expand(states.light) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
data:
transition: !input off_transition
- conditions:
- condition: template
value_template: "{{ 'switch' in expand(states.switch) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
sequence:
- service: switch.turn_off
target:
entity_id: "{{ expand(states.switch) | selectattr('entity_id', 'in', expand(input_device_target) | map(attribute='entity_id')) | map(attribute='entity_id') | list }}"
mode: restart
max_exceeded: silent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment