Skip to content

Instantly share code, notes, and snippets.

@mdegat01
Created June 12, 2022 22:50
Show Gist options
  • Save mdegat01/4f1591f9a6952515f5ead7d2250d2832 to your computer and use it in GitHub Desktop.
Save mdegat01/4f1591f9a6952515f5ead7d2250d2832 to your computer and use it in GitHub Desktop.
Presence Lights Controllers
# Presence lights controllers - On on wake up, off on asleep or leave, override based on manual changes to lights
- trigger:
- id: home_change__bedroom
platform: state
entity_id: binary_sensor.awake_at_home
from: ['on', 'off']
to: ['on', 'off']
- id: sleep_change__bedroom
platform: state
entity_id: binary_sensor.person_is_asleep_in_bedroom
from: &sleep-options ['on', 'off', 'unavailable']
to: *sleep-options
- id: light_change__bedroom
platform: state
entity_id:
- light.bedroom_bed_back
- light.bedroom_bed_front
- light.bedroom_vanity_back
- light.bedroom_vanity_front
attribute: brightness
to:
- id: override_timeout__bedroom
platform: time_pattern
minutes: &timeout-check /15
- id: override
platform: event
event_type: entity_manual_override
event_data: &presence-lights-bedroom
entity_id: select.presence_lights_bedroom
- id: restore__bedroom
platform: event
event_type: restore_state
- id: icon
platform: state
<<: *presence-lights-bedroom
to: ['on', 'off']
select:
name: Presence lights bedroom
unique_id: cbe10f398b774fd5a0f13281a248277f_presence_bedroom
options: &presence-options "{{ ['off', 'on', 'override'] }}"
select_option:
event: entity_manual_override
event_data:
<<: *presence-lights-bedroom
option: "{{ option }}"
icon: >-
mdi:motion-sensor{{ '-off' if not is_state('select.presence_lights_bedroom', 'on') }}
state: &presence-lights-state >-
{% set inputs = trigger.id.split('__') %}
{% set this_id = 'select.presence_lights_%s' %}
{% set room_sleep = 'binary_sensor.person_is_asleep_in_%s' %}
{% set id = inputs[0] %}{% set room = inputs[1] if inputs | count > 1 else '' %}
{% if id == 'icon' %}
{{ trigger.to_state.state }}
{% elif id == 'override' %}
{{ trigger.event.data.option }}
{% elif id == 'restore' %}
{{ trigger.event.data.data[this_id % room].state }}
{% elif is_state('binary_sensor.awake_at_home', 'off') or room != '' and is_state(room_sleep % room, 'on') %}
off
{% elif is_state(this_id % room, 'override') %}
{{ 'on' if is_state(this_id % room, 'override') and (
states[this_id % room].last_updated < (now() - timedelta(hours=2)) or
states[this_id % room].last_updated < (now() - timedelta(minutes=30))
and is_state('binary_sensor.person_detected_' ~ room, 'off')
) else states(this_id % room) }}
{% elif id == 'light_change' %}
{% set from = trigger.from_state %}{% set to = trigger.to_state %}
{% set invalid = ['unknown', 'unavailable'] %}
{% if not (not from or from.state in invalid or not to or to.state in invalid) %}
{% set context = trigger.to_state.context %}
{{ 'override' if is_state(this_id % room, 'on') and not (
not context.user_id and context.parent_id
) else states(this_id % room) }}
{% else %}
states(this_id % room)
{% endif %}
{% else %}
on
{% endif %}
- trigger:
- id: home_change__guest_room
platform: state
entity_id: binary_sensor.awake_at_home
from: ['on', 'off']
to: ['on', 'off']
- id: sleep_change__guest_room
platform: state
entity_id: binary_sensor.person_is_asleep_in_guest_room
from: *sleep-options
to: *sleep-options
- id: light_change__guest_room
platform: state
entity_id: light.guest_room
attribute: brightness
to:
- id: override_timeout__guest_room
platform: time_pattern
minutes: *timeout-check
- id: override
platform: event
event_type: entity_manual_override
event_data: &presence-lights-guest-room
entity_id: select.presence_lights_guest_room
- id: restore__guest_room
platform: event
event_type: restore_state
- id: icon
platform: state
<<: *presence-lights-guest-room
to: ['on', 'off']
select:
name: Presence lights guest room
unique_id: 11f361e7dd1d47ab90d6d64b08e0af26_presence_guest_room
options: *presence-options
select_option:
event: entity_manual_override
event_data:
<<: *presence-lights-guest-room
option: "{{ option }}"
icon: >-
mdi:motion-sensor{{ '-off' if not is_state('select.presence_lights_guest_room', 'on') }}
state: *presence-lights-state
- trigger:
- id: home_change__kids_room
platform: state
entity_id: binary_sensor.awake_at_home
from: ['on', 'off']
to: ['on', 'off']
- id: sleep_change__kids_room
platform: state
entity_id: binary_sensor.person_is_asleep_in_kids_room
from: *sleep-options
to: *sleep-options
- id: light_change__kids_room
platform: state
entity_id: light.kids_room
attribute: brightness
to:
- id: override_timeout__kids_room
platform: time_pattern
minutes: *timeout-check
- id: override
platform: event
event_type: entity_manual_override
event_data: &presence-lights-kids-room
entity_id: select.presence_lights_kids_room
- id: restore__kids_room
platform: event
event_type: restore_state
- id: icon
platform: state
<<: *presence-lights-kids-room
to: ['on', 'off']
select:
name: Presence lights kids room
unique_id: 31a6053720ad4d18b7eeb3b06ad43d52_presence_kids_room
options: *presence-options
select_option:
event: entity_manual_override
event_data:
<<: *presence-lights-kids-room
option: "{{ option }}"
icon: >-
mdi:motion-sensor{{ '-off' if not is_state('select.presence_lights_kids_room', 'on') }}
state: *presence-lights-state
# Family Room, Kitchen and Upstairs don't have a sleep sensor, just go on and off with the house
- trigger:
- &state-home-change
id: home_change
platform: state
entity_id: binary_sensor.awake_at_home
from: ['on', 'off']
to: ['on', 'off']
- id: light_change__family_room
platform: state
entity_id:
- light.dining_room
- light.family_room_front
- light.family_room_main
attribute: brightness
to:
- id: override_timeout__family_room
platform: time_pattern
minutes: *timeout-check
- id: override
platform: event
event_type: entity_manual_override
event_data: &presence-lights-family-room
entity_id: select.presence_lights_family_room
- id: restore__family_room
platform: event
event_type: restore_state
- id: icon
platform: state
<<: *presence-lights-family-room
to: ['on', 'off']
select:
name: Presence lights family room
unique_id: 49f13090f7404c4d8e0d60577f0cd527_presence_family_room
options: *presence-options
select_option:
event: entity_manual_override
event_data:
<<: *presence-lights-family-room
option: "{{ option }}"
icon: >-
mdi:motion-sensor{{ '-off' if not is_state('select.presence_lights_family_room', 'on') }}
state: *presence-lights-state
- trigger:
- *state-home-change
- id: light_change__kitchen
platform: state
entity_id: light.kitchen
attribute: brightness
to:
- id: override_timeout__kitchen
platform: time_pattern
minutes: *timeout-check
- id: override
platform: event
event_type: entity_manual_override
event_data: &presence-lights-kitchen
entity_id: select.presence_lights_kitchen
- id: restore__kitchen
platform: event
event_type: restore_state
- id: icon
platform: state
<<: *presence-lights-kitchen
to: ['on', 'off']
select:
name: Presence lights kitchen
unique_id: fa358039f5dd4eeb8bfa9acb73880733_presence_kitchen
options: *presence-options
select_option:
event: entity_manual_override
event_data:
<<: *presence-lights-kitchen
option: "{{ option }}"
icon: >-
mdi:motion-sensor{{ '-off' if not is_state('select.presence_lights_kitchen', 'on') }}
state: *presence-lights-state
- trigger:
- *state-home-change
- id: light_change__upstairs
platform: state
entity_id:
- light.stairs
- light.upstairs_hallway
attribute: brightness
to:
- id: override_timeout__upstairs
platform: time_pattern
minutes: *timeout-check
- id: override
platform: event
event_type: entity_manual_override
event_data: &presence-lights-upstairs
entity_id: select.presence_lights_upstairs
- id: restore__upstairs
platform: event
event_type: restore_state
- id: icon
platform: state
<<: *presence-lights-upstairs
to: ['on', 'off']
select:
name: Presence lights upstairs
unique_id: f40d278a44da4a42b8ba7d71b00535e5_presence_upstairs
options: *presence-options
select_option:
event: entity_manual_override
event_data:
<<: *presence-lights-upstairs
option: "{{ option }}"
icon: >-
mdi:motion-sensor{{ '-off' if not is_state('select.presence_lights_upstairs', 'on') }}
state: *presence-lights-state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment