А newbie question.
Today got my Aqara knob H1 (wireless) Xiaomi ZNXNKG02LM control via MQTT | Zigbee2MQTT, connect it thru zigbee stick to my HA, and want make automation for control brightness and color temp for my main light - aqara l350 led lamp, but I can’t manage to adjust the brightness control by turning the dimmer knob left and right.
I’m really bad in YAML code and try do it from GUI.
Maybe someone has experience setting up such automation for this switch ?
So I managed to collect some data on actions:
action_rotation_angle: can change with step 12 plus or minus directions
action_rotation_percent: can change with step 3.3 plus or minus directions
seems like need come examples with similar actions
Thank you for this, just what I needed to point me in the right direction. I made a modification and find this works better (for me anyway) as it takes into account the rotation amount and speed:
# Office Dimmer Switch actions
- id: 51766397-B94F-4DC2-9C64-E75BD0C70C71
alias: "Buttons and Switches: Office Dimmer Switch actions"
mode: single
max_exceeded: silent # Do not warn in log if already running
trigger:
- platform: state
entity_id: sensor.office_dimmer_switch_action
to:
- start_rotating
- rotation
action:
- service: light.turn_on
target:
entity_id: light.office_ceiling_light
data:
# Use angle and speed to determine increment
brightness_step_pct: >-
{%- if ((trigger.to_state.attributes.action_rotation_angle_speed | int / 24) | abs) < 1 -%}
{{ trigger.to_state.attributes.action_rotation_angle | int / 12 }}
{%- else -%}
{{ (trigger.to_state.attributes.action_rotation_angle | int / 12) * ((trigger.to_state.attributes.action_rotation_angle_speed | int / 24) | abs) }}
{%- endif -%}
Yes, it is slower unless you turn the knob quickly. That was the point really, that it takes into account the speed of the turning too, so you can vary the speed based on how fast you turn it. You can obviously tweak the values to increase or decrease the sensitivity.
I have actually incorporated this into a fairly extensive automation that gives control over multiple lights and allows you to cycle through scenes as well. I’ll share the code later, when I have a bit more time.
For context, I am using this automation with a Philips Hue Centris 2-Spot Ceiling Light in my office, which has individually controllable lights. However, this can be used with any set of more than one light e.g. a single ceiling light and a couple of table or floor lamps. The automation allows you to select the light that you are controlling and gives visual feedback by flashing it, so you know what is selected.
A double-click within three seconds of the last double-click will cycle through the lights that you can dim. If you double-click outside the three second period, it will simply flash the light that is selected to let you know which one you are about to control. A hold will switch it to scene selection, so that when you turn the knob it will cycle through the scenes of your choice in the order of your choosing and back in the opposite order if you turn the knob in the opposite direction. A double-click will switch it back to dimming mode and a single-click functions as a simple on/off toggle for the light selected by the double-click.
The idea here is to basically have a single dimmer for multiple lights, rather than needing to purchase multiple dimmers to control each light individually. I came up with the idea of adding scene scrolling after adding the multiple light dimming functions as I was trying to think of a use for the hold option.
To use the automation you will need to create some Helpers:
Office Dimmer Switch Double-Click | input_datetime.office_dimmer_switch_double_click | Date and Time | mdi:calendar-clock
Office Dimmer Switch Hold | input_datetime.office_dimmer_switch_hold | Date and Time | mdi:calendar-clock
Office Dimmer Switch Scene | input_text.office_dimmer_switch_scene | Text | Maximum Length 20 | mdi:panorama-outline
Office Dimmer Switch Target | input_text.office_dimmer_switch.target | Text | Maximum Length 50 | mdi:target
Change the names to suit your lights and replace the entity_ids in the automation. Icons are optional. I am just detailing them here, so you can reproduce it exactly if you want to.
And here’s the automation:
# Office Dimmer Switch actions
- id: 56C46CA4-2F47-48AC-93AD-9DA17BBB2FB0
alias: "Buttons and Switches: Office Dimmer Switch actions"
mode: single
max_exceeded: silent # Do not warn in log if already running
trigger:
# Single-Click
- platform: state
entity_id: sensor.office_dimmer_switch_action
to: single
id: single_click
# Double-Click
- platform: state
entity_id: sensor.office_dimmer_switch_action
to: double
id: double_click
# Hold
- platform: state
entity_id: sensor.office_dimmer_switch_action
to: hold
id: hold
# Rotating
- platform: state
entity_id: sensor.office_dimmer_switch_action
to:
- start_rotating
- rotation
id: rotation
action:
- choose:
# Single-Click
- conditions:
- condition: trigger
id: single_click
sequence:
- service: light.toggle
target:
entity_id: "{{ states('input_text.office_dimmer_switch_target') }}"
# Double-Click
- conditions:
- condition: trigger
id: double_click
sequence:
- if:
# Check the last double-click was at least 3 seconds ago
- condition: template
value_template: "{{ as_timestamp(now()) - as_timestamp(states('input_datetime.office_dimmer_switch_double_click')) > 3 }}"
then:
# Flash the target lights
- service: light.toggle
target:
entity_id: "{{ states('input_text.office_dimmer_switch_target') }}"
data:
flash: short
else:
- choose:
# Office Ceiling Light to Office Ceiling Downlight
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_target
state: light.office_ceiling_light
sequence:
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_target
data:
value: light.office_ceiling_downlight
# Office Ceiling Downlight to Office Ceiling Spotlights
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_target
state: light.office_ceiling_downlight
sequence:
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_target
data:
value: light.office_ceiling_spotlights
# Office Ceiling Spotlights to Office Ceiling Light
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_target
state: light.office_ceiling_spotlights
sequence:
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_target
data:
value: light.office_ceiling_light
# Flash the target lights
- service: light.toggle
target:
entity_id: "{{ states('input_text.office_dimmer_switch_target') }}"
data:
flash: short
# Update the last double-click date and time
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.office_dimmer_switch_double_click
data:
timestamp: "{{ now().timestamp() }}"
# Hold
- conditions:
- condition: trigger
id: hold
sequence:
# Set the target lights
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_target
data:
value: light.office_ceiling_light
# Flash the target lights
- service: light.toggle
target:
entity_id: "{{ states('input_text.office_dimmer_switch_target') }}"
data:
flash: short
# Update the last hold date and time
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.office_dimmer_switch_hold
data:
timestamp: "{{ now().timestamp() }}"
# Rotation
- conditions:
- condition: trigger
id: rotation
sequence:
- if:
# Check the last hold was after the last double-click
- condition: template
value_template: "{{ (as_timestamp(states('input_datetime.office_dimmer_switch_hold')) - as_timestamp(states('input_datetime.office_dimmer_switch_double_click'))) > 0 }}"
then:
- if:
# Check the rotation angle is positive i.e. clockwise
- condition: template
value_template: "{{ trigger.to_state.attributes.action_rotation_angle | int > 0 }}"
then:
- choose:
# Off to Bright
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: 'Off'
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_bright
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Bright
# Bright to Concentrate
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Bright
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_concentrate
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Concentrate
# Concentrate to Relax
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Concentrate
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_relax
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Relax
# Relax to Dimmed
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Relax
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_dimmed
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Dimmed
# Dimmed to Nightlight
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Dimmed
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_nightlight
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Nightlight
# Nightlight to Off
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Nightlight
sequence:
- service: light.turn_off
target:
entity_id: light.office_ceiling_light
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: 'Off'
else:
- choose:
# Off to Nightlight
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: 'Off'
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_nightlight
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Nightlight
# Nightlight to Dimmed
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Nightlight
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_dimmed
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Dimmed
# Dimmed to Relax
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Dimmed
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_relax
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Relax
# Relax to Concentrate
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Relax
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_concentrate
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Concentrate
# Concentrate to Bright
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Concentrate
sequence:
- service: scene.turn_on
target:
entity_id: scene.office_ceiling_light_bright
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: Bright
# Bright to Off
- conditions:
- condition: state
entity_id: input_text.office_dimmer_switch_scene
state: Bright
sequence:
- service: light.turn_off
target:
entity_id: light.office_ceiling_light
- service: input_text.set_value
target:
entity_id: input_text.office_dimmer_switch_scene
data:
value: 'Off'
- delay: 00:00:01
else:
# Dim the light
- service: light.turn_on
target:
entity_id: "{{ states('input_text.office_dimmer_switch_target') }}"
data:
brightness_step_pct: >-
{%- if ((trigger.to_state.attributes.action_rotation_angle_speed | int / 24) | abs) < 1 -%}
{{ trigger.to_state.attributes.action_rotation_angle | int / 12 }}
{%- else -%}
{{ (trigger.to_state.attributes.action_rotation_angle | int / 12) * ((trigger.to_state.attributes.action_rotation_angle_speed | int / 24) | abs) }}
{%- endif -%}
They are like variables that you can store things in. Goto Settings->Devices & Services->Helpers->Create Helper and then select the relevant type for each i.e. Date and/or time for the Date and Time helpers (make sure you select Date and Time in the radio button options) and Text for the Text helpers.
Man, you you have to do blueprint for this, I’m serious ))) !
This is the only one, and like best automation for this device, but in clear code a little to difficult to use if you don’t really good in automations and YAML code.