Created
April 27, 2018 18:43
-
-
Save dale3h/233692a3d754f5e6d8af4edc2cf6db7e to your computer and use it in GitHub Desktop.
Delay and transition lights off after motion is no longer detected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################ | |
## Packages / Motion Lights Off | |
## | |
## @description Delay and transition lights off after motion is no longer detected | |
## @component automation, binary_sensor, input_number, light, sensor | |
## @license MIT | |
## @author @Vasiley, Dale Higgs <@dale3h> | |
################################################################ | |
homeassistant: | |
customize: | |
binary_sensor.kitchen_motion: | |
# Number of seconds the motion sensor stays `on` after last motion detected | |
stays_on: 60 | |
# Entity ID(s) of light(s) to turn off with this motion sensor | |
light_entity: light.kitchen, light.sink | |
binary_sensor.living_room_motion: | |
# Number of seconds the motion sensor stays `on` after last motion detected | |
stays_on: 10 | |
# Entity ID(s) of light(s) to turn off with this motion sensor | |
light_entity: light.living_room | |
automation: | |
- alias: "Motion Off" | |
initial_state: 'on' | |
trigger: | |
- platform: state | |
entity_id: | |
- binary_sensor.kitchen_motion | |
- binary_sensor.living_room_motion | |
to: 'off' | |
action: | |
- wait_template: >- | |
{% set default_time_off_min = 5 %} | |
{# Generate time off entity ID #} | |
{% set time_off_entity = 'input_number.' ~ states('sensor.house_mode_state')|lower|replace(' ', '_') ~ '_off' %} | |
{# Calculate the time off #} | |
{% set time_off = (states(time_off_entity)|int(default_time_off_min) * 60) - trigger.to_state.attributes.stays_on|default|int %} | |
{# Calculate how many seconds ago the sensor last changed #} | |
{% set last_changed = as_timestamp(now()) - as_timestamp(trigger.to_state.last_changed) %} | |
{# Bail out if the motion sensor is tripped during the delay #} | |
{{ is_state(trigger.entity_id, 'on') or last_changed >= time_off }} | |
# Make sure the motion sensor is still off | |
- condition: template | |
value_template: "{{ is_state(trigger.entity_id, 'off') }}" | |
- service: light.turn_off | |
data_template: | |
entity_id: >- | |
{% if trigger.to_state.attributes.light_entity is defined %} | |
{# Use `light_entity` attribute of the binary sensor #} | |
{{ trigger.to_state.attributes.light_entity }} | |
{% else %} | |
{# Try to detect light entity ID #} | |
light.{{ trigger.entity_id|regex_replace('binary_sensor\.(.+)_motion', '\\1') }} | |
{% endif %} | |
transition: >- | |
{% set default_transition = 15 %} | |
{# Generate transition entity ID #} | |
{% set transition_entity = 'input_number.' ~ states('sensor.house_mode_state')|lower|replace(' ', '_') ~ '_mode_transition_value' %} | |
{# Output the transition time, or the default in case of conversion failure #} | |
{{ states(transition_entity)|int(default_transition) }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment