Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WizBangCrash/3d0df7c248e49ea921b0f20921ee0683 to your computer and use it in GitHub Desktop.
Save WizBangCrash/3d0df7c248e49ea921b0f20921ee0683 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Philips Dimmer Switch + Occupancy Sensor
blueprint:
name: Philips Dimmer Switch + Occupancy Sensor
description: >
Single press On turns on the light(s) for 60mins & enables occupancy killswitch.
Single press Off turns off the light(s) & disables occupancy killswitch.
Light(s) can be dimmed via up/down buttons.
Occupancy detection turns on/off light(s) if killswitch disabled.
Changing light state with HomeKit has same effect as switch.
domain: automation
input:
zigbee_device:
name: (OPTIONAL) Philips RWL021 switch
description: The dimmer switch to use for this automation
default: none
selector:
device:
integration: zha
manufacturer: Philips
model: RWL021
light_target:
name: Lights
description: >
The light(s) to control.
Note: Only use the Entity picker (not Area or Device)!
selector:
target:
entity:
domain: light
main_light_off_attributes:
name: Main Light Off Attributes
description: "Set the attributes to use when turning off the light"
selector:
object:
default:
transition: 5
illuminance_entity_id:
name: Illuminance
description: "The luminance sensor to use"
selector:
entity:
domain: sensor
device_class: illuminance
luminance_value:
name: Light trigger level
description: "Anything darker than this will turn on the light"
default: 6
selector:
number:
min: 2
max: 3000
mode: box
unit_of_measurement: "lx"
occupancy_entity_id:
name: Occupancy Sensor
description: "The occupancy sensor for the room"
selector:
entity:
domain: binary_sensor
device_class: occupancy
occupancy_killswitch:
name: Occupancy Sensor Killswitch
description: >
If the input_boolean is in the 'on' state
then the occupancy sensor will be disabled
default: "none"
selector:
entity:
domain: input_boolean
timer_entity_id:
name: Timer
description: The timer to use for turning off the light
selector:
entity:
domain: timer
#
# The automation
#
variables:
lights: !input light_target
illuminance_entity_id: !input illuminance_entity_id
luminance_value: !input luminance_value
occupancy_entity_id: !input occupancy_entity_id
occupancy_killswitch: !input occupancy_killswitch
valid_button_press:
- "on_press"
- "off_press"
- "up_press"
- "up_hold"
- "down_press"
- "down_hold"
trigger_reason: >
{% if trigger.platform == 'event' and trigger.event.event_type == 'zha_event' and trigger.event.data.command in valid_button_press %}
{{ trigger.event.data.command }}
{% elif trigger.platform == 'event' and trigger.event.event_type == 'timer' %}
{{ 'timer' }}
{% elif trigger.platform == 'event' and trigger.event.event_type == 'homekit_state_change' and trigger.event.data.entity_id in lights.entity_id %}
{{ 'homekit' }}
{% elif trigger.platform == 'event' and trigger.event.event_type == 'call_service' %}
{{ 'all_lights' }}
{% elif trigger.platform == 'state' and trigger.entity_id == occupancy_entity_id %}
{{ 'occupancy' }}
{% else %}
{{ 'ignore' }}
{% endif %}
timer_duration: "01:00:00"
timer_entity_id: !input timer_entity_id
# Trigger mode
mode: single
# Trigger
trigger:
- platform: event
event_type: zha_event
event_data:
device_id: !input zigbee_device
- platform: state
entity_id: !input occupancy_entity_id
- platform: event
event_type: timer.finished
event_data:
entity_id: !input timer_entity_id
- platform: event
event_type: "homekit_state_change"
- platform: event
event_type: call_service
event_data:
domain: light
service_data:
entity_id: all
# Conditions
condition:
- "{{ trigger_reason != 'ignore' }}"
# Actions
action:
- alias: "What triggered the automation?"
choose:
- alias: "Turn on lights & enable kill switch?"
conditions:
- condition: or
conditions:
- "{{ trigger_reason == 'on_press' }}"
- "{{ trigger_reason == 'all_lights' and trigger.event.data.service == 'turn_on' }}"
- "{{ trigger_reason == 'homekit' and trigger.event.data.service == 'turn_on' }}"
sequence:
- alias: "Enable occupancy killswitch"
service: input_boolean.turn_on
entity_id: !input occupancy_killswitch
- alias: "Start the timer"
service: timer.start
entity_id: !input timer_entity_id
data:
duration: "{{ timer_duration }}"
- alias: "Do we need to turn on the light(s)"
condition: template
value_template: "{{ trigger_reason not in ['homekit', 'all_lights'] }}"
- alias: "Turn on light"
service: light.turn_on
target: !input light_target
- alias: "Turn off lights & disable kill switch?"
conditions:
- condition: or
conditions:
- "{{ trigger_reason == 'off_press' }}"
- "{{ trigger_reason == 'all_lights' and trigger.event.data.service == 'turn_off' }}"
- "{{ trigger_reason == 'homekit' and trigger.event.data.service == 'turn_off' }}"
- "{{ trigger_reason == 'timer' }}"
sequence:
- alias: "Cancel timer"
service: timer.cancel
entity_id: !input timer_entity_id
- alias: "Disable occupancy killswitch"
service: input_boolean.turn_off
entity_id: !input occupancy_killswitch
- alias: "Do we need to turn off the light(s)"
condition: template
value_template: "{{ trigger_reason not in ['homekit', 'all_lights'] }}"
- alias: "Turn off light(s)"
service: light.turn_off
target: !input light_target
data: !input main_light_off_attributes
- alias: "Is room occupied?"
conditions:
- "{{ trigger_reason == 'occupancy' and trigger.to_state.state == 'on' }}"
sequence:
- choose:
- alias: "Timer running & killswitch active?"
conditions:
- "{{ is_state(occupancy_killswitch, 'on') }}"
- "{{ is_state(timer_entity_id, 'active') }}"
sequence:
- alias: "Restart the timer"
service: timer.start
entity_id: !input timer_entity_id
data:
duration: "{{ timer_duration }}"
default: []
- choose:
- alias: "Dark enough for light(s)?"
conditions:
- "{{ is_state(occupancy_killswitch, 'off') }}"
- "{{ states(illuminance_entity_id)|int < luminance_value }}"
sequence:
- alias: "Turn on the light(s)"
service: light.turn_on
target: !input light_target
default: []
- alias: "Is room unoccupied?"
conditions:
- "{{ trigger_reason == 'occupancy' and trigger.to_state.state == 'off' }}"
- "{{ is_state(occupancy_killswitch, 'off') }}"
sequence:
- alias: "Turn off lights"
service: light.turn_off
target: !input light_target
data: !input main_light_off_attributes
- alias: "Do we need to turn up the light(s)?"
conditions:
- "{{ trigger_reason in ['up_press', 'up_hold'] }}"
sequence:
- alias: "Brighten lights"
service: light.turn_on
target: !input light_target
data:
brightness_step_pct: 10
- alias: "Do we need to turn down the light(s)?"
conditions:
- "{{ trigger_reason in ['down_press', 'down_hold'] }}"
sequence:
- alias: "Dim the lights"
service: light.turn_on
target: !input light_target
data:
brightness_step_pct: -10
default: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment