Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save philippsander/4a7f333d4ecc4f4d4a16bcd3d54d516e to your computer and use it in GitHub Desktop.
Save philippsander/4a7f333d4ecc4f4d4a16bcd3d54d516e to your computer and use it in GitHub Desktop.
Home Assistant BluePrint to turn on light, switch, scene or script based on motion and illuminance
blueprint:
name: Turn on light, switch, scene, script or group based on motion and illuminance.
description: >
Turn on a light, switch, scene, script or group based on motion detection, and low light level.
This blueprint uses helper entities you have to create yourself for some input values, to be able to dynamically set limits.
For instructions on creating the helper entities take a look in the Home Assistant Community forum topic:
https://community.home-assistant.io/t/turn-on-light-switch-scene-or-script-based-on-motion-and-illuminance-more-conditions/257085
Required entities:
- Motion sensor (single sensor or group)
- Target entity (light, switch, scene or script)
Optional features:
- You can set a cutoff entity of which the value determines whether the illuminance level is low and the automation needs to trigger.
- You can define a blocking entity, which blocks the automation from running when this entity's state is on.
- You van define a turn-off blocking entity, which blocks the entity from turning off after the set delay.
- Time limits can also be defined to limit the time before and after the automation should trigger.
- If you want the entity to turn off after a certain amount of seconds, you can use the Wait Time input.
- If you do not enable the optional entities the automation will skip these conditions.
- You can define a timeframe where the light will turn on on the lowest possible brightness setting.
domain: automation
input:
motion_sensor:
name: Motion Sensor
description: This sensor will trigger the turning on of the target entity.
selector:
entity:
target_entity:
name: Target entity.
description: The light, switch, scene to turn on (or script to run) when the automation is triggered.
selector:
entity:
illuminance_sensor:
name: (OPTIONAL) Illuminance sensor
description: This sensor will be used to determine the illumination.
default:
selector:
entity:
domain: sensor
device_class: illuminance
illuminance_cutoff:
name: Illuminance cutoff value
description: This input_number will be used to compare to the current illumination to determine if it is low.
default: 999
selector:
number:
min: 0
max: 999
blocker_entity:
name: (OPTIONAL) Blocking entity
description: If this entity's state is on, it will prevent the automation from running. E.g. sleepmode or away mode.
default:
selector:
entity:
time_limit_after:
name: (OPTIONAL) Only run after time.
description: Automation will only run when time is later than this input_datetime value.
default:
selector:
time: {}
time_limit_before:
name: (OPTIONAL) Only run before time.
description: Automation will only run when time is earlier than this input_datetime value.
default:
selector:
time: {}
no_motion_wait:
name: (OPTIONAL) Turn off wait time (seconds)
description: Time in seconds to leave the target entity on after last motion is detected. If not used entity will not auto turn off.
default:
selector:
number:
min: 0
max: 3600
time_power_after:
name: (OPTIONAL) Lowest light setting after time.
description: Automation will turn on the light on the lowest setting after the given time.
default:
selector:
time: {}
time_power_before:
name: (OPTIONAL) Lowest light setting before time.
description: Automation will turn on the light on the lowest setting before the given time.
default:
selector:
time: {}
mode: restart
max_exceeded: silent
variables:
no_motion_wait: !input no_motion_wait
time_limit_before: !input time_limit_before
time_limit_after: !input time_limit_after
time_power_before: !input time_power_before
time_power_after: !input time_power_after
trigger:
platform: state
entity_id: !input motion_sensor
to: 'on'
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: and
conditions:
- condition: state
entity_id: !input target_entity
state: "off"
- condition: template
value_template: "{{ (blocker_entity == none) or (states[blocker_entity].state == 'off') }}"
- condition: numeric_state
entity_id: !input illuminance_sensor
below: !input illuminance_cutoff
- condition: template
value_template: >
{% set current_time = now().strftime("%H:%M") %}
{% if time_limit_before != none and time_limit_after == none %}
{{ states[time_limit_before].state > current_time }}
{% elif time_limit_before == none and time_limit_after != none %}
{{ states[time_limit_after].state < current_time }}
{% elif time_limit_before != none and time_limit_after != none %}
{% set before_limit_is_on_next_day = time_limit_after > time_limit_before %}
{% if not before_limit_is_on_next_day %}
{{ (states[time_limit_after].state < current_time) and (states[time_limit_before].state > current_time) }}
{% elif before_limit_is_on_next_day %}
{{ (states[time_limit_before].state > current_time) or (states[time_limit_after].state < current_time) }}
{% endif %}
{% else %}
true
{% endif %}
action:
- choose:
- conditions:
- condition: template
value_template: >
{% set current_time = now().strftime("%H:%M") %}
{% if time_power_before != none and time_power_after == none %}
{{ states[time_power_before].state > current_time }}
{% elif time_power_before == none and time_power_after != none %}
{{ states[time_power_after].state < current_time }}
{% elif time_power_before != none and time_power_after != none %}
{% set before_power_is_on_next_day = time_power_after > time_power_before %}
{% if not before_power_is_on_next_day %}
{{ (states[time_power_after].state < current_time) and (states[time_power_before].state > current_time) }}
{% elif before_power_is_on_next_day %}
{{ (states[time_power_before].state > current_time) or (states[time_power_after].state < current_time) }}
{% endif %}
{% else %}
true
{% endif %}
sequence:
- service: homeassistant.turn_on
entity_id: !input target_entity
data:
brightness: 100
default:
- service: homeassistant.turn_on
entity_id: !input target_entity
data:
brightness: 1
- condition: template
value_template: "{{ no_motion_wait != none }}"
- wait_for_trigger:
platform: state
entity_id: !input motion_sensor
from: "on"
to: "off"
- delay:
seconds: '{{ states[no_motion_wait].state | int }}'
- 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