Xiaomi Aqara Switch MQTT syntax


I am using the Xiaomi Aqara Double switch (WXKG02LM).
Using Zigbee2MQTT I get proper messages: ‘zigbee2mqtt/0x00158d00023a892d’, payload: ‘{“battery”:100,“voltage”:3125,“linkquality”:5,“click”:“right”}’
Following this tutorial Home Assistant Tutorial I found out that my options are: xiaomi_aqara.click click_type single , both

I am using the Home Assistant webinterface for the automation.

If I don’t put a value in the Trigger >>> Payload, everything works. Meaning, the light turns on and off. BUT: I want to differentiate between the left and right button - and even both.

Again, the info page states: xiaomi_aqara.click click_type single , both

I spent an hour experimenting with different syntax combinations of:
single: left
Or as my mqtt receives the messages: “click”:“right”
I played with single and double quotation marks, curly brackets, colon, =, …

Thanks for any kind of help!
Best wishes


So I am confused - are you using zigbee2mqtt or the xiaomi gateway?


I guess the commands that I was trying to implement are targeted at the Gateway, only?

Is there a standard?
I found something along the lines of this:
payload_on: true
payload_off: false

But still I have no idea how to apply this logic to my device. I can see in the UI that after the press of a button I get a “left” or “right”

Thanks again!

Have you read

1 Like

Didn’t try these WXKG02LM switches yet with the zigbee2mqtt, but use them still with my xiaomi hub.
But could it be that you mix up the HA fundamentals?

The “event” of these switches is “click” and the “event values” is “single” and “double”, but the “entities” are something like :

  • binary_sensor.wall_switch_left_158d000170eeab
  • binary_sensor.wall_switch_right_158d000170eeab
  • binary_sensor.wall_switch_both_158d000170eeab
    (these are my xiaomi hub entities, zigbee2mqtt shows probably something else, but should be with left, right and both)

If zigbee2mqtt and HA are working correctly you should be able to find these entities in the “states” overview ( the “<>” under “Developer Tools” in HA sidepanel).

These can be used in your automations, like


@ blakadder: Yes, I read that, but I can’t get it to work.
First, I tried a simple copy/paste with some adaption:

- alias: Respond to button clicks
      platform: mqtt
      topic: 'zigbee2mqtt/<FRIENDLY_NAME'
      condition: template
      value_template: "{{ 'single' == trigger.payload_json.click }}"
      entity_id: light.bedroom
      service: light.toggle

Seems like the whole automation got it’s own .yaml
Seems like the syntax has changed, as it’s a markup-language, that should be of importance… So my other automation that work have an ID and there are dashes…

What looks quite good to me but does not work (HomeAssistant won’t even boot properly):

- id: '1544006372739'
  alias: New
  - platform: mqtt
    topic: 'zigbee2mqtt/sensor.0x00158d00023a892d'
  - condition: template
    value_template: "{{ 'left' == trigger.payload_json.click }}" 
  - entity_id: light.yeelight_color2_7c49ebb9085e
    service: light.toggle

I am not sure about the single quotation marks etc. I played around with “single” or “left” as that’s the payload that is transmitted by mqtt…

It appears to me that for the automation it is single (Quote from the Koenkk site):

  - alias: Respond to button clicks
      platform: mqtt
      topic: 'zigbee2mqtt/<FRIENDLY_NAME'

But for the sensor >> configuration.yaml it’s double:

  - platform: "mqtt"
    state_topic: "zigbee2mqtt/<FRIENDLY_NAME>"
    availability_topic: "zigbee2mqtt/bridge/state"
    icon: "mdi:toggle-switch"
    value_template: "{{ value_json.click }}"
      - "linkquality"
      - "battery"
      - "voltage"
      - "action"
      - "duration"
    force_update: true

Thanks for further ideas!

1 Like

Can you edit the previous post and format the yaml using the instructions in the blue box at the top of the page. I will then be able to paste that in to my configuration to see what is wrong.

In general, the types of quotes are interchangeable. Just make sure you don’t try and nest the same type.

Thanks for the hint. Done

Switching the Xiaomi Aqara Double switch (WXKG02LM) from Xiaomi gateway to zigbee2mqtt now works fine in my setup with left and right selections. For example the left button reacts correctly in following way:

(strange bug: I cannot fill in the “value template” in the automation editor, but it shows when I’ve done this in automation.yaml. Hassio 0.83.3)

Hope this helps.

Could you post your automation.yaml code about the above screenshot?
I’m trying to switch from xiaomi gateway to zigbee2mqtt my WXKG02LM…but w/o any kind of luck.
In HA, my switch is of type “sensor” and not “binary_sensor” as it should be…I don’t know why honestly
For your info, I have mqtt discovery set to “true” on zigbee2mqtt hassio addon


Ok, here you are, nothing fancy I guess

- id: '1530960318173'
  alias: Living Room Lamp Switch Left (toggle)
  - platform: mqtt
    topic: zigbee2mqtt/0x00158d000170eeab
  - condition: template
    value_template: '{{ ''left'' == trigger.payload_json.click }}'
  - data:
      entity_id: switch.sonoffbasic03
    service: switch.toggle
  - data:
      entity_id: switch.sonoffs2001
    service: switch.toggle

- id: '1530960436081'
  alias: Living Room Lamp Switch Right (toggle)
  - platform: mqtt
    topic: zigbee2mqtt/0x00158d000170eeab
  - condition: template
    value_template: '{{ ''right'' == trigger.payload_json.click }}'
  - data:
      entity_id: switch.sonoffbasic04
    service: switch.toggle
  - data:
      entity_id: switch.sonoffbasic05
    service: switch.toggle

And my double switch representation in "States:

And my double switch logged in zigbee2mqtt log:

zigbee2mqtt:info 2018-12-21 20:13:16 0x00158d000170eeab (0x00158d000170eeab): WXKG02LM - Xiaomi Aqara double key wireless wall switch (EndDevice)

zigbee2mqtt:info 2018-12-21 20:13:17 MQTT publish: topic ‘zigbee2mqtt/0x00158d000170eeab’, payload ‘{“battery”:55,“voltage”:2995,“linkquality”:68}’

zigbee2mqtt:info 2018-12-21 20:13:17 MQTT publish: topic ‘homeassistant/sensor/0x00158d000170eeab/click/config’, payload ‘{“icon”:“mdi:toggle-switch”,“value_template”:“{{ value_json.click }}”,“json_attributes”:[“linkquality”,“battery”,“voltage”,“action”,“duration”],“force_update”:true,“state_topic”:“zigbee2mqtt/0x00158d000170eeab”,“availability_topic”:“zigbee2mqtt/bridge/state”,“name”:“0x00158d000170eeab_click”,“unique_id”:“0x00158d000170eeab_click_zigbee2mqtt”,“device”:{“identifiers”:“zigbee2mqtt_0x00158d000170eeab”,“name”:“0x00158d000170eeab”,“sw_version”:“Zigbee2mqtt 1.0.1”,“model”:“Aqara double key wireless wall switch (WXKG02LM)”,“manufacturer”:“Xiaomi”}}’

Now you have all te information. Hope it helps.

1 Like

Thanks mate.

As I can see from your code, you have the same…issue…as I have.
With this kind of code (mine is very similar), when you reload your automations.yaml (configuration --> general) or restart HA server, the automations will be executed (with ‘initial_state: true’) in accordance with the last state of your buttons (i.e. if you have clicked the left button to toggle your switch off before HA restart, on the reboot the automation starts and it will toggle the switch on automatically…).
On the other side, if you do not set ‘initial_state’ (it will assume ‘false’, as you know) or set it to false, the automation will not be empowered on the next HA boot and you have to enable it by hand.

What can we do against this (for me, at least) very annoying…behaviour?


Hmm, didn’t notice this with restarts. Perhaps because I have, next to the automatons for this switch, also automation for the same lights to turn on by sunset and to turn off late evening.
And I do not restart HA often, so I have no real problem with this behaviour. Anyway, I didn’t notice any non logical behaviour.

Ok, this sounds well…nevertheless, do you know any kind of possible workaround?
For me, this behavior is not much appropriated, especially because I my automations very often and this thing append every time I reload .yaml file…


Automations do not trigger when you restart ha or reload automations.yaml.

I’m sorry but this isn’t true…for me, at least.
I have just (re)tried, and I can confirm you that my automations get enabled (and triggered) on automations reload too.
See below the piece of my code I’m talking about

 - id: 'xiaomi_wall_switch_left'
   alias: Xiaomi Wall Switch (left) - My Bedroom - My Bed
   initial_state: True
     platform: mqtt
     topic: zigbee2mqtt/0x00158d000255dd85
     - condition: template
       value_template: "{{ 'left' == trigger.payload_json.click }}"
      service: homeassistant.toggle
          entity_id: light.my_bed

For your reference, I’m on hassio (supervisor version 141), HA version 0.84.6 and zigbee2mqtt-edge.

Thanks guys and…Merry Christmas!!

Are you sure the automation is getting triggered and it’s just not a “retain:” issue?

Can you look in the logbook and actually see that the automation indeed does get triggered.

And I just noticed that your trigger doesn’t include a “to:” statement (or equivalent). That means that every time there is a change in state of that topic the automation will trigger. Maybe the automation actually does get triggered because that topic is resent every time you restart. Try changing the trigger to something else to see if it fixes it.

Yea, I am sure that is not a retail issue.
In logbook I can see the automation get triggered every time I restart HA or reload automations.yaml.

The last part of your statement made me thinking…
I noticed since the beginning I linked the xiaomi dual switch with zigbee2mqtt that HA sees it as a “sensor” and not as “binary_sensor” as the addon wiki says too.
I have other sensors (aqara and mijia uman presence sensor) linked by zigbee2mqtt and each one of those is recognized rightly by HA as “binary_sensor”.
The xiami’s switches (I have two aqara dual wireless wall switch and two mijia single wireless switch) get seen as “sensor” though.

Could be this a (or “the”) problem?

1 Like

try to do this and see if you can figure out how to trigger it with a different trigger.

I don’t know why HA thinks it is a sensor instead of a binary sensor. I don’t use discovery and for pretty much this reason. If something doesn’t work quite right then you are fighting against the system automatically trying to insert stuff that you don’t want it to. And that makes troubleshooting difficult.

But thinking about it, it doesn’t really matter what kind of sensor it is. The automation will trigger every time the state of the sensor changes (or if it’s MQTT maybe whenever the topic is republished?). That’s why I suggested changing the trigger to find something else that might work.

hi I had the same issue, found out it had to do with the use of single quotes instead of double quotes.
this worked for me:
'{{ ''left'' == trigger.payload_json.click }}'