# MQTT, message formats (again) stuck

**URL:** <https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961>\
**Category:** General\
**Created:** [8 June 2019 02:16 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961 "2019-06-08T02:16:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2019 02:16 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/1 "2019-06-08T02:16:41Z")

</div>

This is the flow (cut down)

```auto
[{"id":"54dcbdbf.2fe7a4","type":"inject","z":"c56bddee.ca0a18","name":"test","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":1810,"wires":[["37f3cedd.fe71da"]]},{"id":"37f3cedd.fe71da","type":"function","z":"c56bddee.ca0a18","name":"Name of database & Time Stamp","func":"var time = new Date().toLocaleString();\nvar status;\nif (msg.payload === true)\n{\n //\n status = \"Online\";\n} else\n{\n //\n status = \"Offline\";\n}\nmsg.payload = \"Test message \" + time + ' - ' + status;\nmsg.payload.wap = \"Test message2\";\nmsg.wap = \"Test message1\";\nreturn msg;","outputs":1,"noerr":0,"x":740,"y":1810,"wires":[["de1c59ea.b5bcb8"]]},{"id":"de1c59ea.b5bcb8","type":"function","z":"c56bddee.ca0a18","name":"","func":"//msg.payload = msg.payload + \" CHANGED \" + msg.wap;\nmsg.payload = msg.payload + \" CHANGED \";\nnode.warn(msg.wap);\nnode.warn(msg.payload.wap);\n\n//msg = {payload: msg.payload + \" CHANGED \" + msg.wap,wap: msg.wap};\n\nreturn msg;","outputs":1,"noerr":0,"x":960,"y":1810,"wires":[["c1abb565.b2e5f"]]},{"id":"d534bd32.c384","type":"mqtt out","z":"c56bddee.ca0a18","name":"Wap change","topic":"STATUS/WAP_CHANGE","qos":"0","retain":"","broker":"1ecd3a9e.dc5bfd","x":1350,"y":1810,"wires":[]},{"id":"219bc168.f4e5e6","type":"debug","z":"c56bddee.ca0a18","name":"Sent out MQTT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1360,"y":1770,"wires":[]},{"id":"30a47e80.24cbc2","type":"mqtt in","z":"c56bddee.ca0a18","name":"Wap Change","topic":"STATUS/WAP_CHANGE","qos":"2","datatype":"auto","broker":"f4182f7.9ec475","x":720,"y":1890,"wires":[["53f94133.9997b"]]},{"id":"53f94133.9997b","type":"debug","z":"c56bddee.ca0a18","name":"MQTT received","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":920,"y":1890,"wires":[]},{"id":"c1abb565.b2e5f","type":"function","z":"c56bddee.ca0a18","name":"DUMMY","func":"\nreturn msg;","outputs":1,"noerr":0,"x":1190,"y":1810,"wires":[["d534bd32.c384","219bc168.f4e5e6"]]},{"id":"1ecd3a9e.dc5bfd","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"0","birthPayload":"TelePi comms up","closeTopic":"EOM","closePayload":"TelePi shutting down","willTopic":"EOM","willQos":"0","willPayload":"TelePi Comms Failure"},{"id":"f4182f7.9ec475","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"2","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

_something_ happens, and I create a message.  
It is time stamped and sent off.

I made the payload "Test message" for the sake of making it obvious it was a test.

I then want to get the "device name" sent in the message as an easily get-able thing.  
So I included a `msg.wap` to contain the device. Ok, in this case it is also "test message".  
But for the sake of **double** checking I made `msg.wap` "test message1".

This is what is sent:

```auto
{"_msgid":"1d1faca7.2b4453","topic":"","payload":"Test message 2019-6-8 12:13:28 - Offline CHANGED ","wap":"Test message1"}

```

This is what is received:

```auto
{"topic":"STATUS/WAP_CHANGE","payload":"Test message 2019-6-8 12:13:28 - Offline CHANGED ","qos":0,"retain":false,"_msgid":"5feeb89e.2108b8"}

```

The `msg.wap` has been lost.

Adding a `json` node before the MQTT doesn't help. I don't get any thing being sent to the MQTT node, so it is a loss.

I am missing something but am just not getting it.

Sorry for the (again) dumb question. I just hope that one day all the pieces will fall into place and I will get it then.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 June 2019 02:26 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/2 "2019-06-08T02:26:24Z")

</div>

read the Info panels for both the MQTT in and out nodes... What do they say the node accepts as inputs and provides as outputs ?

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [8 June 2019 02:39 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/3 "2019-06-08T02:39:52Z")

</div>

You may have some luck with the functions I use in [this post.](https://discourse.nodered.org/t/converting-entire-msg-object-to-json/5967) I find them very useful in communicating between multiple instances of node-RED, but there are lots of other uses too.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2019 07:26 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/4 "2019-06-08T07:26:49Z")

</div>

dceejay.

Yes, I believe I have.

The other (recent) problem **should** have been suffice for me to work out what I need to do.

In that it is Arduino code. This is Node.Red (JavaScript).

So the `msg.payload` is the _carrier_.

So I tried setting `msg.payload.wap` as shown in the first `function` node.  
`msg.payload.wap = "Test message2";`

Just after that in the next `function` node there is:  
`node.warn(msg.payload.wap);`

To which I get _undefined_.

Ok. So I'm stuck.

Back to the Arduino one: I just "constructed" the message in pretty much the same way as I did here, and MQTT accepted it.

On that, I did it the same way in the `function` node.  
It doesn't work.

From the arduino code:

```auto
      IPAddress ip = WiFi.localIP();
      snprintf(ip_addr, 60, "{\"WIFI_DEVICE\":\"GPS\",\"IP_Address\":\"%ld.%ld.%ld.%ld\"}" , ip[0], ip[1], ip[2], ip[3]);
      Serial.println(ip_addr);
      client.publish(idTopic, ip_addr);

```

So to me that published to the `idTopic` the `ip_addr`.  
Which is created just above - I guess.

So - as usual - I'm missing something.

Believe me when I say: I don't want to waste anyone's time. I want to learn.  
I also accept that there is probably something wrong with my _learning system_. I can't do much about that other than keep trying to learn.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2019 07:33 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/5 "2019-06-08T07:33:09Z")

</div>

Jay,

Ok, looked.

This is what I now have in the second `function` node:

```auto
msg.payload = msg.payload + " CHANGED ";
node.warn(msg.payload);
node.warn(msg.wap);
node.warn(msg.payload.wap);
JSON.parse(msg.payload);

return msg;

```

I get the first warning, the second. Third _undefined_ and there is no further happenings.  
Nothing is sent to MQTT and (therefore) nothing received.

I even modified the code in the said node to include a fourth `node.warn()` just after the `JSON.parse()` line.

Nothing.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 June 2019 07:49 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/6 "2019-06-08T07:49:18Z")

</div>

Put a debug node showing the complete message showing the input to that function node and see what it shows.  
**[Edit]** and show us what you see for the node.warn messages.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2019 07:57 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/7 "2019-06-08T07:57:33Z")

</div>

This is what I see:

There are three `node.warn()` outputs. (blue lines)  
There is the message being sent INTO MQTT (red line)

 ![Screenshot%20from%202019-06-08%2017-52-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/335a301524d1c63a235477f8b54b723c915a9ea8.png)

Then you see the MQTT output (red lines) and the missing msg.wap (yellow bits)

 ![Screenshot%20from%202019-06-08%2017-52-08](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f29e5a267c422922c82d496e2db8cb115fd88f33.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 June 2019 07:59 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/8 "2019-06-08T07:59:10Z")

</div>

You have set `msg.payload` to a String.

A string property in JavaScript cannot have properties of its own. That is why `msg.payload.wap` doesn't get set.

If you want to send multiple values in the one payload then you need to set payload to be an object:

```auto
msg.payload = {
   payload: msg.payload + " CHANGED ",
   wap: "your wap value"
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 June 2019 08:51 UTC](https://discourse.nodered.org/t/mqtt-message-formats-again-stuck/11961/9 "2019-06-08T08:51:03Z")

</div>

Thank you Nick.

I forgot the `msg.payload.payload` trick.

I hope next time I do remember.
