Skip to content

Instantly share code, notes, and snippets.

@alexdetsch
Last active February 26, 2026 14:30
Show Gist options
  • Select an option

  • Save alexdetsch/486d5e4d96aa53350cc37aea19b933d0 to your computer and use it in GitHub Desktop.

Select an option

Save alexdetsch/486d5e4d96aa53350cc37aea19b933d0 to your computer and use it in GitHub Desktop.
Homeassistant Blueprint Advanced Motion activated light
blueprint:
name: Advanced Motion-Activated Lighting
description: Turn lights on when motion is detected and off after a delay, with automatic vs manual control detection, conditions, and advanced features.
domain: automation
source_url: https://gist.github.com/alexdetsch/486d5e4d96aa53350cc37aea19b933d0
input:
motion_sensors:
name: Motion Sensors
description: Motion sensors that trigger the lights
selector:
entity:
filter:
- domain: binary_sensor
device_class: motion
multiple: true
target_lights:
name: Target Lights
description: Lights to control
selector:
target:
entity:
- domain: light
# Timing
no_motion_wait:
name: Wait Time
description: Time to wait after last motion before turning off lights
default: 120
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds
mode: slider
# Turn ON conditions
turn_on_conditions:
name: Turn ON Required Conditions
description: Additional conditions that must be true for lights to turn ON (e.g., someone home, not in sleep mode)
default: []
selector:
entity:
multiple: true
# Turn OFF conditions
turn_off_conditions:
name: Turn OFF Required Conditions
description: Additional conditions that must be true for lights to turn OFF (e.g., no one in shower)
default: []
selector:
entity:
multiple: true
# Illuminance
illuminance_sensor:
name: Illuminance Sensor (Optional)
description: Only turn on lights if below illuminance threshold or if state matches one of the trigger states
default: []
selector:
entity:
multiple: false
illuminance_threshold:
name: Illuminance Threshold
description: Lights will only turn on if illuminance is below this value (for numeric sensors)
default: 100
selector:
number:
min: 0
max: 1000
unit_of_measurement: lux
illuminance_trigger_states:
name: Illuminance Trigger States (Optional)
description: For text-based sensors (e.g., "dim", "dark"), enter states when lights should turn on (comma-separated, case-insensitive)
default: "dim,dark"
selector:
text:
# Blocking entities
blocking_entities:
name: Blocking Entities (Optional)
description: Automation won't run when these entities are on/true (e.g., movie mode, manual override)
default: []
selector:
entity:
multiple: true
# Time restrictions
time_after:
name: Only After Time (Optional)
description: Automation only runs after this time
default: "00:00:00"
selector:
time:
time_before:
name: Only Before Time (Optional)
description: Automation only runs before this time
default: "00:00:00"
selector:
time:
# Night mode
night_mode_enabled:
name: Enable Night Mode
description: Use different brightness during night hours
default: false
selector:
boolean:
night_mode_start:
name: Night Mode Start Time
description: When to switch to night mode brightness
default: "22:00:00"
selector:
time:
night_mode_end:
name: Night Mode End Time
description: When to end night mode brightness
default: "06:00:00"
selector:
time:
night_mode_brightness:
name: Night Mode Brightness
description: Brightness percentage during night mode
default: 20
selector:
number:
min: 1
max: 100
unit_of_measurement: "%"
day_brightness:
name: Day Brightness
description: Brightness percentage during normal hours (0 = keep current brightness)
default: 0
selector:
number:
min: 0
max: 100
unit_of_measurement: "%"
# Transition
turn_on_transition:
name: Turn On Transition
description: Transition time when turning lights on
default: 0
selector:
number:
min: 0
max: 60
unit_of_measurement: seconds
turn_off_transition:
name: Turn Off Transition
description: Transition time when turning lights off
default: 5
selector:
number:
min: 0
max: 60
unit_of_measurement: seconds
mode: restart
max_exceeded: silent
variables:
target_lights: !input target_lights
illuminance_sensor: !input illuminance_sensor
illuminance_threshold: !input illuminance_threshold
illuminance_trigger_states: !input illuminance_trigger_states
blocking_entities: !input blocking_entities
turn_on_conditions: !input turn_on_conditions
turn_off_conditions: !input turn_off_conditions
time_after: !input time_after
time_before: !input time_before
night_mode_enabled: !input night_mode_enabled
night_mode_start: !input night_mode_start
night_mode_end: !input night_mode_end
night_mode_brightness: !input night_mode_brightness
day_brightness: !input day_brightness
turn_on_transition: !input turn_on_transition
turn_off_transition: !input turn_off_transition
automation_id: "{{ this.entity_id }}"
trigger:
- platform: state
entity_id: !input motion_sensors
to: "on"
id: "motion_detected"
- platform: state
entity_id: !input motion_sensors
to: "off"
for: !input no_motion_wait
id: "motion_cleared"
condition:
# Check blocking entities
- condition: template
value_template: >
{% set blocked = namespace(value=false) %}
{% if blocking_entities | length > 0 %}
{% for entity in blocking_entities %}
{% if is_state(entity, 'on') or is_state(entity, 'true') %}
{% set blocked.value = true %}
{% endif %}
{% endfor %}
{% endif %}
{{ not blocked.value }}
# Check time window if specified
- condition: template
value_template: >
{% if time_after == '00:00:00' and time_before == '00:00:00' %}
true
{% else %}
{{ time_after != time_before and now().time() >= (time_after | as_datetime | default(now(), true) | as_local | default(now(), true)).time() and now().time() < (time_before | as_datetime | default(now(), true) | as_local | default(now(), true)).time() }}
{% endif %}
action:
- choose:
# Motion detected - turn lights ON
- conditions:
- condition: trigger
id: "motion_detected"
# Check illuminance if sensor provided
- condition: template
value_template: >
{% if illuminance_sensor | length > 0 %}
{% set sensor_state = states(illuminance_sensor) %}
{% set is_numeric = sensor_state | float(default='not_numeric') != 'not_numeric' %}
{% if is_numeric %}
{{ sensor_state | float(1000) < illuminance_threshold }}
{% else %}
{% set trigger_states_list = illuminance_trigger_states.lower().replace(' ', '').split(',') %}
{{ sensor_state.lower() in trigger_states_list }}
{% endif %}
{% else %}
true
{% endif %}
# Check turn_on conditions
- condition: template
value_template: >
{% set conditions_met = namespace(value=true) %}
{% if turn_on_conditions | length > 0 %}
{% for entity in turn_on_conditions %}
{% if not (is_state(entity, 'on') or is_state(entity, 'true') or is_state(entity, 'home')) %}
{% set conditions_met.value = false %}
{% endif %}
{% endfor %}
{% endif %}
{{ conditions_met.value }}
sequence:
# Store that automation turned on the lights
- service: automation.turn_off
data:
entity_id: "{{ automation_id }}"
stop_actions: false
- service: input_boolean.turn_on
continue_on_error: true
target:
entity_id: "{{ 'input_boolean.' ~ automation_id.split('.')[1] ~ '_active' }}"
- service: automation.turn_on
data:
entity_id: "{{ automation_id }}"
# Determine brightness based on night mode
- choose:
- conditions:
- condition: template
value_template: "{{ night_mode_enabled or day_brightness > 0 }}"
sequence:
- variables:
calculated_brightness: >
{% if night_mode_enabled %}
{% set current_time = now().time() %}
{% set night_start = (night_mode_start | as_datetime | default(now(), true) | as_local | default(now(), true)).time() %}
{% set night_end = (night_mode_end | as_datetime | default(now(), true) | as_local | default(now(), true)).time() %}
{% if night_start > night_end %}
{% if current_time >= night_start or current_time < night_end %}
{{ night_mode_brightness }}
{% else %}
{{ day_brightness }}
{% endif %}
{% else %}
{% if current_time >= night_start and current_time < night_end %}
{{ night_mode_brightness }}
{% else %}
{{ day_brightness }}
{% endif %}
{% endif %}
{% else %}
{{ day_brightness }}
{% endif %}
- service: light.turn_on
target: !input target_lights
data:
transition: "{{ turn_on_transition }}"
brightness_pct: "{{ calculated_brightness }}"
default:
- service: light.turn_on
target: !input target_lights
data:
transition: "{{ turn_on_transition }}"
# Motion cleared - turn lights OFF
- conditions:
- condition: trigger
id: "motion_cleared"
# Only turn off if automation turned them on
- condition: template
value_template: "{{ is_state('input_boolean.' ~ automation_id.split('.')[1] ~ '_active', 'on') }}"
# Check turn_off conditions
- condition: template
value_template: >
{% set conditions_met = namespace(value=true) %}
{% if turn_off_conditions | length > 0 %}
{% for entity in turn_off_conditions %}
{% if not (is_state(entity, 'on') or is_state(entity, 'true') or is_state(entity, 'home')) %}
{% set conditions_met.value = false %}
{% endif %}
{% endfor %}
{% endif %}
{{ conditions_met.value }}
sequence:
# Turn off lights
- service: light.turn_off
target: !input target_lights
data:
transition: "{{ turn_off_transition }}"
# Clear automation control flag
- service: input_boolean.turn_off
continue_on_error: true
target:
entity_id: "{{ 'input_boolean.' ~ automation_id.split('.')[1] ~ '_active' }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment