Can I convert HSB to HSL to send the color correctly via MQTT


I’ve really been struggling to convert the native colour of openHAB (HSB) to one that my LED lights require which is HSL. Does anyone have any idea on how I could achieve this?

You can do anything you can think of, but it is going to take some work. It looks like the two ways to represent color are pretty different from each other and there is no builtin support for HSL that I’m aware of. This means you will need to do the calculations in a rule.

I found this example in JavaScript which I’ve translated to Rules DSL below:

    val hsbHue = (MyColorItem.state as HSBType).hue.floatValue
    val hsbSat = (MyColorItem.state as HSBType).saturation.floatValue
    val hsbBrt = (MyColorItem.state as HSBType).brightness.floatValue

    val hue = hsbHue
    val sat = hsbSat * hsbBrt/( (if((2-hsbSat)*hsbBrt < 1) hsbHue else 2-hsbHue) )
    val lightness = hsbBrt / 2.0

Thanks very much for taking the time to look into this for me. This is way over my head at the moment as I’m new to coding.

However it appears that I may not need to do this as after much more research into my problem it appears that it is not a colour conversion that I require.

FYI - I’m trying to control some bulbs that are connected to SmartThings that I can half control using the MQTT Bridge and the MQTT binding . The problem I’m trying to solve is that using the color item in openHAB changed the colours but not correctly.

I have found that SmartThings may in fact use the HSB after all but they compress the Hue 0-360 to 0-100 which is why I’m getting odd results.

So I guess I need to find a way to convert that, swap out the real value and send that.