Convert RGB String to Color Item

Hello!

I’m trying to read RGB values from a string-item (which gets its data from a HTTP-request) and convert it to a Color-item, so that I can display it in a color-picker.

I already got it working the other way round, but can’t figure it out, how to create a new HSBType from RGB-values.

My rule file to convert HSB to RGB looks as follows:

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

var HSBType hsb

rule "HSBtoRGB"
	when
		Item LEDcolor changed
	then
		hsb = LEDcolor.state as HSBType
		val red   = hsb.red
		val green = hsb.green
		val blue  = hsb.blue
		
		sendCommand(LEDcolor_RGB_String, red+","+green+","+blue)
end

The used items are:
Color LEDcolor "LED Strip" String LEDcolor_RGB_String "LED Strip (RGB Values)"


To convert a String-Item to a Color-Item I tried the following:
var String  httpRGB
var String[] parts
var java.awt.Color color
var HSBType hsb

rule "RGBtoHSB"
when
        Item LEDcolor_RGB_String changed
then
        httpRGB = LEDcolor_RGB_String.state.toString()
        parts =  httpRGB.split(",")

        val red = parts.get(0) as DecimalType
        val green = parts.get(1) as DecimalType
        val blue = parts.get(2) as DecimalType

        logInfo("RGB", "R:" + red + " G:" + green + " B:" + blue)

        color = new java.awt.Color(red,green,blue)
        hsb = new HSBType(color) as HSBType

        sendCommand(LEDcolor, hsb)
end

But I get an error: Error during the execution of rule ‘RGBtoHSB’: Cannot cast java.lang.String to org.openhab.core.library.types.DecimalType

Can anybody help me, creating a rule which is able to convert RGB to HSB?

Thanks in advance!
Niklas

You cannot just car a String to a DecimalType. You need to parse it. Try val int red = Integer::parse(parts.get[0])

Thanks!
I got it working now :slight_smile:

If someone may have the same problem, here is my solution:

var String  httpRGB
var String[] parts
var java.awt.Color color
var HSBType hsb

rule "RGBtoHSB"
when
        Item LEDcolor_RGB_String changed
then
        httpRGB = LEDcolor_RGB_String.state.toString()
        parts =  httpRGB.split(",")

        val red = Integer::parseInt(parts.get(0))
        val green = Integer::parseInt(parts.get(1))
        val blue = Integer::parseInt(parts.get(2))

        logInfo("RGB", "R:" + red + " G:" + green + " B:" + blue)

        color = new java.awt.Color(red,green,blue)
        hsb = new HSBType(color) as HSBType

        sendCommand(LEDcolor, hsb)
end
2 Likes