Skip to content

Instantly share code, notes, and snippets.

@tomoinn
Last active December 27, 2023 17:41
Show Gist options
  • Save tomoinn/8be33ad4418a85998c7fb43264538e28 to your computer and use it in GitHub Desktop.
Save tomoinn/8be33ad4418a85998c7fb43264538e28 to your computer and use it in GitHub Desktop.
blueprint:
domain: automation
name: Switch scenes with Philips Hue Dimmer Switch (RWL022 or RWL021)
description: >
Control custom scenes within HA in the same way the Hue dimmer switches work for
native Philips Hue scenes. If lights are on then the power button will cycle through
scenes, if they're off it'll try to start the most recently used scene. Off button
will turn off all lights in the most recently used scene's entity list, as will the
brightness up / down control.
Double and triple clicks on the on and off buttons can optionally turn on and off selected
sets of smart plugs and lights directly.
input:
zha_device_21:
name: Dimmer switch
description: >
Switch to use to control scenes (RWL021), use either this or the RWL022 entry below.
If both are specified this will be used in preference. At least one must be provided
or the automation won't do anything.
selector:
device:
filter:
integration: zha
model: RWL021
entity:
- domain: sensor
device_class: battery
multiple: false
default: null
zha_device_22:
name: Dimmer switch
description: Switch to use to control scenes (RWL022)
selector:
device:
filter:
integration: zha
model: RWL022
entity:
- domain: sensor
device_class: battery
multiple: false
default: null
area:
name: Area to control
description: Scenes will be discovered from this area
selector:
area:
multiple: false
prefix:
name: Scene name prefix
description: Only scenes in this area matching this prefix will be used
selector:
text:
multiline: false
prefix: 'scene.'
type: text
default: ''
transition_time:
name: Transition time
description: Time to move between scenes and switch on or off in seconds
selector:
number:
min: 0
max: 10
step: 0.1
unit_of_measurement: "seconds"
mode: "slider"
default: 1.0
double_press_devices:
name: Double press devices
description: >
Specific lights and switches to control with
double presses of the on and off buttons
selector:
target:
entity:
- domain: light
- domain: switch
integration: tasmota
default: [ ]
triple_press_devices:
name: Triple press devices
description: >
Specific lights and tasmota smart-plugs to control with triple
presses of the on and off buttons
selector:
target:
entity:
- domain: light
- domain: switch
integration: tasmota
default: [ ]
source_url: https://gist.github.com/tomoinn/8be33ad4418a85998c7fb43264538e28
mode: queued
max_exceeded: silent
variables:
device_id_21: !input zha_device_21
device_id_22: !input zha_device_22
device_id: >
{% if device_id_21 is defined and device_id_21!=None %}
{{ device_id_21 }}
{% else %}
{{ device_id_22 }}
{% endif %}
trigger:
- platform: event
event_type: zha_event
condition: '{{ trigger.event.data.device_id == device_id }}'
action:
- variables:
prefix: !input prefix
area: !input area
command_type: '{{ trigger.event.data.command }}'
double_press_devices: !input double_press_devices
triple_press_devices: !input triple_press_devices
all_scenes: >
{{ states.scene |
selectattr('entity_id', 'in', area_entities(area)) |
selectattr('entity_id','match','^scene.'+prefix) |
map(attribute='entity_id') | list }}
unknown_scenes: >
{{ expand(all_scenes) |
selectattr('state','eq','unknown') |
map(attribute='entity_id') | list }}
known_scenes: >
{{ expand(all_scenes) |
rejectattr('state','eq','unknown') |
sort(attribute='state', reverse=true) |
map(attribute='entity_id') | list }}
ordered_scenes: '{{ known_scenes + unknown_scenes }}'
current_scene: >
{% if known_scenes | count == 0 %}
{{ '' }}
{% else %}
{{ ordered_scenes[0] }}
{% endif %}
next_scene: >
{% if known_scenes | count == 0 %}
{% if ordered_scenes | count > 0 %}
{{ ordered_scenes[0] }}
{% else %}
{{ '' }}
{% endif %}
{% else %}
{{ ordered_scenes[ordered_scenes | count -1] }}
{% endif %}
lights: >
{% if current_scene == '' %}
{{ [] }}
{% else %}
{{ state_attr(current_scene, 'entity_id') | expand | map(attribute='entity_id') | list }}
{% endif %}
lights_on: >
{{ lights | expand | selectattr('state','eq','on') | map(attribute='state') | list | count > 0 }}
transition_time: !input transition_time
- service: logbook.log
data:
name: all_scenes
message: >
Ordered scenes : {{ ordered_scenes }}
Current scene: {{ current_scene }}
Next scene: {{ next_scene }}
Lights: {{ lights }}
Lights on: {{ lights_on }}
- choose:
- conditions: '{{ command_type == "on_double_press" }}'
sequence:
- service: light.turn_on
target: '{{ double_press_devices }}'
data:
transition: '{{ transition_time }}'
- service: switch.turn_on
target: '{{ double_press_devices }}'
- conditions: '{{ command_type == "off_double_press" }}'
sequence:
- service: light.turn_off
target: '{{ double_press_devices }}'
data:
transition: '{{ transition_time }}'
- service: switch.turn_off
target: '{{ double_press_devices }}'
- conditions: '{{ command_type == "on_triple_press" }}'
sequence:
- service: light.turn_on
target: '{{ triple_press_devices }}'
data:
transition: '{{ transition_time }}'
- service: switch.turn_on
target: '{{ triple_press_devices }}'
- conditions: '{{ command_type == "off_triple_press" }}'
sequence:
- service: light.turn_off
target: '{{ triple_press_devices }}'
data:
transition: '{{ transition_time }}'
- service: switch.turn_off
target: '{{ triple_press_devices }}'
- conditions: '{{ command_type == "on_press" and lights_on }}'
sequence:
- service: scene.turn_on
data:
transition: '{{ transition_time }}'
target:
entity_id: '{{ next_scene }}'
- conditions: '{{ command_type == "on_press" and lights_on == false and current_scene != "" }}'
sequence:
- service: scene.turn_on
data:
transition: '{{ transition_time }}'
target:
entity_id: '{{ current_scene }}'
- conditions: '{{ command_type == "off_press" and lights_on }}'
sequence:
- service: light.turn_off
target:
entity_id: '{{ lights }}'
data:
transition: '{{ transition_time }}'
- service: switch.turn_off
data:
entity_id: '{{ lights }}'
- conditions: '{{ command_type == "down_hold" or command_type == "down_short_release" }}'
sequence:
- service: light.turn_on
data:
brightness_step_pct: -15
transition: 0.3
target:
entity_id: '{{ lights }}'
- conditions: '{{ command_type == "up_hold" or command_type == "up_short_release" }}'
sequence:
- service: light.turn_on
data:
brightness_step_pct: 15
transition: 0.3
target:
entity_id: '{{ lights }}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment