Handling Hue dimmer switch events


I have the following rules set up on all my dimmer switches:

rule "<NAME> Dimmer Switch"
   Channel "hue:0820:<BRIDGE>:<sensorId>:dimmer_switch_event" triggered
    switch(receivedEvent.getEvent()) {
        case "1001.0": {
            // Button 1 press and hold - white 100%
        case "1002.0": {
            // Button 1 press - return to previous colour
        case "2001.0": {
            // Button 2 press and hold - previous colour but 100% brightness
        case "2002.0": {
            // Button 2 press - Increase brightness
        case "3001.0": {
            // Button 3 press and hold - previous colour minimum brightness
        case "3002.0": {
            // Button 3 press - Decrease brightness
        case "4001.0": {
            // Button 4 press and hold - OFF    
        case "4002.0": {
            // Button 4 press - OFF   

Hopefully other beginners may find this example useful! :slight_smile:

I would like to improve things and add the functions I have lost by resetting the switches in the Hue app as well as adding new tricks :slight_smile:

So my questions are:

Is there a way to make this rule work for all my switches without repeating it for each switch?

Also, is it possible to handle multiple button presses in order to perform different action each time a button is pressed, For example, switch colour each time 1002 is triggered?



See Design Pattern: DRY, How Not to Repeat Yourself in Rules DSL for a collection of techniques that may help. The big challenge here though is that you are using a Channl trigger instead of an Item so a lot of the tools in that post will not be available.

I don’t know much about Hue. Is it possible to link this to an Item somehow? If so then the answer is easy, just apply the Associated Items DP and How to structure a Rule DP.

If not, you will have to create a separate Rule for each Dimmer and then put the code in a lambda (Lambda Example with Copious Notes).

All of the DPs mentioned are linked to from the DRY DP.