Better Way of Handling Tuya/Moes Scene Switches with Zigbee2Mqtt

TL;DR - Is there a better way to handle Tuya/Moes scene switches with Zigbee2MQTT?

I have migrated from ZHA to Zigbee2MQTT.

In ZHA Scene switches were exposed in the UI so it was possible to create new automations and select the button/press type from the UI. In Zigbee2MQTT press events are sent as payloads on the action topic, as a result a number of people have created templates like this:

That simply list out each possible combination of button and press type (single, double and hold) and allow the user to assign actions to each option.


So I was wondering if anybody had implemented a better solution than this?

Note: I did have a few of ideas, I have implemented the first one (it works).
Just wanted to see if someone had a better idea?

“Button Press Router” Automation

In some rooms I have multiple scene switches, to reduce the complexity I have a rule that all switches in the same room function the same way (button 1 does the same thing on all switches in the room and so on).

I use a single automation that has triggers for all my scene switches, it simply maps the topic to a room and calls downstream scripts with the parameters:

  • room_name
  • button_number
  • press_type

That way the downstream scripts don’t care which switch was pressed they simply have to have a case for each room.

Note: I implement other patterns to make the scene switches easier to use:

  • Single click - Cycle through scenes, which all have the same order:
    • Off
    • Bright (max)
    • Evening
    • … any other scenes…
  • Double click - Toggle between previous scene and Off scene.
  • Hold - Activate max brightness

TL;DR - For guests, I simple say:

  • To turn on: Single Click
  • To turn off: Double Click

Merge MQTT Topics

This is pretty much the same I idea as above, however I could write some code to intercept the MQTT discovery message and merge all the action topics into a single topic (where the payload contains the room name).

That way I don’t need to to update my automation whenever a new switch is added I would need a naming convention to ensure the mapping to rooms was done correctly.

Re-publish MQTT Events

Instead of unifying the events I could separate them out at the MQTT level, i.e. I could send MQTT device triggers, I believe that would expose the buttons in HA the same way as they are exposed in ZHA.

have a look at this…