Help with mqtt color incoming tranformation

trying to extract the color value from a nspanel color widget .At mqttexplorer i get

{"NSPanel":{"ctype":"group","id":"3","params":{"switch":"on","light_type":1,"colorR":88,"colorG":252,"colorB":0,"bright":50,"mode":1}}}

from my tele/nspanel/RESULT topic
so i made a color channel and trying to extract the color value in the form of R,G,B .My Incoming Value Transformations is

REGEX:(.*"id":"3".*)∩JSONPATH:$.NSPanel.params.colorR∩JSONPATH:$.NSPanel.params.colorG∩JSONPATH:$.NSPanel.params.colorB

but its not working…I am a complete noobie at tranformations so just need a hint where to fix that…
For Outgoing Value Format i use

{"NSPanel":{"ctype":"group","id":"3","params":{"colorR":%s,"colorG":%s,"colorB":%s}}}

and it is working very nice.

edit: i tried JS tranformation ,i add this nscolor.js file inside tranform folder

(function(i) {
    var rgb = JSON.parse(i)

    return rgb.colorR+","+rgb.colorG+","+rgb.colorB
})(input)

and at channel’s Incoming Value Transformations :

REGEX:(.*"id":"3".*)∩JS:nscolor.js

with that i get in the logs

2023-03-09 09:59:45.906 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command 'undefined,undefined,undefined' from channel 'mqtt:topic:b94727f5:nspanel:nscolor' not supported by type 'ColorValue': For input string: "undefined"

Any ideas?

The transformation chaining with the symbol means take the result of the previous transformation as the input for the next transformation. So naturally your transformation above doesn’t work the way you intended.

Avoid using js transformation, because that’s going away in Openhab4. In its place, you can use SCRIPT transformation, which is available on openhab 3.4.2 (possibly one or two sub versions earlier too). SCRIPT transformation in turns lets you use JSScripting to perform the transformation.

Another way I can think of is to do this in a rule.

  • Have an item called String ResultItem that’s linked to receive the whole json data.
  • Have a rule that runs whenever this Item changed
  • Parse the value in the rule and update the intended Color item from the rule.

Someone else might come up with a better idea.

1 Like

I have never used this, but I wonder if JINJA transformation could work for you.

This is highly untested, just based on reading the above documentation, but try this transformation:

JINJA:{{value_json.NSPanel.params.colorR}},{{value_json.NSPanel.params.colorG}},{{value_json.NSPanel.params.colorB}}

Either that or

JINJA:{{value_json["NSPanel"]["params"]["colorR"]}},{{value_json["NSPanel"]["params"]["colorG"]}},{{value_json["NSPanel"]["params"]["colorB"]}}

I’m not familiar with the syntax, so a bit of trial and error or reading the fine manual is needed.

REGEX:(s/.*rR":(.*?),.*rG":(.*?),.*rB":(.*?),.*/$1,$2,$3/g)

Edit:
Colon added.

2 Likes

There is nothing wrong with your REGEX:(.*"id":"3".*)∩JS:nscolor.js Transformation

Try Logging you result from transformation first you can do the exact same to a string item to see the command you are giving your item.

Commanding colour value item is “interger,interger,interger” tells me it is something in your return line that is not formatted the way openHAB needs.

Light_Color.sendCommand("55,80,50")

Your error "not supported by type 'ColorValue"

Try

(function(i) {
    var rgb = JSON.parse(i)

    return rgb.NSPanel.params.colorR+","+rgb.NSPanel.params.colorG+","+rgb.NSPanel.params.colorB
})(input)
1 Like

Genius

@Constantinos_Contis

REGEX:(.*"id":"3".*)∩REGEX(s/.*rR":(.*?),.*rG":(.*?),.*rB":(.*?),.*/$1,$2,$3/g)

Also is your MQTT channel type set to RGB?

i put that in Incoming Value Transformations and i get

2023-03-09 14:05:33.540 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service REGEX(S/.*RR" for pattern (.*?),.*rG":(.*?),.*rB":(.*?),.*/$1,$2,$3/g) not found!
2023-03-09 14:05:33.541 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command '{"NSPanel":{"ctype":"group","id":"3","params":{"switch":"on","light_type":1,"colorR":248,"colorG":168,"colorB":0,"bright":50,"mode":1}}}' from channel 'mqtt:topic:b94727f5:nspanel:nscolor' not supported by type 'ColorValue': {"NSPanel":{"ctype":"group","id":"3","params":{"switch":"on","light_type":1,"colorR":248,"colorG":168,"colorB":0,"bright":50,"mode":1}}} is not a valid string syntax

yes

You need to write REGEX:( - you’re missing the colon symbol after REGEX

yes i saw that :slight_smile: but even i set

REGEX:(.*"id":"3".*)∩REGEX:(s/.*rR":(.*?),.*rG":(.*?),.*rB":(.*?),.*/$1,$2,$3/g)

the linked color item does not update…no errors in the log .

that is working just fine mate thnx.

can i use the “same” script with SCRIPT transformation?