OH2: How to convert ColorPicker to RGB values?

I upgraded my installation to OH2. I have various LED Strips connected via MQTT that needs the RGB values as individual ints.

With OH1 I used a rule based on the sample rule: https://github.com/openhab/openhab/wiki/Samples-Rules#how-to-use-colorpicker-widget-with-knxdali-rgb-led-stripe and everything worked fine.

In the upgrade to OH2 guide (http://docs.openhab.org/tutorials/migration#transformations) it is mentioned that there were some changes for HSBType and Color objects.
According to this description I need to create a Color object first, however this results in an error:

17:08:28.667 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Set Kueche RGB value': An error occured during the script execution: The name 'Color' cannot be resolved to an item or type.

So, how do I convert the state of a ColorPicker to RGB values with OH2?

My current version of the rules (including some commented-out variants I tried ):


rule "Set Kueche RGB value"
	when
		Item kueche_led changed
	then
		logInfo("RGB.kueche_led", "kueche_led changed: "+ kueche_led)
		
		//var HSBType hsb = HSBType::fromRGB(kueche_led.red, kueche_led.green, kueche_led.blue)
	
		
	   var hsbValue = kueche_led.state as HSBType
	   logInfo("RGB.kueche_led", "HSB: "+ hsbValue) 
		
//   	   var Color color = Color::getHSBColor(hsb.hue.floatValue / 360, hsb.saturation.floatValue / 100, hsb.brightness.floatValue / 100)
       var Color color = Color::getHSBColor(hsbValue.hue.floatValue / 360, hsbValue.saturation.floatValue / 100, hsbValue.brightness.floatValue / 100)

//       logInfo("RGB.kueche_led", "Color: "+ color) 

	   redValue = hsbValue.red.intValue
	   greenValue = hsbValue.green.intValue
	   blueValue = hsbValue.blue.intValue
	 
//	   redValue = color.red.intValue
//	   greenValue = color.green.intValue
//	   blueValue = color.blue.intValue
	 
	   postUpdate(kueche_led_R, redValue)
	   postUpdate(kueche_led_G, greenValue)
	   postUpdate(kueche_led_B, blueValue)
	 
	   RGBvalues= "Values" + redValue.toString + ";" + greenValue.toString + ";" + blueValue.toString
	   logInfo( "kueche_led", RGBvalues )
end


Dear Euphi,
dear all,

I’m facing exactly the same issue here. Have you already found a solution?

I have an Enertex KNX Led device. I have 3 items in OpenHAB as dimmers for red, green and blue. The Group Address for switching on/off within the dimmer items of OpenHAB does not work, so I had to add a separate Switch to get the LED stripe on. If the lights are switched on, I am at least able to control the Color of the LEDs via changing the three dimmers items in OpenHab. But the Color Item does not work at all.

Switch LED "LED Lichtband" <socket> (gT) { knx="0/2/0+<0/2/1"}
Dimmer LedR     "LED Red"               <dimmer>     (gT)  { knx = "0/2/0+<0/2/1, 0/2/2, 0/2/3+<0/2/4" }
Dimmer LedG     "LED Green"             <dimmer>      (gT) { knx = "0/2/0+<0/2/1, 0/2/7, 0/2/8+<0/2/9" }
Dimmer LedB     "LED Blue"              <dimmer>   (gT)    { knx = "0/2/0+<0/2/1, 0/2/12, 0/2/13+<0/2/14" }
Color  KNX_RGB_LED  "RGB Light"     <slider>     (gT)

However, the Color itme is not working. I fully followed the sample rules which you quoted as well and I saw that their might be an issue due to a Change from OpenHAB 1 to Openhab 2.

Is there anybody out there who knows how to change the rule in order to align it to OpenHAB2?

import org.openhab.core.library.types.*

var HSBType hsbValue
var String  redValue
var String  greenValue
var String  blueValue

rule "Set RGB value"
when
        Item KNX_RGB_LED changed
then
        hsbValue = RGB.state as HSBType

        redValue   = hsbValue.red.intValue.toString
        greenValue = hsbValue.green.intValue.toString
        blueValue  = hsbValue.blue.intValue.toString

        sendCommand( LedR, redValue )
        sendCommand( LedG, greenValue )
        sendCommand( LedB, blueValue )
end

You need to import java.awt.color

Example:

import java.awt.Color

rule "Set Kueche RGB value"
	when
		Item kueche_led changed
	then
		logInfo("RGB.kueche_led", "kueche_led changed: "+ kueche_led)
		var hsbValue = kueche_led.state as HSBType
		
		var Color color = Color::getHSBColor(hsbValue.hue.floatValue / 360, hsbValue.saturation.floatValue / 100, hsbValue.brightness.floatValue / 100)
		
		var redValue = (color.red.floatValue / 2.55).intValue
		var greenValue = (color.green.floatValue / 2.55).intValue
		var blueValue = (color.blue.floatValue / 2.55).intValue
		logInfo("RGB.kueche_led", "RED: "+ color.red.intValue + "% GREEN: "+ color.green +  "% BLUE: "+ color.blue + "%")
		
		postUpdate(kueche_led_R, redValue)
		postUpdate(kueche_led_G, greenValue)
		postUpdate(kueche_led_B, blueValue)
		
end
1 Like

Dear Euphi,

thanks for giving some hints. The rule you posted seems to go in the right direction. However, I still get an error message within the log.

19:58:46.857 [INFO ] [marthome.event.ItemStateChangedEvent] - KNX_RGB_LED changed from 0.000000 to 100
19:58:46.865 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Set RGB value': org.eclipse.smarthome.core.library.types.HSBType
19:58:47.961 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'KNX_RGB_LED' received command 111,89,100

I am on the way but not fully there. I will go on testing.

Best regards,
Sebastian

Hi,

I don’t see your mistake but I have this rule successfully running with openHAB2:

items:

Color  fWIFI_RGB_1             "RGB"    <slider>     
String WIFI_RGB_1_RGB                             (WIFI_RGB_1) {mqtt=">[mosquitto:/openHAB/H801_1/RGB:command:*:default]"}

sitemap:

Frame label="Tube Lamp" {
      Colorpicker item=fWIFI_RGB_1 icon="slider"
}

rules:

import org.openhab.core.library.types.*
 
var HSBType hsbValue
var int redValue
var int greenValue
var int blueValue
var String RGBvalues
 
rule "Set RGB 1 value"
   when
   Item fWIFI_RGB_1 changed
   then
   hsbValue = fWIFI_RGB_1.state as HSBType 
 
   redValue = hsbValue.red.intValue
   greenValue = hsbValue.green.intValue
   blueValue = hsbValue.blue.intValue
 

   RGBvalues= redValue.toString + ";" + greenValue.toString + ";" + blueValue.toString
   sendCommand( WIFI_RGB_1_RGB, RGBvalues )
   
   logInfo( "fWIFI_RGB_1", RGBvalues )
 end
1 Like

Dear all,

First of all, kudos to the OpenHAB team. I think is a really powerfull platform which allows people with very basic programming knowledge to do our own domotic systems and play around with it. Thank you.

Regarding this issue, I am fighting with it since three weeks ago and I have tried with different options, but at the end of the day I get the same error as Sebastian_OpenHAB. I am frustrated :sweat:

2016-11-19 10:08:52.941 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Set RGB 1 value': org.eclipse.smarthome.core.library.types.HSBType

This morning I have tried, with a clean installation, all the rules that are posted here, but with the same result.
I am using a Raspberry pi 2 with Raspbian Jessie v.8 and OpenHAB2.
Any idea about this problem?
@Sebastian_OpenHAB Did you manage to solve it?
Thank you very much.
Best Regards.

Dear JoseG,

I have a similar Setup. I simply did a update and upgrade of Openhab. Afterwards all issues were gone and the version as Euphi posted worked out of the box. It took me also three weeks… I suggest to update & upgrade. This solved an additional issue for me with my Sonos rules as well.

Best regards,
Sebastian

Dear all,

Thank you very much @Sebastian_OpenHAB for the info. It works perfect after updating & upgrading

Best regards.

Dear Sebastian_OpenHAB, 

I understand that a lot of time has passed since the publication of your message.
But maybe you can help me, could you show what should be the items, sitemap, rules for managing RGB by MQTT?

Thanks David. I used your code to control RGBLeds and WS2812 Leds and it works great

Hi Sebastian

I’m new to OpenHab2, thanks to CedricB’s Velbus binding.

I’m struggling to work out where all these rules are placed and what to do with them, which is a whole new topic :wink:

Can I ask if it’s possible to explain how the OpenHab2 colorpicker HSB value could be turned into 3 seperate 0~100 values to send to 3 different Velbus dimmers? (A separate dimmer channel for each RGB element)

I assume some kind of maths like “roundup (red value / 255)*100” would do the trick.

Likewise, converting the HSB value into a single Hex RGB (with leading Zeros where required) would be fabulous, but I guess that’s for a new topic too.

Many thanks

Stuart

MDAR Ltd
UK importer of Velbus hardware

It’s okay.

@cedricboon very kindly provided me with a rule that has done the trick

 rule "Middle bedroom colour" // The unique name for the rule
when
	Item MidColour changed // "MidColour" is the manually created Color item to capture ColourPicker values from HabPanel
then
	var HSBType hsbValue = MidColour.state as HSBType
	var int redValue = hsbValue.red.intValue
	var int greenValue = hsbValue.green.intValue
	var int blueValue = hsbValue.blue.intValue

	Mid_Red.sendCommand(redValue) // Mid_Red is my Velbus dimmer channel item for the RED component
	Mid_Green.sendCommand(greenValue)  // Mid_Green is my Velbus dimmer channel item for the Green component
	Mid_Blue.sendCommand(blueValue)  // Mid_Blue is my Velbus dimmer channel item for the Blue component
end
2 Likes