Skip to content

Instantly share code, notes, and snippets.

@storeman
Created January 7, 2023 23:18
Show Gist options
  • Save storeman/6aa0be47f705af6fc72a32e2e4dab269 to your computer and use it in GitHub Desktop.
Save storeman/6aa0be47f705af6fc72a32e2e4dab269 to your computer and use it in GitHub Desktop.
blueprint:
name: Advanced motion activated device
description: >
Turn on an entity with a lot of conditional options
It can also trigger on other changes, so if "coming home" is detected with a small delay, and motion is noticed, the lights
will still turn on if you are detected as being home, and there is still motion in the room.
Some condition values have an option for entity-input and numeric input. If you fill both, both will be checked with an OR condtion.
domain: automation
input:
motion_sensor:
name: Motion Sensor
description: This sensor will trigger the turning on of the target entity.
selector:
entity:
device_class: motion
target_entity:
name: Target entity
description: The light, switch, scene to turn on (or script to run) when the automation is triggered.
selector:
entity:
trigger_entities:
name: (OPT) Other triggers
description: >
Other triggers that can (re)start the automation.
default: []
selector:
entity:
multiple: true
allowing_entities:
name: (OPT) Allowing entities, these entities MUST be all "on" to continue
description: All entities states should be on to be allowed. Useful for a `schedule` helper or other switches
default:
selector:
entity:
multiple: true
blocking_entities:
name: (OPT) Blocking entities, these entities MUST be all "off" to continue
description: If one of the states of blocking entities is `on`, the automation will stop
default:
selector:
entity:
multiple: true
turn_off_when_conditions_unmet:
name: (OPT) Turn off entity if conditions unmet
description: When the blocking or allowing entities conditions are not met, should the target be switched off?
default: false
selector:
boolean:
light_brightness:
name: (OPT) Target brightness if target is light.
description: >
Brightness of the target light or group of lights (percentage, 0-100). If entity below is chosen,
this value is ignored.
default:
selector:
number:
min: 0
max: 100
light_brightness_entity:
name: (OPT) Target brightness if target is light (input_number entity)
description: >
Brightness percentage of the target light or group of lights provided by an input_number. This entity
supersedes the value given above.
default:
selector:
entity:
domain: input_number
illuminance_sensor:
name: (OPT) Illuminance sensor
description: This sensor will be used to determine the illumination.
default:
selector:
entity:
domain: sensor
device_class: illuminance
illuminance_cutoff:
name: (OPT) Illuminance cutoff value
description: >
This will be used to compare to the current illumination to determine if it is low.
If entity below is configured, this value is ignored
default:
selector:
number:
min: 0
max: 1000
illuminance_cutoff_entity:
name: (OPT) Illuminance cutoff value (input_number)
description: >
This input_number will be used to compare to the current illumination to determine if it is low. This
entity supersedes the value given above.
default:
selector:
entity:
domain: input_number
no_motion_wait:
name: (OPT) Turn off wait time (minutes, float)
description: >
Time in minutes to leave the target entity on after last motion is detected. If not used entity will not
auto turn off. If entity below is entered, this value will be ignored.
default:
selector:
number:
min: 0
max: 60
step: 0.5
no_motion_wait_entity:
name: (OPT) Turn off wait time entity (input_number, minutes)
description: >
Time in minutes to leave the target entity on after last motion is detected. If not used entity will not
auto turn off. This entity supersedes the value given above.
default:
selector:
entity:
domain: input_number
timer_entity:
name: (OPT) The timer entity to control
description: >
If you supply a timer entity, you can view the progress of how long the lights remain on. PLUS: the timer will
continue counting down when other triggers except the motion trigger fire.
default: {}
selector:
entity:
domain: timer
blocking_turn_off_entities:
name: (OPT) Turn-off Blocking entity
description: >
If one of these entities states is on, it will prevent the target entity from turning off after the set delay.
default:
selector:
entity:
multiple: true
target_off_entity:
name: (OPT) Turn-off entity
description: If defined, this entity will be turned off instead of the default target entity. This can be helpful when using target entities of type scene or script.
default:
selector:
entity:
mode: restart
max_exceeded: silent
variables:
motion_sensor: !input motion_sensor
target_entity: !input target_entity
target_domain: "{{ states[target_entity].domain }}"
light_brightness: !input light_brightness
light_brightness_entity: !input light_brightness_entity
illuminance_sensor: !input illuminance_sensor
illuminance_cutoff: !input illuminance_cutoff
illuminance_cutoff_entity: !input illuminance_cutoff_entity
blocking_entities: !input blocking_entities
allowing_entities: !input allowing_entities
no_motion_wait: !input no_motion_wait
no_motion_wait_entity: !input no_motion_wait_entity
timer_entity: !input timer_entity
blocking_turn_off_entities: !input blocking_turn_off_entities
target_off_entity: !input target_off_entity
turn_off_when_conditions_unmet: !input turn_off_when_conditions_unmet
trigger:
- platform: state
entity_id: !input motion_sensor
- platform: state
entity_id: !input trigger_entities
condition:
# First condition: When entity was already on because the automation ran recently, do not check illuminance because it could have increased above threshold because of a light that was just turned on.
- condition: template
value_template: >
{{
states[target_entity].state == 'on'
or (timer_entity != none and states[timer_entity] is defined and states[timer_entity].state == 'active')
or illuminance_sensor == none
or (illuminance_cutoff != none and states[illuminance_sensor].state | int < illuminance_cutoff | int)
or (illuminance_cutoff_entity != none and states[illuminance_sensor].state | int < states[illuminance_cutoff_entity].state | int)
}}
# Second condition: Motion should be detected or entity must be on already
- condition: template
value_template: >
{{
states[target_entity].state == 'on'
or states[motion_sensor].state == 'on'
or (timer_entity != none and states[timer_entity].state == 'active')
}}
action:
- if:
- condition: or
conditions:
- condition: template
value_template: >
{% set allowed = namespace(b=true) %}
{% if blocking_entities != none -%}
{% for blocking_entity in blocking_entities -%}
{% set allowed.b = allowed.b and (states[blocking_entity].state == 'off') %}
{%- endfor %}
{%- endif %}
{{ not allowed.b }}
- condition: template
value_template: >
{% set allowed = namespace(b=true) %}
{% if allowing_entities != none -%}
{% for allowing_entity in allowing_entities -%}
{% set allowed.b = allowed.b and states[allowing_entity].state == 'on' %}
{%- endfor %}
{%- endif %}
{{ not allowed.b }}
then:
- if:
- condition: template
value_template: "{{ turn_off_when_conditions_unmet }}"
then:
- choose:
- conditions:
- condition: template
value_template: "{{ target_off_entity != none and states[timer_entity] is defined }}"
sequence:
- service: homeassistant.turn_off
entity_id: !input target_off_entity
default:
- service: homeassistant.turn_off
entity_id: !input target_entity
- if:
- condition: template
value_template: "{{ timer_entity != None and states[timer_entity] is defined }}"
then:
- service: timer.cancel
entity_id: !input timer_entity
- stop:
- choose:
- conditions:
- condition: template
value_template: "{{ target_domain == 'light' and (light_brightness != none or light_brightness_entity != none) }}"
sequence:
- service: light.turn_on
entity_id: !input target_entity
data:
brightness: >
{% if light_brightness_entity != none -%}
{{ (states[light_brightness_entity].state|float * 2.55)|round }}
{% else %}
{{ (light_brightness|float * 2.55)|round }}
{%- endif %}
default:
- service: homeassistant.turn_on
entity_id: !input target_entity
- condition: template
value_template: "{{ no_motion_wait != none or no_motion_wait_entity != none }}"
- choose:
- conditions:
- condition: template
value_template: "{{ (states[motion_sensor].state == 'on') }}"
sequence:
- if:
- condition: template
value_template: "{{ timer_entity != None and states[timer_entity] is defined and states[timer_entity].state == 'active' }}"
then:
- service: timer.cancel
entity_id: !input timer_entity
- wait_for_trigger:
platform: state
entity_id: !input motion_sensor
from: "on"
to: "off"
- if:
- condition: template
value_template: "{{ timer_entity != None and states[timer_entity] is defined and states[timer_entity].state != 'active' }}"
then:
- service: timer.start
entity_id: !input timer_entity
data:
duration:
minutes: >
{% if no_motion_wait_entity != none -%}
{{ states[no_motion_wait_entity].state|float }}
{% else %}
{{ no_motion_wait|float }}
{%- endif %}
- if:
- condition: template
value_template: "{{ timer_entity != None and states[timer_entity] is defined }}"
then:
- wait_for_trigger:
platform: event
event_type: timer.finished
event_data:
entity_id: !input timer_entity
else:
- delay:
minutes: >
{% if no_motion_wait_entity != none -%}
{{ states[no_motion_wait_entity].state|float }}
{% else %}
{{ no_motion_wait|float }}
{%- endif %}
- condition: template
value_template: >
{% set allowed = namespace(b=true) %}
{% if blocking_turn_off_entities != none -%}
{% for blocking_entity in blocking_turn_off_entities -%}
{% set allowed.b = allowed.b and (states[blocking_entity].state == 'off') %}
{%- endfor %}
{%- endif %}
{{ allowed.b }}
- choose:
- conditions:
- condition: template
value_template: "{{ (target_off_entity != none) }}"
sequence:
- service: homeassistant.turn_off
entity_id: !input target_off_entity
default:
- service: homeassistant.turn_off
entity_id: !input target_entity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment