OpenHab - Red Switches and Dimmers and Smart bulbs rule

I purchased a 4 pack of the red dimmers to use with my LIFX bulbs. I was hoping that being able to disable the internal relay would mean that I could still change what the “dimming level” of the switch was without actually decreasing power going to my smart bulbs and just Group the dimmer and my bulbs together so controlling one would update the other. That turned out not to be the case, though I’m still hoping they can squeeze that functionality in with a firmware update or at least let us control the indicator level. I put together a rule that mostly does what I wanted, though it isn’t as smooth of a dimming as it could be and I’d be able to accomplish the exact same with the switch as with the dimmer.

Basically I’ve setup the switches so 1 tap up or down turns the light on or off. 2 taps up turns the lights to 100% and 2 taps down turns them to 30%. Holding up or down dims or brightens the lights. And 3 taps controls the power to the lights just in case the LIFX bulbs stop communicating and need rebooted.

var Timer timer = null

val dim= [ GroupItem item, Integer amount |
  val brightness = item.state as Number
  var Boolean bool = false
  
  if (brightness + amount > 100) item.sendCommand(100)
  else if (brightness + amount < 0) item.sendCommand(0)
  else {
    item.sendCommand(brightness + amount)
    bool = true
  } 
  bool
]
// If using a color item directly:
// val dim= [ ColorItem item, Integer amount |
//   val brightness = (item.state as HSBType).getBrightness()
//   var Booleen bool = false
  
//   if (brightness + amount > 100) item.sendCommand(100)
//   else if (brightness + amount < 0) item.sendCommand(OFF)
//   else {
//     item.sendCommand(brightness + amount)
//     bool = true
//   }
  
//   bool
// ]

rule "Bedroom Switch Scenes"
when
  Item BRE_Dimmer_01_SceneNumber received update
then
  switch BRE_Dimmer_01_SceneNumber.state {
    case 1.0: {
      GRP_BRE_Dimmer.sendCommand(OFF)
    }
    case 1.2: {     
      var dimAmount = -5
      dim.apply(GRP_BRE_Dimmer, dimAmount)
      timer = createTimer(now.plusMillis(500), [ |
        dimAmount = dimAmount - 5
        if (BRE_Dimmer_01_SceneNumber.state == 1.2) {
          if (dim.apply(GRP_BRE_Dimmer, dimAmount)) timer.reschedule(now.plusMillis(500))
          else timer = null           
        }
        else timer = null        
      ])
    }
    case 1.3: {
      GRP_BRE_Dimmer.sendCommand(30)
    }
    case 1.4: {
      BRE_Dimmer_01_Dimmer.sendCommand(OFF)
    }
    case 2.0: {
      GRP_BRE_Dimmer.sendCommand(ON)
    }
    case 2.2: {
      var dimAmount = 5
      dim.apply(GRP_BRE_Dimmer, dimAmount)
      
      timer = createTimer(now.plusMillis(500), [ |
        dimAmount = dimAmount + 5
        if (BRE_Dimmer_01_SceneNumber.state == 2.2) {
          if (dim.apply(GRP_BRE_Dimmer, dimAmount)) timer.reschedule(now.plusMillis(500))
          else timer = null           
        }
        else timer = null        
      ])
    }
    case 2.3: {
      GRP_BRE_Dimmer.sendCommand(100)
    }
    case 2.4: {
      BRE_Dimmer_01_Dimmer.sendCommand(ON)
    }
  }
end

GPR_BRE_Dimmer is an item group with a default type of dimmer that I put the LIFX bulbs color channel in. BRE_Dimmer_01 is the inovelli dimmer switch. I commented out some code that was applying the dimming to the Lifx bulb directly. I hope this helps someone. I’m sure it could be tweaked a bit to get a smoother dimming action. Not sure how short of a timer you can get away with without breaking something. And you can change the dimAmount = dimAmount + 5 line to control the ramp rate.

LOL, i read your subject line as “these switches rule!”

Nice rule for OH. It’s pretty similar to what I’ve got running as well.

As an added bonus, you can also modify your rule to change your color lights by cycling through light/indicator colors when clicking the config button on the switch. Here’s a code snippet (excuse the formatting I don’t see a code fence option here:

  var String FamilyRoomColorCur = FamilyRoomLightsEffect.state.toString
  val hueActionsColorLocal1 = getActions("hue","hue:0210:1:bulb12")
  var HSBType color = new HSBType(new DecimalType(0),new PercentType(0),new PercentType(100)




case "3.0": //config button
		{
			logInfo("Family Room Color Change", "Current color          " + FamilyRoomColorCur) 
			switch FamilyRoomColorCur {
			case "100600447": //cyan pulsing (default)
			{
				color = new HSBType(new DecimalType(180),new PercentType(0),new PercentType(100))  //bright white
				FamilyRoomLightsEffect.sendCommand(17107597) //set switch indicator to light blue solid for 5s
			}
			case "17107597":
			{
				color = new HSBType(new DecimalType(39),new PercentType(89),new PercentType(100))  //warm white
				FamilyRoomLightsEffect.sendCommand(17107500) //set switch indicator to pale yellow blue solid for 5s
			}		   
			case "17107500": 
			{
				color = new HSBType(new DecimalType(197),new PercentType(100),new PercentType(100)) //blue
				FamilyRoomLightsEffect.sendCommand(17107619) //set switch indicator to blue solid for 5s
			}
			case "17107619":
			{
				color = new HSBType(new DecimalType(120),new PercentType(100),new PercentType(100)) //green
				FamilyRoomLightsEffect.sendCommand(17107536) //set switch indicator to green solid for 5s			   
			}   
			case "17107536":
			{
				color = new HSBType(new DecimalType(343),new PercentType(43),new PercentType(100)) //pink
				FamilyRoomLightsEffect.sendCommand(17107674) //set switch indicator to pink solid for 5s			   
			}   
			case "17107674":
			{
				color = new HSBType(new DecimalType(30),new PercentType(60),new PercentType(100)) //med white
				FamilyRoomLightsEffect.sendCommand(100600447) //set switch indicator to cyan pulsing			   
			}   
			default: 
			{
				color = new HSBType(new DecimalType(30),new PercentType(60),new PercentType(100))  //med white
				FamilyRoomLightsEffect.sendCommand(100600447) //set switch indicator to cyan pulsing			   
			}
			} 

hueActionsColorLocal1.fadingLightCommand("color", color, new DecimalType(2000))

I was playing with some bitwise operations to make changing the color of the indicator easier.

case 3.0: {
      var int currentEffect = (BRE_Dimmer_01_LEDStripEffect.state as Number).intValue
      var int e = currentEffect.bitwiseAnd(255 << 24) >> 24   //effect 1-5
      var int d = currentEffect.bitwiseAnd(255 << 16) >> 16   //duration 0-255
      var int b = currentEffect.bitwiseAnd(255 << 8) >> 8     //brightness 1-10
      var int c = currentEffect.bitwiseAnd(255)               //color 0-255

      c = (c + 16) % 255        //Increment color
      var hue = c * 360 / 255   //Convert 0-255 to 0-360
      var HSBType color = new HSBType(new DecimalType(hue),new PercentType(100),new PercentType(100))
      var newEffect = currentEffect.bitwiseAnd(-256).bitwiseOr(c) //replace color
      BRE_Dimmer_01_LEDStripEffect.sendCommand(newEffect)
      BRE_LightBulb_01_Color.sendCommand(color)

      logInfo("Config Button Pressed", "Effect: " + e.toString + ", Duration: " + d.toString + ", Brightness: " + b.toString + ", Color: " + c.toString)
}

This will make the config button increment the color of the indicator by 16 and set the lights to the same color. It keeps the other effects of the indicator the same.

1 Like

This is definitely much better! Thanks for posting this.

How would you use the same function to change the other elements of the indicator (effect, duration, etc)?