Simple rule to detect multiple button presses

I saw a post doing something similar that was more complicated than this, so i thought I would share what I did. In my case, I have a Lutron RadioRa2 keypad button that I wanted to use for different functions, by using a single press, double press, etc.


Switch Special_Button_Flag { expire="3s,command=OFF" }
Number Special_Button


var Number special_count = 0

rule "Special Button Press"
   Item YourButtonItem changed to ON
   special_count = special_count + 1

rule "Special Button End"
   Item Special_Button_Flag changed to OFF
   special_count = 0

rule "Special Button Process"
   Item Special_Button received command
   AmazonController.sendCommand("You pressed the special button "+receivedCommand.toString+" times.")

You may need to play with the expire time to work for your example. The latency to get the button presses processed in my case needed the 3s time. Each time you press the button, it increments the count in a variable and turns on the “special button” item. So 3s after the last press happens, the “special button” item turns off and triggers sending the count to the Special_Button item. My sample rule here for testing just sent the count for Alexa to speak. The real rule would just use the number in receivedCommand to do the different functions.

I’m running OH 3.0.2 but I don’t think this depends on the version at all.

1 Like

Thanks for sharing.

Its my OCD I know but this same line can be written the following ways:

The second way is the better way of the three because at least in the old days a microprocessor would save time by incrementing the variable in 1 cpu cycle. Increment was a separate assembly command the cpu had implemented. The other ways it takes cycles to read the memory, some more to do the maths, then some more to store the result. Does not really matter when you have tons of cpu power sitting idle, but the code is shorter.

For a very long time Rules DSL supported neither += nor ++. I’m not 100% positive it does so today.

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