Zigbee2MQTT & Xiaomi Aqara wall switch problems with the sensor / automations

So I just finished setting up zigbee2mqtt and connect all my zigbee devices to it (works like a charm btw) - one of which is a Xiaomi Aqara douple wall switch (WXKG02LM)

Pairing was no problem and Home assistant recognizes right, left, both - double and long

Sounds awesome, right?!

Unfortunately there is one huge problem. Home Assistant remembers the last press forever instead of going back to nothing after pressing the switch…

So the sensor (what the switch technically is) always shows the state it was last in. This is a huge problem If you want for example toggle a light with each click. Because going form “right” to “right” changes nothing, hence no action will be performed.

I also tried just making “right” and “right_long” automations for ON and OFF, but it is no solution … if you toggle a light from womewhere else (for example if you have two switches, each on opposite sides of the room), chances are, the switch already is in the state you want it to go into … which is not possible.

.

So I thought there must be a solution to this. The best way to solve that issue would be to have the sensor (the wall switch) always go back to a “null” stage right after using it, so even pressing “right” multiple times, would work: “null” -> “right” -> “null” -> “right” -> “null” -> “right” -> “null” and so on (maybe with a second delay before going to “null”) - each time it’s a “to: right” command, even if the last thing you did was pressing “right”. In this case using an automation like this would work

- id: 'xiaomi_switch_01'
  alias: Xiaomi_switch_01
  trigger:
  - entity_id: sensor.0x07158d0242cb7d41_click
    platform: state
    to: left
  condition: []
  action:
  - data:
      entity_id: light.0x0242480102l10ob4_light
    service: light.toggle

The bad thing is, I have no Idea how to do that, I just don’t know enough about writing scripts etc…

So, anyone know how to accomplish such a configuration?

Here’s an example of how I do what you describe that you want to. It uses mqtt updates that get send to home assistant everytime you press a button.

 - alias: "Toggle living"
initial_state: true
trigger:
  platform: mqtt
  topic: zigbee2mqtt/0x00158d0002c3f735
condition:
- condition: template
  value_template: "{{ 'left' == trigger.payload_json.click }}"
action:
    - service: homeassistant.toggle
      entity_id: group.living_room
2 Likes

Oh great - that does work … allthoguh I have no idea why and how xD

Thank you!

btw: what does

initial_state: true

stand for?

Since, I think 0.85, you need to put that line in all your automations to enable them after every reboot of the system

strange - Idon’t have that anywhere but my automations work just fine after reboots :thinking:

Thanks for this!

I was looking for the same solution. This made it.

As the topic title says: problems with sensor/ automations…

I’m having a problem using “the xxx_long_press” for dimming and brighten just like Finally - a cheap WIRELESS switch that dims! Xiaomi Switch Gen1

Thing is, i cant let it stop by releasing the switch. This is because there is no xxx_long_release or equivalent to _release. Any ideas how to get this around?

Like you said there just isn’t a release command, so just holding down until release won’t work (well, at least, I havn’t found a solution - which doesn’t mean anything)

I solved the problem like that for now

- id: 'xiaomi_wandschalter_01_r_l'
  alias: Xiaomi_Wandschalter_01_r_l
  trigger:
  - platform: mqtt
    topic: 'zigbee2mqtt/0x001xxxxxxxcb7d11'
  condition:
  - condition: template
    value_template: "{{ 'right_long' == trigger.payload_json.click }}"
  action:
    service: light.turn_on
    entity_id: light.0x001xxxxxxxeb327_light
    data_template:
      brightness: "{{states.light['0x001xxxxxxxeb327_light'].attributes.brightness|int + 45}}"

This is not exactly what you are looking for, but each long press inceases the brightness by 45. Same thing works the other way around too by using a minus sign “-” in front of the value

brightness: "{{states.light['0x001xxxxxxxeb327_light'].attributes.brightness|int - 45}}"

Just make sure you use values that make sense. Also, you can’t just decrerase the value to 0, because HA still sees the light as on (because you use light.turn on, brightness 0 … which apearently is on)

So my brightness decrease automations look like this:

- id: 'xiaomi_wandschalter_01_l_l_1'
  alias: Xiaomi_Wandschalter_01_l_l_1
  trigger:
  - platform: mqtt
    topic: 'zigbee2mqtt/0x001xxxxxxxxb7d11'
  condition:
  - condition: template
    value_template: "{{ 'left_long' == trigger.payload_json.click }}"
  - condition: numeric_state
    entity_id: 'light.0x001xxxxxxxxb327_light'
    value_template: "{{ states.light['0x001xxxxxxxxb327_light'].attributes.brightness }}"
    above: 30
  action:
    service: light.turn_on
    entity_id: 'light.0x001xxxxxxxxb327_light'
    data_template:
      brightness: "{{states.light['0x001xxxxxxxxb327_light'].attributes.brightness|int - 45}}"

- id: 'xiaomi_wandschalter_01_l_l_2'
  alias: Xiaomi_Wandschalter_01_l_l_2
  trigger:
  - platform: mqtt
    topic: 'zigbee2mqtt/0x001xxxxxxxxb7d11'
  condition:
  - condition: template
    value_template: "{{ 'left_long' == trigger.payload_json.click }}"
  - condition: numeric_state
    entity_id: 'light.0x001xxxxxxxxeb327_light'
    value_template: "{{ states.light['0x001xxxxxxxxb327_light'].attributes.brightness }}"
    below: 31
  action:
    service: light.turn_off
    entity_id: light.0x001xxxxxxxxb327_light

Take note of the numeric_state above and below contitions - this way instaed of triggering “light.turn_on, brightness 0”, you trigger “light.turn_off”

EDIT:

I turn on the light at brightness: 165 , so the automations make sense

1 Like

What a great idea! and a quick reply too.

From what i see is that you brighten up with right_long and dim with left_long?

I am going to try this left_long to brighten and left_double to dim.

Thanks again

1 Like