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
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
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
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
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 )