Skip to content

Instantly share code, notes, and snippets.

@jamesharding
Last active November 4, 2026 23:53
Show Gist options
  • Select an option

  • Save jamesharding/5d994f7c2abe23bd8f28853258b37d27 to your computer and use it in GitHub Desktop.

Select an option

Save jamesharding/5d994f7c2abe23bd8f28853258b37d27 to your computer and use it in GitHub Desktop.
Presence automation blueprint for Home Assistant, using binary sensors, media players, etc.
blueprint:
name: Presence Automations
description: >
Presence-based automation system with day/sunset/night lighting scenes, binary
device control, custom script triggers, configurable timeouts, manual override
detection, and occupancy tracking.
Supports motion sensors (bursty), media players, door sensors, and static presence indicators.
Uses rising-edge detection: lights, devices, and scripts only activate at the moment
the room transitions from unoccupied to occupied. While the room remains occupied,
no adjustments are made — if you manually turn off lights, they stay off until you
leave and return. Requires an Occupied Helper (input_boolean) per room.
Each period (day/sunset/night) has a Scene, a Brightness, a Timeout and an Auto-On
toggle. If a Scene is set for a period it is used; otherwise all lights turn on at
that period's Brightness.
Time transition support: when the period changes (day→sunset, sunset→night) while
the room is occupied and ALL lights are off, lights turn on using the new period's
scene/brightness (if auto-on is enabled for that period). Optionally, lights that
are already on can also be adjusted to the new period (e.g. dim at night).
On the falling edge (room empties after timeout), lights that are on optionally
flash as a warning, then lights and devices turn off and on_no_presence scripts run.
The falling edge only runs if the room was actually occupied.
Pending timeouts survive Home Assistant restarts and automation reloads.
All automated actions (lights, devices, scripts) can be disabled via the
Automations Toggle without affecting occupancy tracking.
domain: automation
author: James Harding
input:
# ===== PRESENCE SENSORS =====
presence_binary_sensors:
name: Binary Presence Sensors
description: >
Motion sensors, door sensors, or any binary_sensor that indicates presence when "on".
selector:
entity:
domain: binary_sensor
multiple: true
default: []
presence_media_players:
name: Media Players
description: >
Media players that indicate presence when actively playing.
Room is considered occupied only when state is "playing".
selector:
entity:
domain: media_player
multiple: true
default: []
area:
name: Room Area (Unused)
description: "Reserved for future use. Has no effect."
selector:
area:
default: ""
# ===== LIGHTS =====
lights:
name: Lights
description: The lights to control in this room.
selector:
entity:
domain: light
multiple: true
default: []
# ===== DEVICES =====
binary_devices:
name: Binary Devices (Optional)
description: >
Switches, smart plugs, fans, or other on/off devices to turn on/off
based on presence. Turned on when the room becomes occupied (rising edge)
and off when the room empties (falling edge).
selector:
entity:
domain:
- switch
- fan
- input_boolean
multiple: true
default: []
# ===== SCRIPTS =====
on_presence_scripts:
name: On-Presence Scripts (Optional)
description: >
Scripts to run when the room first becomes occupied (rising edge).
Only triggered once per occupancy cycle, not on repeated sensor triggers.
selector:
entity:
domain: script
multiple: true
default: []
on_no_presence_scripts:
name: On-No-Presence Scripts (Optional)
description: >
Scripts to run when the room empties (falling edge, after timeout).
Useful for cleanup actions, setting modes, etc.
selector:
entity:
domain: script
multiple: true
default: []
# ===== SCENES =====
sunset_scene:
name: Sunset Scene
description: >
Scene to activate from sunset until night mode begins.
If not set, lights turn on at the Sunset Brightness.
selector:
entity:
domain: scene
default: ""
night_scene:
name: Night Scene
description: >
Scene to activate during night mode (e.g., late evening/sleep time).
If not set, lights turn on at the Night Brightness.
selector:
entity:
domain: scene
default: ""
day_scene:
name: Day Scene (Optional)
description: >
Scene to activate during daytime (before sunset).
If not set, lights turn on at the Day Brightness.
Only used when daytime auto-on is enabled.
selector:
entity:
domain: scene
default: ""
# ===== BRIGHTNESS =====
brightness_day:
name: Day Brightness
description: Brightness used during the day when no Day Scene is set.
default: 100
selector:
number:
min: 1
max: 100
unit_of_measurement: "%"
mode: slider
brightness_sunset:
name: Sunset Brightness
description: Brightness used after sunset when no Sunset Scene is set.
default: 80
selector:
number:
min: 1
max: 100
unit_of_measurement: "%"
mode: slider
brightness_night:
name: Night Brightness
description: Brightness used during night mode when no Night Scene is set.
default: 5
selector:
number:
min: 1
max: 100
unit_of_measurement: "%"
mode: slider
# ===== AUTO-ON =====
day_auto_on:
name: Daytime Auto Turn-On
description: >
When enabled, lights automatically turn on during daytime when presence
is detected (using the Day Scene or Day Brightness).
When disabled, lights will NOT turn on automatically, but will still
auto-turn-off when the room empties.
default: false
selector:
boolean:
sunset_auto_on:
name: Sunset Auto Turn-On
description: >
When enabled, lights automatically turn on after sunset when presence
is detected (using the Sunset Scene or Sunset Brightness).
When disabled, lights will NOT turn on automatically, but will still
auto-turn-off when the room empties.
default: true
selector:
boolean:
night_auto_on:
name: Night Auto Turn-On
description: >
When enabled, lights automatically turn on during night mode when presence
is detected (using the Night Scene or Night Brightness).
When disabled, lights will NOT turn on automatically, but will still
auto-turn-off when the room empties.
default: true
selector:
boolean:
adjust_on_period_change:
name: Adjust Lights That Are On When Period Changes
description: >
When enabled, if the room is occupied and lights are ON when the period
changes (day→sunset, sunset→night), the new period's scene/brightness is
applied (e.g. hallway dims at night). Only lights that are on are adjusted
when using brightness. When disabled, lights that are on are left alone.
default: false
selector:
boolean:
# ===== TURN-OFF WARNING =====
flash_warning:
name: Flash Before Turning Off
description: >
Briefly flash the lights that are on before turning them off, giving you a
grace period to move and cancel the turn-off. Lights that are off are never flashed.
default: true
selector:
boolean:
flash_grace_seconds:
name: Flash Grace Period (seconds)
description: How long to wait after the warning flash before turning off.
default: 60
selector:
number:
min: 5
max: 300
unit_of_measurement: seconds
mode: slider
# ===== HELPERS =====
occupied_helper:
name: Occupied Helper
description: >
An input_boolean to track room occupancy. Required for rising-edge detection —
ensures lights/devices are only turned on at the exact moment the room becomes
occupied and not on subsequent sensor re-triggers. Create one input_boolean per
room (e.g., input_boolean.office_occupied).
selector:
entity:
domain: input_boolean
automations_toggle:
name: Automations Toggle (Optional)
description: >
An input_boolean that enables/disables all automated actions (lights,
devices, and scripts). When "off", nothing turns on/off automatically,
but occupancy tracking still works.
selector:
entity:
domain: input_boolean
default: ""
# ===== TIMEOUTS =====
timeout_day:
name: Daytime Timeout (minutes)
description: >
How long to wait after presence clears before turning off lights (daytime).
default: 10
selector:
number:
min: 1
max: 60
unit_of_measurement: minutes
mode: slider
timeout_sunset:
name: Sunset Timeout (minutes)
description: >
How long to wait after presence clears before turning off lights (sunset period).
default: 10
selector:
number:
min: 1
max: 60
unit_of_measurement: minutes
mode: slider
timeout_night:
name: Night Timeout (minutes)
description: >
How long to wait after presence clears before turning off lights (night period).
default: 5
selector:
number:
min: 1
max: 60
unit_of_measurement: minutes
mode: slider
# ===== TIME SENSORS =====
night_mode_sensor:
name: Night Mode Sensor
description: >
A binary_sensor that is "on" during night mode (e.g., late evening/sleep time).
Night takes priority over sunset, even if the Sunset Sensor is off.
selector:
entity:
domain: binary_sensor
default: binary_sensor.night
sunset_sensor:
name: Sunset Sensor
description: >
A binary_sensor that is "on" after sunset (typically sun.sun based).
selector:
entity:
domain: binary_sensor
default: binary_sensor.sunset
# ===== TRIGGERS =====
# Trigger on state changes of presence entities. `to: ~` ignores attribute-only
# changes (media players update media_position/volume constantly, which would
# otherwise keep restarting the timeout while paused).
trigger:
- platform: state
entity_id: !input presence_binary_sensors
to: ~
id: binary_sensor_change
- platform: state
entity_id: !input presence_media_players
to: ~
id: media_player_change
# Trigger on time period transitions only when sunset/night become active.
- platform: state
entity_id: !input sunset_sensor
to: "on"
id: time_transition
- platform: state
entity_id: !input night_mode_sensor
to: "on"
id: time_transition
# A restart or reload kills any pending timeout delay, which would leave the
# occupied helper (and lights) on indefinitely. Re-evaluate so the timeout restarts.
- platform: homeassistant
event: start
id: resume
- platform: event
event_type: automation_reloaded
id: resume
# ===== VARIABLES =====
variables:
# Input variables
presence_binary_sensors: !input presence_binary_sensors
presence_media_players: !input presence_media_players
lights: !input lights
binary_devices: !input binary_devices
on_presence_scripts: !input on_presence_scripts
on_no_presence_scripts: !input on_no_presence_scripts
sunset_scene: !input sunset_scene
night_scene: !input night_scene
day_scene: !input day_scene
brightness_day: !input brightness_day
brightness_sunset: !input brightness_sunset
brightness_night: !input brightness_night
day_auto_on: !input day_auto_on
sunset_auto_on: !input sunset_auto_on
night_auto_on: !input night_auto_on
adjust_on_period_change: !input adjust_on_period_change
flash_warning: !input flash_warning
flash_grace_seconds: !input flash_grace_seconds
occupied_helper: !input occupied_helper
automations_toggle: !input automations_toggle
timeout_day: !input timeout_day
timeout_sunset: !input timeout_sunset
timeout_night: !input timeout_night
night_mode_sensor: !input night_mode_sensor
sunset_sensor: !input sunset_sensor
# Transition times (in seconds)
transition_day: 1
transition_sunset: 2
transition_night: 3
# ===== CONDITIONS =====
condition: []
# ===== ACTIONS =====
action:
# Calculate current presence state (evaluated fresh each time)
- variables:
check_binary_presence: >
{{ presence_binary_sensors | default([]) | select('is_state', 'on') | list | length > 0 }}
check_media_presence: >
{{ presence_media_players | default([]) | select('is_state', 'playing') | list | length > 0 }}
presence_active: >
{{ check_binary_presence or check_media_presence }}
automation_enabled: >
{{ automations_toggle == '' or automations_toggle is none or is_state(automations_toggle, 'on') }}
# Time period detection (night takes priority):
# night = night mode active
# sunset = sun has set, night mode not active
# day = otherwise
is_night: >
{{ is_state(night_mode_sensor, 'on') }}
is_sunset: >
{{ not is_night and is_state(sunset_sensor, 'on') }}
is_day: >
{{ not is_night and not is_sunset }}
has_lights: >
{{ lights | default([]) | length > 0 }}
has_devices: >
{{ binary_devices | default([]) | length > 0 }}
has_on_presence_scripts: >
{{ on_presence_scripts | default([]) | length > 0 }}
has_on_no_presence_scripts: >
{{ on_no_presence_scripts | default([]) | length > 0 }}
# Rising-edge detection: was the room already occupied before this trigger?
was_already_occupied: >
{{ is_state(occupied_helper, 'on') }}
timeout_minutes: >
{% if is_night %}{{ timeout_night }}
{% elif is_sunset %}{{ timeout_sunset }}
{% else %}{{ timeout_day }}
{% endif %}
turn_on_enabled: >
{% if is_night %}{{ night_auto_on }}
{% elif is_sunset %}{{ sunset_auto_on }}
{% else %}{{ day_auto_on }}
{% endif %}
lights_on: >
{{ lights | default([]) | select('is_state', 'on') | list }}
all_lights_off: >
{{ lights_on | length == 0 }}
# Resolved scene/brightness/transition for the current time period
target_scene: >
{% if is_night %}{{ night_scene }}
{% elif is_sunset %}{{ sunset_scene }}
{% else %}{{ day_scene }}
{% endif %}
has_target_scene: >
{{ target_scene is not none and target_scene | string | trim != '' }}
target_brightness: >
{% if is_night %}{{ brightness_night }}
{% elif is_sunset %}{{ brightness_sunset }}
{% else %}{{ brightness_day }}
{% endif %}
target_transition: >
{% if is_night %}{{ transition_night }}
{% elif is_sunset %}{{ transition_sunset }}
{% else %}{{ transition_day }}
{% endif %}
# =====================
# TIME PERIOD TRANSITION (pass-through, runs before main logic)
# =====================
# When the period changes and the room is occupied:
# - all lights off + auto-on enabled → turn on for the new period
# - some lights on + adjust_on_period_change → re-apply the new period
# Execution always continues to the main choose block below.
- if:
- condition: trigger
id: time_transition
- condition: template
value_template: "{{ was_already_occupied and automation_enabled and has_lights }}"
- condition: template
value_template: >
{{ (all_lights_off and turn_on_enabled)
or (not all_lights_off and adjust_on_period_change) }}
then:
- if:
- condition: template
value_template: "{{ has_target_scene }}"
then:
- service: scene.turn_on
target:
entity_id: "{{ target_scene }}"
data:
transition: "{{ target_transition }}"
else:
- service: light.turn_on
target:
# Adjust only lights that are on; if all are off, turn them all on
entity_id: "{{ lights if all_lights_off else lights_on }}"
data:
brightness_pct: "{{ target_brightness }}"
transition: "{{ target_transition }}"
# =====================
# MAIN PRESENCE LOGIC
# =====================
- choose:
# =====================
# PRESENCE DETECTED (rising edge)
# =====================
- conditions:
- condition: template
value_template: "{{ presence_active }}"
sequence:
- service: input_boolean.turn_on
target:
entity_id: "{{ occupied_helper }}"
# Rising edge: only act on the transition from unoccupied → occupied
- condition: template
value_template: "{{ not was_already_occupied }}"
- condition: template
value_template: "{{ automation_enabled }}"
- if:
- condition: template
value_template: "{{ has_devices }}"
then:
- service: homeassistant.turn_on
target:
entity_id: "{{ binary_devices }}"
- if:
- condition: template
value_template: "{{ has_on_presence_scripts }}"
then:
- service: script.turn_on
target:
entity_id: "{{ on_presence_scripts }}"
- condition: template
value_template: "{{ has_lights and turn_on_enabled }}"
# Only turn on lights if ALL lights are currently off
# (don't override manually controlled lights)
- condition: template
value_template: "{{ all_lights_off }}"
- if:
- condition: template
value_template: "{{ has_target_scene }}"
then:
- service: scene.turn_on
target:
entity_id: "{{ target_scene }}"
data:
transition: "{{ target_transition }}"
else:
- service: light.turn_on
target:
entity_id: "{{ lights }}"
data:
brightness_pct: "{{ target_brightness }}"
transition: "{{ target_transition }}"
# =====================
# PRESENCE CLEARED (falling edge)
# =====================
- conditions:
- condition: template
value_template: "{{ not presence_active }}"
sequence:
# Only run the turn-off cycle if the room was actually occupied.
# Without this, any sensor change in an empty room (door closing,
# sensor going unavailable, period change) would flash and switch
# off lights and re-run the no-presence scripts.
- condition: template
value_template: "{{ was_already_occupied }}"
# mode: restart cancels this delay if presence is detected again
- delay:
minutes: "{{ timeout_minutes | int }}"
- if:
- condition: template
value_template: "{{ automation_enabled }}"
then:
- if:
- condition: template
value_template: "{{ has_lights }}"
then:
# Re-evaluate after the delay; only flash lights that are on.
# (Flashing an off light turns it on at its last brightness.)
- variables:
lights_on_now: "{{ lights | select('is_state', 'on') | list }}"
- if:
- condition: template
value_template: "{{ flash_warning and lights_on_now | length > 0 }}"
then:
- service: light.turn_on
target:
entity_id: "{{ lights_on_now }}"
data:
flash: short
- delay:
seconds: "{{ flash_grace_seconds | int }}"
- service: light.turn_off
target:
entity_id: "{{ lights }}"
data:
transition: "{{ target_transition }}"
- if:
- condition: template
value_template: "{{ has_devices }}"
then:
- service: homeassistant.turn_off
target:
entity_id: "{{ binary_devices }}"
- if:
- condition: template
value_template: "{{ has_on_no_presence_scripts }}"
then:
- service: script.turn_on
target:
entity_id: "{{ on_no_presence_scripts }}"
- service: input_boolean.turn_off
target:
entity_id: "{{ occupied_helper }}"
# Restart mode ensures bursty sensors work correctly:
# - New motion detection restarts the automation, preventing premature turn-off
# - Handles rapid on/off transitions gracefully
mode: restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment