# Example: Convert Color Item Values To RGB (With Explanation)

Because there are regular questions on how to read the Color Items’ values as 24-bit RGB ranging from 0-255 I just wrote this little explanation how and why it works. (Or at least one of the many ways)

Let’s say you have a `Color` item named `aacc_color`. With this rule you can read out the R, G and B values from it:

``````rule "get RGB Colors from ColorItem"
when
then
val red = (receivedCommand as HSBType).red * 2.55
val green = (receivedCommand as HSBType).green * 2.55
val blue = (receivedCommand as HSBType).blue * 2.55
end
``````

`aacc_color` is the name of my Color item.

`aacc_color.state` contains the State object of the Color item. Not really useful for us. So we need to convert it to something that really represents colors. In OpenHAB and OpenHAB2 this is the HSBType, which essentially is Hue, Saturation and Brightness.

`(aacc_color.state as HSBType)` is our Color item. Now it’s not an OpenHAB State object anymore, but a useful color type. If we are interested in the RED, GREEN and BLUE values, we need another step.

`(aacc_color.state as HSBType).red` contains the percentage of red color. This is returned as a PercentType. Yet, I want to have it as a number, ranging from 0 to 255 to control my lights. This can be done through simple maths.

`(aacc_color.state as HSBType).red * 2.55` represents your amount of red between 0 (no red at all) and 255 (full brightness red).

Now you can take your variables and sent commands to other items that may be your RGB light bulbs, or in my case, my DIY MySensors 2,4GHz remote controlled china LED RGB Strips:

``````rule "Set RGB LEDs from Color item using ColorPicker"
when
then
val red = (receivedCommand as HSBType).red * 2.55
val green = (receivedCommand as HSBType).green * 2.55
val blue = (receivedCommand as HSBType).blue * 2.55

aacc_color_r.sendCommand(red)
aacc_color_g.sendCommand(green)
aacc_color_b.sendCommand(blue)
end
``````

Brotip: While using any editor of your choice is fine for writing simple rules, you really should consider using the Eclipse SmartHome IDE for more complicated things like casting Item states to different types. Errors will show up instantly and you have integrated JavaDoc documenatation. This will save you a lot of time and hair.

Bonus content
You can also make use of your own new color-handling code to have functioning ON and OFF buttons near the colorpicker:

``````rule "Luminium Color Item"
when
then
{
val red = (receivedCommand as HSBType).red * 2.55
val green = (receivedCommand as HSBType).green * 2.55
val blue = (receivedCommand as HSBType).blue * 2.55

aacc_color_r.sendCommand(red)
aacc_color_g.sendCommand(green)
aacc_color_b.sendCommand(blue)
}
aacc_color.sendCommand(new HSBType("23,60,100"))
}
aacc_mode.sendCommand(0) // Fade-out lights very slowly. Implemented in the device code
}
end
``````

14 Likes

That is the first time I did understand all this HSBType stuff , thanks for that.
Maybe you should copy that to:
https://community.openhab.org/c/tutorials-examples

1 Like

This is very much appreciated! I just happened to start a project with an H801 and rgb strip a few days ago. Although I got the general gist of how things work through browsing several examples, this really helped clear things up.

A couple questions. The slider below the color picker, what exactly does it control? In playing with it it seemed to have done anything from change the brightness, to completely change the color. Any examples for how to implement it?

Also, is it just me, or is incredibly difficult to get accurate colors using the color picker? Anything in much from the very edge ends up being a bluish white. “In between” colors barely work. No matter how precisely I try to be, I am unable to get a true red using the color picker.

If you are talking about that picture I posted, that is from the Basic UI. It only controls the Brightness channel. But this can be chosen by the UI that displays the ColorPicker.

Why link it? Just move the article

Great article! Thanks!

Thanks for the clarification. So something like this should work in the rule?

``````var brightness = (aacc_color.state as HSBType).brightness.intValue
``````

Hopefully some of you color Gurus can help me with a color problem I am having with this device -> EZMultiPli.

In the manual it is first stated, "LED Color EZMultiPli has a color LED beneath the motion sensor dome. This LED is a convenient night light or an indicator light. The LED can be set to any of eight colors via the Z-Wave Color Switch Command Class
:
1)BLACK (off)
2)WHITE
3)RED
4)GREEN
5)BLUE
6)AQUA
7)PINK
8)YELLOW

A Z-Wave BASIC SET ON/OFF also controls the LED but it only uses the white color. Use the Color Switch Command
Class to control the color of the LED."

Then later we have,

Color Switch Command Class
The color of the LED is controlled with the Color Switch Command Class.

``````Name                   Value (HEX)                        Description
Command Class          33                                 COMMAND_CLASS_COLOR_CONTROL
Command                03                                 STATE_SET
Length                 3                                  Number of ID/State pairs in this command
Capability ID          02                                 RED
State                  00 or FF                           00=OFF, 01-FF=ON
Capability ID          03                                 GREEN
State                  00 or FF                           00=OFF, 01-FF=ON
Capability ID          04                                 BLUE
State                  00 or FF                           00=OFF, 01-FF=ON
Dimming Duration                                          Not used
``````

There are 3 LEDs inside the PIR sensor dome, RED, GREEN and BLUE. Each of the LEDs can be turned either ON or OFF to yield eight different colors. Dimming is not supported. A single command can set the value of all three LEDs or
each LED can be controlled in a separate command.

Note that if the pushbutton is pressed, the color setting is lost because the sensor will enter motion testing mode where
the LED turns on white anytime motion is detected for 5 minutes. Setting the color however cancels motion testing mode and the color will remain.

A BASIC_SET command turns the LED on white or off

With openHAB 1, I could turn the led on with a sendCommand(device, ON). Never tried to work with the color.

Paper UI has also created a channel similar to: “I have a channel created zwave:device:xxxxxxxx:node19:color_color”

So folks, what command can I send to turn these lights on and off, as I have searched for WEEKS, tried many things, but, no light.

(I know I’m going smack myself in the head when I get an explanation)

Because the HSBType inherits the intValue method from DecimalType (see
here:
this should work, yes.

Go and try to type it in the Eclipse SmartHome Designer if you feel unsure
about which methods you may use.

So then you have the brightness in a variable. Just out of interest: What
are you going to do with it?

Cody bot@community.openhab.org schrieb am Di., 28. Feb. 2017 um 15:10 Uhr:

I also see the following in my logs:

``````2017-03-05 17:11:04.245 [INFO ] [.commandclass.ZWaveColorCommandClass] - NODE 19: Color report RED 0
2017-03-05 17:11:04.277 [INFO ] [.commandclass.ZWaveColorCommandClass] - NODE 19: Color report BLUE 0
2017-03-05 17:11:04.308 [INFO ] [.commandclass.ZWaveColorCommandClass] - NODE 19: Color report GREEN 0
2017-03-05 17:11:04.309 [INFO ] [.commandclass.ZWaveColorCommandClass] - NODE 19: Color report finished {RED=0, BLUE=0, GREEN=0}

``````

Hey friends! I found a rather big issue in my technique and ran into trouble today.
As you can see in the code, I am using `(aacc_color.state as HSBType).red` to get the red amount in the Color item. If a another rule (or the RGB device itself) posts an update to the Item, you will have wrong (the new) values in `.red` instead of the values you requested.

So `(aacc_color.state as HSBType)` should really be just `receivedCommand`. We don’t even need to cast, because the command already is an HSBType.

I have updates my examples in my original post.

I am sending an RGB value in hex format to an Arduino running MySensors and controlling itself a RGB strip with neo pixel, the rule ended up looking like this:

``````rule "LEDColorChanging"
then Item LEDColor changed
then

hsbValue = LEDColor.state as HSBType
redValue   = String.format("%02X", (hsbValue.red.floatValue * 2.55) as int)
greenValue = String.format("%02X", (hsbValue.green.floatValue * 2.55) as int)
blueValue  = String.format("%02X", (hsbValue.blue.floatValue * 2.55) as int)

sendCommand(Text_Text, redValue + greenValue + blueValue)
logInfo("LED Color changed", "R" + redValue + "G" + greenValue + "B" + blueValue)

end
``````

This ended logging something like: `LED Color changed: RA8G03BFF`

Clear, thank you.
just a question to see if i fully understood:
Is receivedCommand the substitution of (aac_color.state as HSBType)?

As the one is used in your “Set RGB LEDs from color item Using Colorpicker” rule and the other in your
"Luminium Farbe Setzen" rule for the same purpose

command” events.

2 Likes

I had to think about that a little but now I get it. Tnx

As a thank you to these guides, I share an example of Arduino code which will implement this logic behind MQTT and MySensors network. Have a look if you need sample to build devices to do the RGB.

1 Like

I was requested for video of the LED effects done by @maghac. Here you can see the effects as of today: https://youtu.be/chN6CxMVWm4

There is also slider for lightbulb brightness I forgot to show. All things can be controlled over MQTT. This is about the link in previous post.

So, is it wrong what is mentioned in documentation to multiply with 255 (https://www.openhab.org/docs/configuration/rules-dsl.html#color-item),

1 Like

It does not look all that correct. I will test this and change the docs, if applicable and let you hear back from me.

The docs were indeed wrong. There were also some other issues with maths. I fixed them and wait for the pull request to be accepted and the docs to be re-rendered. Thanks for this pointer, @a.tibi . I also updated my original post to reflect the changes that came in openHAB 2.2

1 Like

Hi Folks,

I’ve been trying to work out how to use the Colour picker to control three LEDs i have that are attached to separate dimmer channels and this solutions work perfectly - thank you!

Only issue i have is that i get a load of errors in my log file (although the desired outcome is correct and the dimmers adjust to the correct brightness and colours!). So this is really just to try and keep things clean, but any help to understand what is wrong would be greatly appreciated!

Here is the rule:

``````val Number red
val Number green
val Number blue

rule "get RGB Colors from ColorItem"

when

then
val red = (receivedCommand as HSBType).red
val green = (receivedCommand as HSBType).green
val blue = (receivedCommand as HSBType).blue

Pond_Red_X.sendCommand(red)
Pond_Green_X.sendCommand(green)
Pond_Blue_X.sendCommand(blue)
end
``````

Like i said, it works perfectly and the channels are updated with the correct values 0-100%.

Here is an example of the errors i see in the log:

``````2020-06-06 18:08:11.265 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'get RGB Colors from ColorItem': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: debug.rules#|::0.2.1.2.0.7::0::/1)
2020-06-06 18:08:11.575 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'get RGB Colors from ColorItem': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: debug.rules#|::0.2.1.2.0.7::0::/1)
2020-06-06 18:08:11.827 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'get RGB Colors from ColorItem': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: debug.rules#|::0.2.1.2.0.7::0::/1)
``````

Any ideas? Cheers!