How to send Color Picker output via MQTT as integer or HEX

I’m using MQTT to interact with Arduino devices using the MySensor libraries. I am able to publish my ColorPicker updates to my MQTT server and subsequently to my Arduino client node. The problem is that OpenHAB is sending the long decimal string for the color selected. My client nodes can only receive a max 25 byte string, and the sent string is usually 35+ characters. The best option is to convert the integers to HEX and publish the 6 character HEX number. Second option would be to drop all the decimal places and then publish the integers. Unfortunately I can’t figure out how to do either, and I haven’t found any examples on my search.


Your best bet will probably be to create a rule that gets triggered when the ColorPicker Item gets updated, reformats the value as needed, and then postUpdate the reformatted value to the MQTT Item.

Or use something like mqttwarn which can subscribe to your raw data topics, do any translation, and republish to another topic. Thereby decoupling the MySensor network from your openHAB config.

I think the RULE idea is the most appropriate, since I don’t really want to introduce another tool in the mix. I have already figured out how to get the right data from the ColorPicker output using .state. Now I’m trying to figure out how to take a number like this:


And parse it into the following

Which I then save to variables

I’m unfamiliar with the code used within OpenHAB to right this rule up. I’ve seen some code for an Arduino to do this, however, they are not the same.

Can someone point me in the right direction?


I’ve not dealt with colors much but assuming that your item is a Color item and it is carrying an HSBType:

If your Color Item is named LightColor

val RGBRed = (LightColor.state as HSBType).getRed.intValue
val RGBGreen = (LightColor.state as HSBType).getGreen.intValue
val RGBBlue = (LightColor.state as HSBType).getBlue.intValue

I’m pretty sure this truncates instead of rounds. If you want to round based on the fractional part of the number you need to do:

val RGBRed = Math::round((LightColor.state as HSBType).getRed.doubleValue)