MQTT color light : not supported by type 'ColorValue'

Hi, setting up a lot of mqtt entities coming from HA, i’m struggling with a LED strip, the on/off is wortking, as well the brightness, but the color one is giving me an headache, not sure how i can fix it… I can send the color, that works, but the problem is receiving the state, that gives me this issue…


2024-11-07 20:19:59.627 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command '{h=120, s=100, x=0.172, y=0.747, r=0, g=255, b=0}' from channel 'mqtt:topic:ha_mqtt_broker:verlichting_slaapkamers:led_alessioc' not supported by type 'ColorValue': {h=120, s=100, x=0.172, y=0.747, r=0, g=255, b=0} is not a valid string syntax

Below is the mqtt thing, the first 2 one are working, the 3th one partiallly (only receiving state issue)

        Type switch : led_alessio "Led Alessio" 	[ stateTopic="openhab/light/led_alessio/state", 
														commandTopic="openhab/light/led_alessio/set_light", 
														transformationPattern="JSONPATH:$.state", 
														formatBeforePublish="{\"state\":\"%s\"}", 
														on="ON", 
														off="OFF" ]
		Type dimmer : led_alessiob "Led Alessio" 	[ stateTopic="openhab/light/led_alessio/state",
														commandTopic="openhab/light/led_alessio/set_light", 
														transformationPattern="JS:brightness2.js∩JSONPATH:$.brightness", 
														formatBeforePublish="{\"brightness\":%s,\"state\":\"ON\"}",
														min=0, max=255, step=25 ]
		Type color : led_alessioc "Led Alessio" 	[ stateTopic="openhab/light/led_alessio/state",
														commandTopic="openhab/light/led_alessio/set_light",
														colorMode="RGB",
														transformationPattern="JS:color2.js∩JSONPATH:$.color", 
														formatBeforePublish="{\"state\": \"ON\",\"color\": {\"r\": %1$d,\"g\": %2$d,\"b\": %3$d}}" ]

anyone here to help?

For incoming values, if the format or values do not match what the OH ColorItem expect, you must apply a stateTransformation to convert what you’ve received to something that OH can understand.

The color coordinate system used by an OH ColorItem is HSB.

Unfortunately this isn’t JSON with those = signs and it has the color defined in HS, XY, and RGB coordinates (I’m guessing, I don’t use a whole lot of color stuff).

For something this complicated you’ll probably need a script transformation. Something like:

(function(input){
  const ColorUtil = Java.type('org.openhab.core.util.ColorUtil'); // import the Java class to access the conversion
  const split = input.split(',');
  const r = parseInt(split[4].split('=')[1]);
  const g = parseInt(split[5].split('=')[1]);
  const b = parseInt(split[6].split('=')[1].replace('}', ''));
  return ColorUtil.rgbToHsb([r, g, b]).toString();
})(input)

The above parses out the RGB coordinates and converts it to HSB.

However, going the other direction is going to be a lot more complicated if the device is also expecting a JSON with all of HS, XY, and RGB in a JSON String. You’ll need to add a transformation for that. Assuming you just need RGB in the JSON and the HS and XY is redundant, the commandTransformation would be something like:

(function(input){
  const HSBType = Java.type('org.openhab.core.library.types.HSBType');
  const ColorUtil = Java.type('org.openhab.core.util.ColorUtil'); 
  const rgb = ColorUtil.hsbToRgb(new HSBType(input));
  return "{ r="+r+", g="+g+", b="+b+" }");
})(input)

hi, thnx! gonna try tomorrow, … you said: “the other direction”, do you mean by that: sending an color value? that already works with this

formatBeforePublish="{\"state\": \"ON\",\"color\": {\"r\": %1$d,\"g\": %2$d,\"b\": %3$d}}"

to to use your test script, i add it to transform folder, “color.js” and then do something like:

transformationPattern="JS:color.js"
?

Yes, or create the transformation in the UI and reference is using the UID for the transformation instead of the file name.

Perfect, gonna try tomorrow

hmm, got some type errors like:

Failed to execute script: TypeError: undefined has no such function "split"
Failed to execute script: TypeError: undefined has no such function "replace"

i have addon below active, do i maybe need to install some extra ?

image

Make sure you have no typos. undefined means you are referecing a variable that doesn’t exist.

hmm, difficult task :frowning: , i’m no coder

How does the return work without dedicated variables for r, g, and b?

@rlkoshak , whats confusing to me, if i listen to the events when i turn on the led strip, i see like below…

Message 6 received on openhab/light/led_alessio/state at 17:40:

`{"state": "ON", "brightness": 255, "color_mode": "hs", "effect": "off", "color": {"h": 0, "s": 100, "x": 0.701, "y": 0.299, "r": 255, "g": 0, "b": 0}}`

So i’m wondering, why is Openhab saying a string like:

not supported by type 'ColorValue': {h=120, s=100, x=0.172, y=0.747, r=0, g=255, b=0} is not a valid string syntax

where are the = coming from?

It doesn’t. That was left over from a different attempt and I didn’t update it.

It should be

return "{ r="+rgb[0]+", g="+rgb[1]+", b="+rgb[2]+" }";

:man_shrugging:

Maybe it’s recognized it’s JSON and parsed it and that’s how the Java JSON parser renders the JSON. I don’t use color Items so have little experience with them.

1 Like

ok, the script below works indeed when i test it in a java compiled, just dont know wht i still get that error :frowning:

(function(input){
  const ColorUtil = Java.type('org.openhab.core.util.ColorUtil'); // import the Java class to access the conversion
  const split = input.split(',');
  const r = parseInt(split[4].split('=')[1]);
  const g = parseInt(split[5].split('=')[1]);
  const b = parseInt(split[6].split('=')[1].replace('}', ''));
  return ColorUtil.rgbToHsb([r, g, b]).toString();
})(input)