How can I change an output to the MQTT

How can I change an output to the MQTT.

The Payload from Node Red to the MQTT is:

{"Device":"0x88DA","Send":{"Color":[15932,14999]}}

But i Need it so:

{"Device":"0x88DA","Send":{"Color":"15932,14999"}}

The two numbers are generated by a function node and have the variables X and Y. I've tried a few things but I can't get the payload to form.
I have tried several things but I can't get the payload formatted like this. And without the " the Zigbee lamp always changes to green.

Hi, welcome to the forum.

Please share your function code and show us the msg in the debug output (BEFORE it enters the function)


NOTE: you can paste screenshot snips directly into a reply.

Hello, sorry I'm only getting in touch now.
Here is the code that creates the payload.
If I use an inverted comma instead of the square bracket, I don't get a value.

let red = msg.payload.r
let green = msg.payload.g
let blue = msg.payload.b

let redC =  (red / 255)
let greenC = (green / 255)
let blueC = (blue / 255)
console.log(redC, greenC , blueC)


let redN = (redC > 0.04045) ? Math.pow((redC + 0.055) / (1.0 + 0.055), 2.4): (redC / 12.92)
let greenN = (greenC > 0.04045) ? Math.pow((greenC + 0.055) / (1.0 + 0.055), 2.4) : (greenC / 12.92)
let blueN = (blueC > 0.04045) ? Math.pow((blueC + 0.055) / (1.0 + 0.055), 2.4) : (blueC / 12.92)
console.log(redN, greenN, blueN)

let X = redN * 0.664511 + greenN * 0.154324 + blueN * 0.162028;

let Y = redN * 0.283881 + greenN * 0.668433 + blueN * 0.047685;

let Z = redN * 0.000088 + greenN * 0.072310 + blueN * 0.986039;
console.log(X, Y, Z)

let x = X / (X + Y + Z);

let y = Y / (X + Y + Z);

X = x * 65536 
Y = y * 65536

msg.payload = {"Device":"0x88DA","Send":{"Color":[X,Y]}};
return msg;

Change this line...

To this...

msg.payload = {"Device":"0x88DA","Send":{"Color":[X,Y].join(",")}};

Or...

msg.payload = {"Device":"0x88DA","Send":{"Color":`${X},${Y}`}};

Thank you!
The first line worked right away.
My Zigbee LEDs now switch colours nicely

I still have a lot to learn about Java.

That wouldn't help you for node or node-red...

node and therefore node-red run in JavaScript (totally different to Java) so don't go looking for Java tutorials otherwise you will be completely baffled :slight_smile:

Oh I thought it was the same.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.