I’ve a couple of Xiaomi Aqara/Opple switches with 6 buttons. I use these buttons to toggle lights (single click), dim a specific light (long press) and some other automations (double/triple clicks). These switches are connected to Zigbee2MQTT which publishes the devices/actions to Home Assistant via MQTT.
My problem is that I need a lot of automations for each switch. For example, I need an automation for each button to toggle a specific light.
I was wondering if I could use the new action/choose syntax in my action block. I’ve seen some examples using a condition check inside the choose block but I’m not sure if that’s possible in my case.
This is an example of one of my existing automations:
The topic propagates a JSON with the field action, I think this should be the case for your Opples, too, as we both work with z2m.
The condition is to avoid activations in case that the remote just sends updates for link-/battery-quality withput any user-interaction. An improvement at this point could be - to beautify/simplify this even more - to let the condition-template check for the “key-substrings” like trigger.payload_json.action in ['toggle','click','hold','release'], but unfortunately this didn’t work out. Probably somebody has an idea where the mistake is.
Just to document my pretty long journey on this, here another approach inspired by tinkerer on Discord, which I could not get to work:
For future reference, if there is only one type of service call and only the entity names change, then instead of using choose it can be done like this:
The modes dictionary is used determine which entity to use based on the received payload action.
This automation doesn’t need a condition. If the received payload action doesn’t exist in the modes dictionary then the entity is none. The light.toggle service will fail silently when given an entity called none.
FWIW, the same technique could be used even when different kinds of service calls are needed (i.e. one payload action calls one kind of service whereas another payload action calls a different service).