Multi-button switches and rules/scripts

I am working on migrating my light from a Hue Bridge to Zigbee2MQTT. So far I have managed to get a few light bulbs and walls witches up running with OpenHAB3 and Zigbee2MQTT, but I am ending up with quite a lot of rules. E.g. I have this multi-button wall switch, which can return 8 different values (press or release for 4 buttons). For now, I have managed to get the action I want by creating a rule in OpenHAB for each and every possible action returned by the wall switch, but this really clutters my ruleset. Is this the way to do it, or can I solve this in another way with OpenHAB? Should I look into scripting instead or are you guys doing this?

If scripting is the way to go, can you then point me in the direction of script examples?

Thanks in advance

If you are just using UI rules (no code) than no, there is no other way to do this. You’ll need to advance to Script Actions and possibly Script Conditions.

If you don’t know how to code already, I recommend Blockly.

The Rules sections of the Getting Started Tutorial walk you though the basic concepts of rules and then get gradually more and more advanced, moving from UI rules and rule templates to Blockly to actually programming rules.

You should be able to make a generic rule that does what you need. If that’s not enough, search the forum. There are literally tens of thousands of examples in posts on this forum. Just be sure to take note of the language of the example to make sure it’s relevant to what you are using.

If you don’t want multiple rules then scripting is the way to go, and I also recommend Blockly. Here’s an example which takes an action from one of my ZigBee switches via zigbee2mqtt and the MQTT binding:

Perhaps this generic-command profile could be useful:

1 Like

Firstly yes you’ll need to use some form of scripting in order to do the more advanced functionalities.

Ideally you’d want to have one generic “rule” to deal with all the buttons / lights. You do this by using group member triggers.

Set all your buttons that would function the same way (although they’d activate their corresponding lights) into the same group, then trigger on the group member.

Then in your rule, you need a way to associate / link the button that triggered the rule, to the corresponding light that it should control. There are several ways to do this, the simplest way would be to use naming patterns. For example, your buttons are named:

  • LivingRoom_Button
  • BathRoom_Button
  • FrontPorch_Button

and your lights are named:

  • LivingRoom_Light
  • BathRoom_Light
  • FrontPorch_Light

See the pattern?

When LivingRoom_Button triggered your rule, it can deduce that it needs to control LivingRoom_Light, and so on.

So ONE rule, the same piece of code can handle all those buttons instead of writing 3 separate rules. One rule can handle 20, 30, unlimited number of button → light associations.

There are other ways of associating things, my favourite is using the Semantic model but it may be a bit more complicated than just using the naming system above.

Furthermore that same rule can handle single tap, double taps, triple, quad, hold, release, etc.

One piece of advice that I learned much too late in the game, is to use postCommand=true in your MQTT thing channel setting for the button and use Received command trigger. This is probably way TMI for you right now though, so we’ll circle back to it later once you’ve come up with a working rule.

I too have several 4-button Hue switches that can generate 4 codes per button. With a lot of help from @JimT , I combined moving from Python and JavaScript to Ruby with having these actions in one or two rules and a few small supporting methods/functions and was able to easily assign a lot of different lighting actions for various light groups. It still depends mostly on naming standards but I am going to move towards metadata to make it even more flexible.

BTW, one thing I had to deal with is the fact that the binding (3.4.1) is not 100% reliable in the sense that it sometimes misses a key press event (like HOLD or SHORT_PRESS). That needed some tricks to work around (again with the help of @JimT ).

I can share the scripts/rules but you have to tweak them and I’m not sure you are able/willing to dive into that… If you do, let me know.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.