Programmatically Set State of Sensor

I have recently purchased a few Xiaomi Aqara Zigbee buttons (WXKG11LM) and am struggling to set them up properly to trigger some of my automations.

They are working fine with my Zigbee2MQTT dongle on a Pi3 and show up as sensors in HA.

When I push the button the state changes to single, double, triple or quadruple - but then it keeps that state for about 50 minutes.

So if I want to use the button to toggle the light on/off with a single key press, I either need to do a multi key press in between - or wait 50 minutes :wink:

I found that if I set the state of the button manually in the /dev-state, I can use a single click straight away again.

Iā€™m now looking for advice on how to replicate this GUI interaction as the last step in any of my automations.

I donā€™t have the exact same button as you, but I do have another Xiaomi Aqara switch. If it works the same way, the way you want to trigger automatons is not by looking at state changes, but by listening to events. In my case, since my switch has two buttons, the automation trigger in the front end looks like this:

Trigger type: Event
Event type: zha_event
Event data:
    {
      "command": "left_single",
      "device_ieee": "xx:xx:xx:xx:xx:xx:xx:xx"
    }

If I want to trigger a different automation with a double click, that automation will listen for a ā€œleft_doubleā€ zha_event. If I want to use another physical switch, I change device_ieee to the one of the other switch, etc.

To check what these values are for your particular switch, go to the Events page under developer tools in the front end, subscribe to ā€œzha_eventā€ (without quotes), and press the switch. Good luck!

Is this a Zigbee or a Z-Wave device?
Is it connected via MQTT?
Does it show up as an actual switch or as a sensor?

Sorry, missed that youā€™re using MQTT. For me itā€™s a zigbee switch, connected via the zha component and HUSBZB-1 radio plugged into RPi 3B+. It shows up as several sensors (binary and others).

Maybe you need to listen for mqtt triggers instead of sensor changes then? (like described on this page)

Donā€™t worry, I found a solution.
Iā€™m using this code to ā€˜resetā€™ the sensor manually:

  action:
    - service: switch.toggle
      entity_id: switch.etekcity_0315_5
    - service: mqtt.publish
      data_template:
        topic: 'zigbee2mqtt/XA_Btn_01'
        payload: >-
          {
           "click": "" 
          }

The tricky part was the sequence in the last four lines; everything else I tried, e.g. escaping the " with \ threw errors in the config check. This seems to work fine now and I can use the same sequence at the end of every automation that is triggered by one of these nifty little buttons.

FYI - I found the solution here, provided by @NotoriousBDG:

1 Like