MQTT Rule for sending hex

I have a dimmer accepting a hex string like ff0000. Everything seems to be working except that the colors are offset as it seems i have a problem with hex conversion. Choosing almost red on the wheel i see in the log

2016-05-14 23:55:28.654 [INFO ] [enhab.model.script.s_RGB_light] - 643b

So i have set up items like

Color s_RGB_light "RGB" <slider> (BED_RGB) String s_RGB_light_str "RGB string" (BED_RGB) { mqtt=">[mymosquitto:mygateway1-in/99/0/1/0/40:command:*:default]" }

And a rule

import org.openhab.core.library.types.* var HSBType hsbValue var int redValue var int greenValue var int blueValue var String RGBvalues rule "Set s_RGB_light value" when Item s_RGB_light changed then hsbValue = s_RGB_light.state as HSBType redValue = hsbValue.red.intValue greenValue = hsbValue.green.intValue blueValue = hsbValue.blue.intValue RGBvalues= redValue.toHexString + greenValue.toHexString + blueValue.toHexString logInfo( "s_RGB_light", RGBvalues ) sendCommand(s_RGB_light_str, RGBvalues) end

Would it work if you used

Integer::toHexString((s_RGB_light.state as HSBType).toColor().getRGB())

?

Not that good at programming at all should it be like this than?

`

import org.openhab.core.library.types.*
var HSBType hsbValue
var int redValue
var int greenValue
 var int blueValue
var String RGBvalues
rule "Set s_RGB_light value"
when
  Item s_RGB_light changed
then
  hsbValue = s_RGB_light.state as HSBType
  RGBvalues= Integer::toHexString((s_RGB_light.state as HSBType).toColor().getRGB())
  sendCommand(s_RGB_light_str, RGBvalues)
end

`

If you install the MQTT action JAR, you could remove the s_RGB_light_str item and the rule could be stripped down to:

import org.openhab.core.library.types.HSBType

rule "Set s_RGB_light value"
when
  Item s_RGB_light changed
then
  publish("mymosquitto", "mygateway1-in/99/0/1/0/40", Integer::toHexString((s_RGB_light.state as HSBType).toColor().getRGB()))
end

Thank you. Added MQTT action JAR. Copied the rule as it is…
This is what i am receiving on the arduino when choosing red. Seems just a little bit offset

read: 0-0-99 s=0,c=1,t=40,pt=0,l=8,sg=0:ffff1500 RGB command: ffff1500 Color long: 2147483647 Color: ffff1500 Red: 32767 Green: 255 Blue: 255

And with blue

read: 0-0-99 s=0,c=1,t=40,pt=0,l=8,sg=0:ffff1500 RGB command: ffff1500 Color long: 2147483647 Color: ffff1500 Red: 32767 Green: 255 Blue: 255

Blue is a typo - pasted same result as red

One issue is that getRGB() returns the alpha in the high byte, and that the string will format to different lengths depending on the color, which might confuse your parsing code on the Arduino side. So try this maybe:

import org.openhab.core.library.types.HSBType
import java.awt.Color

rule "Set s_RGB_light value"
when
  Item s_RGB_light changed
then
  val Color color = (s_RGB_light.state as HSBType).toColor()
  val String rgb = String::format("%1$02x%2$02x%3$02x", color.red, color.green, color.blue)
  publish("mymosquitto", "mygateway1-in/99/0/1/0/40", rgb)
end

This one is working like a charm!!!

1 Like

Long time since you give that solution, but I have today this exact same issue but with OH2. Is there a way to convert this rule to make it work on OH2. I just don’t understand how to convert HSB to RGB.

Thanks!
EDIT:
I finally figure out a way to make it work. I now use that code:

// Color HSB to Hex
hsb = RGB_1.state as HSBType
var red = Math::round(hsb.red.intValue * 2.55)
var green = Math::round(hsb.green.intValue * 2.55)
var blue = Math::round(hsb.blue.intValue * 2.55)
var String rgb = String::format("%1$02x%2$02x%3$02x", red, green, blue)
sendCommand( MQTT_RGB_1, rgb )

Hello i have one Question what must i write for MQTT_RGB_1

The variable name you want to store the value. It could be any item of type String.