MOTION ACTIVATE LIGHT EXTENDED - New version
This is blueprint for Home Assistant intended to help to create automations to turn on/off lights as a reaction of a movement sensor.
This is an evolution of standard motion activated light.
In this blueprint, I’ve supported some important additional features intended to improve the flexibility of the component.
-
Ability to activate enable/disable the motion activation light based on sunset and sunlight
-
Ability to override the sunset / sunrise conditional activation based on the weather
-
Ability to define a timeout after that the light turn off also if movement sensor is already on (useful to situations in which sensors sometimes fails to communicate deactivation due to connectivity problems).
-
NEW - Ability to don’t execute the blueprint if the light (or the lights) is already on (prevent auto-turn-off if the light was turned on manually). This option is false by default to preserve backward compatibility. When enabled, it ensures that the blueprint does not trigger if at least one of the lights associated with the motion sensor is on. The purpose of this is to prevent the automation from turning off the light at the end of the period if it was manually turned on with a switch rather than by the motion sensor (thx to @wgumaa for the suggestion)
All this options can be activated or not so you can freely combine them. For use the blueprint in its more general form you need one or more entities of this domains/class:
- binary_sensor / motion
- light
- weather
blueprint:
name: "Motion-activated Light extended"
description: "Turn on a light when a movement occurs"
domain: automation
source_url: https://github.com/giannisigalotti/HomeAssistant/blob/main/blueprints/motion-activated-light.yaml
input:
motion_entity:
name: "Motion sensor"
selector:
entity:
domain: binary_sensor
device_class: motion
light_target:
name: Light
selector:
target:
entity:
domain: light
no_motion_wait:
name: "Wait time"
description: "The time to wait before turning off the light after no movement is detected."
default: 120
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds
skip_if_light_is_on:
name: Do not execute if the light is already on (prevent auto-turn-off if the light was turned on manually).
selector:
boolean:
default: false
no_sunset_handling:
name: "Disable day/night management?"
description: "When you turn this option to off, you can define how many hour before sunset and after sunshine the automation turn on the light"
selector:
boolean:
default: false
wheater_handling:
name: "Enable meteo managment?"
description: "Does it also take weather into account to enable/disable motion sensors (use the time range only when it's sunny and disable otherwise)?"
selector:
boolean:
default: false
wheater_entity:
name: "Weather status sensor"
description: "This sensor is used only with the Meteo managment option to determine if the weather is sunny or not (useful if you are in a room usually dark in case of bad weather condition"
selector:
entity:
domain:
- weather
multiple: false
default: "weather.home"
sunset_start_hh:
name: "Pre-sunset hours activation"
description: "How many hours before sunset does the management activate (negative numbers indicate a delay)?"
selector:
number:
min: 0
max: 12
unit_of_measurement: hours
default: 0
sunset_start_mm:
name: "Pre-sunset minutes activation"
description: "How many minutes before sunset does the management activate (negative numbers indicate a delay)?"
selector:
number:
min: 0
max: 59
unit_of_measurement: minutes
default: 0
sunrise_start_hh:
name: "Post-sunrise hours activation"
description: "How many hours after sunrise does the management deactivate (negative numbers indicate an advance)?"
selector:
number:
min: 0
max: 12
unit_of_measurement: hours
default: 0
sunrise_start_mm:
name: "Post-sunrise minutes activation"
description: "How many minutes after sunrise does the management deactivate (negative numbers indicate an advance)?"
selector:
number:
min: 0
max: 59
unit_of_measurement: minutes
default: 0
timeout:
name: "Deactivation timeout?"
description: "After how many minutes does the switch turn off automatically (-1 if no timeout)"
selector:
number:
min: -1
max: 60
unit_of_measurement: minutes
default: 5
# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent
trigger:
platform: state
entity_id: !input motion_entity
from: "off"
to: "on"
variables:
# Make input my_input available as a script level variable
dusk_start_hour: !input sunset_start_hh
dusk_start_minute: !input sunset_start_mm
rising_start_hour: !input sunrise_start_hh
rising_start_minute: !input sunrise_start_mm
use_weather: !input wheater_handling
skip_light_on: !input skip_if_light_is_on
target_lights: !input light_target
force_skip: >
{{ skip_light_on and
(expand(
(([] if target_lights.device_id is undefined
else target_lights.device_id | device_entities
if target_lights.device_id is string
else target_lights.device_id | map('device_entities') | sum(start=[]) )
+
([] if target_lights.entity_id is undefined
else [target_lights.entity_id]
if target_lights.entity_id is string
else target_lights.entity_id ))
| select('search', '^light\.') | list
) | selectattr('state', 'eq', 'on') | list | length > 0)
}}
sun_rising: >
{% if (states.sun.sun.state == "above_horizon") %}
{{ states.sun.sun.last_changed }}
{%- else -%}
{{ states.sun.sun.attributes.next_rising }}
{%- endif %}
sun_dusk: >
{% if (states.sun.sun.state == "above_horizon") %}
{{ states.sun.sun.attributes.next_dusk }}
{%- else -%}
{{ states.sun.sun.last_changed }}
{%- endif %}
wheater_sensor: !input wheater_entity
no_sunny_wheater: >
{{ states(wheater_sensor) != 'sunny' and use_weather }}
condition:
- condition: and
conditions:
- condition: template
value_template: "{{ not force_skip }}"
- condition: or
conditions:
- condition: template
value_template: !input no_sunset_handling
- condition: template
value_template: "{{ no_sunny_wheater }}"
- condition: and
conditions:
- "{{ as_timestamp(now()) >= ((as_timestamp(sun_dusk) - dusk_start_hour*3600 - dusk_start_minute*60) )}}"
- "{{ as_timestamp(now()) < ((as_timestamp(sun_rising) + rising_start_hour*3600 + rising_start_minute*60) )}}"
action:
- service: light.turn_on
target: !input light_target
- wait_for_trigger:
platform: state
entity_id: !input motion_entity
from: "on"
to: "off"
timeout:
hours: 0
minutes: !input timeout
seconds: 0
milliseconds: 0
- delay: !input no_motion_wait
- service: light.turn_off
target: !input light_target