Aqara knob H1 (wireless) automations

Hello everybody !

А 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 ?

P.S. can’t find blueprint for it.

Try following this guide, change the dim part to what your H1 exposes (turn % and so on)

Thanks, but it’s not very helpful, i try to catch exposes value from H1 but it’s not work they change from real value to null to fast.

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

Did you manage to program the ZNXNKG02LM?

It would be amazing to have Blueprint for this switch. I would like to migrate my whole household to this switch.

Yes program solved.
Change entity’s and device id’s on yours and it will work

trigger:
  - platform: state
    entity_id:
      - sensor.bedroom_knob_action
    to: rotation
    id: rotating_action
  - platform: state
    entity_id:
      - sensor.bedroom_knob_action
    to: start_rotating
    id: start_rotating_action
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: rotating_action
        sequence:
          - service: light.turn_on
            data:
              brightness_step_pct: >-
                {{
                ((trigger.to_state.attributes.action_rotation_percent|float(0)
                /3.33) |  round(0)) * 2.5 }}
            target:
              device_id: dfc32098cc000c4c25df800be21b95fb
      - conditions:
          - condition: trigger
            id: start_rotating_action
        sequence:
          - service: light.turn_on
            data:
              brightness_step_pct: >-
                {{
                ((trigger.to_state.attributes.action_rotation_percent|float(0)
                /3.33) |  round(0)) * 2.5 }}
            target:
              device_id: dfc32098cc000c4c25df800be21b95fb
mode: single
3 Likes

can u make a blueprint? I’m try your code and its not work

ControllerX supports the ZNXNKG02LM:
https://xaviml.github.io/controllerx/controllers/ZNXNKG02LM/

Haven’t tried it yet. But since I have the switch since today, I will give it a try.

in tracing of automation i see

Error rendering data template: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'action_rotation_angle'

but if I see on Mqtt logs, value of action_rotation_angle is true.

MQTT publish: topic 'zigbee2mqtt/Столовая. Регулятор люстры над столом', payload '{"action":"start_rotating","action_rotation_angle":24,"action_rotation_angle_speed":24,"action_rotation_percent":6.666666507720947,"action_rotation_percent_speed":6.666666507720947,"action_rotation_time":200,"battery":100,"device_temperature":25,"last_seen":"2023-03-12T07:05:30.682Z","linkquality":78,"operation_mode":null,"power_outage_count":88,"state":"OFF","voltage":3201}'

what is wrong?

service: input_number.set_value data: value: "{{ trigger.to_state.attributes.action_rotation_angle }}" target: entity_id: input_number.stolovaia_protsent_reguliatora_iarkosti

Yeah im getting the same on mine

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 -%}
2 Likes

Yep this works realy good too ! But looks like this is little slower dimming ?

Sorry, I don’t know how to do blueprint (

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.

1 Like

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. :slight_smile:

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. :slight_smile:

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 -%}
4 Likes

That’s awesome man ! Thanks, hope try it in future in my light control automations !

Can you tell a little more about this part ? What is this halpers ?

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.

1 Like

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.

Thanks one more time !

1 Like