# How can I change an output to the MQTT

**URL:** <https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511>\
**Category:** Developing Nodes\
**Created:** [7 March 2022 19:53 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511 "2022-03-07T19:53:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisausat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisausat/32/58022_2.png) [@chrisausat](https://discourse.nodered.org/u/chrisausat)\
**Post date:** [7 March 2022 19:53 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/1 "2022-03-07T19:53:04Z")

</div>

How can I change an output to the MQTT.

The Payload from Node Red to the MQTT is:

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

```

But i Need it so:

```auto
{"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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 March 2022 20:02 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/2 "2022-03-07T20:02:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrisausat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisausat/32/58022_2.png) [@chrisausat](https://discourse.nodered.org/u/chrisausat)\
**Post date:** [13 March 2022 22:38 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/3 "2022-03-13T22:38:57Z")

</div>

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.

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 March 2022 23:09 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/4 "2022-03-13T23:09:45Z")

</div>

Change this line...

> [@chrisausat](#):
>
> `msg.payload = {"Device":"0x88DA","Send":{"Color":[X,Y]}};`

To this...

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

```

Or...

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

```

---

<div class="post-metadata">

**Author:** ![chrisausat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisausat/32/58022_2.png) [@chrisausat](https://discourse.nodered.org/u/chrisausat)\
**Post date:** [14 March 2022 10:50 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/5 "2022-03-14T10:50:50Z")

</div>

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

I still have a lot to learn about Java.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 March 2022 11:08 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/6 "2022-03-14T11:08:05Z")

</div>

> [@chrisausat](#):
>
> 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 🙂

---

<div class="post-metadata">

**Author:** ![chrisausat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisausat/32/58022_2.png) [@chrisausat](https://discourse.nodered.org/u/chrisausat)\
**Post date:** [14 March 2022 11:22 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/7 "2022-03-14T11:22:10Z")

</div>

> [@Steve-Mcl](#):
>
> 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

Oh I thought it was the same.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 March 2022 11:22 UTC](https://discourse.nodered.org/t/how-can-i-change-an-output-to-the-mqtt/59511/8 "2022-03-28T11:22:58Z")

</div>

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