MQTT, message formats (again) stuck

This is the flow (cut down)

[{"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:

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

This is what is received:

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

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 ?

1 Like

You may have some luck with the functions I use in this post. I find them very useful in communicating between multiple instances of node-RED, but there are lots of other uses too.

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:

      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.

Jay,

Ok, looked.

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

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.

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.

This is what I see:

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

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

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:

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

Thank you Nick.

I forgot the msg.payload.payload trick.

I hope next time I do remember.