MQTT is driving me crazy. How to format messages - again, sorry

(This is not enjoyable for me as it clearly shows how bad I am at remembering things.)

Cutting to it:
I want to send a complex message over MQTT.
I want to send the payload and state.
The actual message is HUGE. :frowning:
But despite my every effort I can NOT get what I want out of the remote MQTT node.

Basically I want this as the message (with given variables in the fields)

{"payload":{"payload":"2022-12-29 12:05:09 Main    - Up","state":"Up"},"_msgid":"f1dab4a30fac58e1"}

As is I get this at the remote end:

{"payload":"2022-12-29 12:05:09 Main    - Up","state":"Up"}

But it isn't recognised and can't be put back to what I sent.
If I put it through a JSON node I get the same out as I put it.
I have spent 30 minutes trying permutations of nodes, JSON and function at ends and just can't get the desired result.
It is always a payload with the message in it. (the payload and state messages.)

I won't waste your time explaining what I learnt a long time ago when I went through this the painful way.
But I was told a couple of times and I WROTE IT DOWN - what to do and how to do it.
Now it doesn't seem to be playing by the same rules.

Someone - please.

Do you need to include the msgid ?
Also do you need a payload within another payload ?

Hi Steve.

Maybe this will help.
This is the whole thing - just to maybe help.
(ok, not THE WHOLE THING, but a cut down part of what IS the code)

[{"id":"c611da12474cc64f","type":"inject","z":"9dc4f25c091456e7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":370,"wires":[["12e8f83cb0ca06a5"]]},{"id":"12e8f83cb0ca06a5","type":"function","z":"9dc4f25c091456e7","name":"function 8","func":"msg = \n    { \"delay\": 120000, \"origPayload\": \"[\\\"Marys_Farm_2.4\\\", \\\"Marys_Farm_2.4\\\", \\\"Optus_ABA172_5GHz\\\", \\\"Marys_Farm_5\\\", \\\"Optus_ABA172\\\", \\\"TelstraCA5E31\\\", \\\"\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\", \\\"WiFi-AFB6\\\", \\\"PiNet\\\", \\\"MyRepublic 49D9\\\", \\\"WiFi-F852-5G\\\", \\\"WiFi-F852\\\", \\\"WiFi-AFB6-5G\\\"]\", \"settings\": { \"input\": \"2022-12-29T01:05:09.987Z\", \"input_format\": \"\", \"input_tz\": \"Australia/Sydney\", \"output_format\": \"YYYY-MM-DD HH:mm:ss\", \"output_locale\": \"en_AU\", \"output_tz\": \"Australia/Sydney\" }, \"time\": \"2022-12-29 12:05:09\", \"payload\": \"2022-12-29 12:05:09 Main    - Up\", \"parts\": { \"id\": \"65a01a72889a1b8b\", \"type\": \"string\", \"ch\": \",\", \"index\": 1, \"count\": 13 }, \"_msgid\": \"78d70cd5afd842b2\", \"_event\": \"node:59d4324dd537c9e8\", \"state\": \"Up\", \"filename\": \"/media/pi/9020-9C27/events/main.db\", \"device\": \"Main\" }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":370,"wires":[["5ba78680e4db67c3","3ac66f31b7991a30","4d22b8e3dfa753bc","a0ad6fcbc14eefed"]]},{"id":"5ba78680e4db67c3","type":"debug","z":"9dc4f25c091456e7","name":"Payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":290,"wires":[]},{"id":"3ac66f31b7991a30","type":"debug","z":"9dc4f25c091456e7","name":"FULL","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":540,"y":250,"wires":[]},{"id":"4d22b8e3dfa753bc","type":"function","z":"9dc4f25c091456e7","name":"function 9","func":"let msg2 = {};\nmsg2.payload = {\"payload\":msg.payload,\"state\":msg.state};\nreturn msg2;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":440,"wires":[["cde947a1f2bb79a5","ad528e22a4e5ca79","f01425be1c6b6365"]]},{"id":"a0ad6fcbc14eefed","type":"debug","z":"9dc4f25c091456e7","name":"State","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"state","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":330,"wires":[]},{"id":"ad528e22a4e5ca79","type":"debug","z":"9dc4f25c091456e7","name":"My effort on what to send.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":490,"wires":[]},{"id":"f01425be1c6b6365","type":"mqtt out","z":"9dc4f25c091456e7","name":"","topic":"test","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"8941f4c3.0f151","x":690,"y":370,"wires":[]},{"id":"cde947a1f2bb79a5","type":"json","z":"9dc4f25c091456e7","d":true,"name":"","property":"payload","action":"","pretty":false,"x":560,"y":440,"wires":[["f01425be1c6b6365","b375a3d80fe5b24f"]]},{"id":"8941f4c3.0f151","type":"mqtt-broker","name":"Local","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I won't say much more as I feel I am actually making things worse for myself the more I say at this point.

Does this explain it better?

this should give you what you want I think ?

msg = { payload: "2022-12-29 12:05:09 Main - Up", state: "Up"}

Yes, but that excludes the variability of the two text parts.
Those are real values, but are also variable in:
the date/time, the "Main - Up" and the state.
So those parts need to be *extracted from the incoming payload. The "BIG" one.
(Debug node not active on the flow I posted)

And:
Would that survive going through MQTT?
Would I get both the payload and state at the other end?
I thought MQTT only sent the payload.

OK I see what you mean - don't use payload then, so something like this -
msg = { payld: msg.payload, "state": msg.state };

can you explain for the idiot in the class where payld comes? (SORRY EDITED)

And ITMT I have nearly done exactly the same thing and got it working now.

But I would like to hear what you mean with payld please.

Excluding the MQTT nodes, this is what I did and it seems to work.
I get the payload and state at the remote end.

[{"id":"c611da12474cc64f","type":"inject","z":"9dc4f25c091456e7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":370,"wires":[["12e8f83cb0ca06a5"]]},{"id":"12e8f83cb0ca06a5","type":"function","z":"9dc4f25c091456e7","name":"function 8","func":"msg = \n    { \"delay\": 120000, \"origPayload\": \"[\\\"Marys_Farm_2.4\\\", \\\"Marys_Farm_2.4\\\", \\\"Optus_ABA172_5GHz\\\", \\\"Marys_Farm_5\\\", \\\"Optus_ABA172\\\", \\\"TelstraCA5E31\\\", \\\"\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\", \\\"WiFi-AFB6\\\", \\\"PiNet\\\", \\\"MyRepublic 49D9\\\", \\\"WiFi-F852-5G\\\", \\\"WiFi-F852\\\", \\\"WiFi-AFB6-5G\\\"]\", \"settings\": { \"input\": \"2022-12-29T01:05:09.987Z\", \"input_format\": \"\", \"input_tz\": \"Australia/Sydney\", \"output_format\": \"YYYY-MM-DD HH:mm:ss\", \"output_locale\": \"en_AU\", \"output_tz\": \"Australia/Sydney\" }, \"time\": \"2022-12-29 12:05:09\", \"payload\": \"2022-12-29 12:05:09 Main    - Up\", \"parts\": { \"id\": \"65a01a72889a1b8b\", \"type\": \"string\", \"ch\": \",\", \"index\": 1, \"count\": 13 }, \"_msgid\": \"78d70cd5afd842b2\", \"_event\": \"node:59d4324dd537c9e8\", \"state\": \"Up\", \"filename\": \"/media/pi/9020-9C27/events/main.db\", \"device\": \"Main\" }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":370,"wires":[["5ba78680e4db67c3","3ac66f31b7991a30","4d22b8e3dfa753bc","a0ad6fcbc14eefed"]]},{"id":"4d22b8e3dfa753bc","type":"function","z":"9dc4f25c091456e7","name":"function 9","func":"let msg2 = {};\nmsg2.payload = {\"payload\":msg.payload,\"state\":msg.state};\nreturn msg2;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":440,"wires":[["cde947a1f2bb79a5","ad528e22a4e5ca79","f01425be1c6b6365"]]},{"id":"e1d27bc5107c467e","type":"function","z":"9dc4f25c091456e7","name":"function 12","func":"let text = msg.payload;\nlet msg2 = {};\nmsg2 = msg.payload;\n\nnode.warn(msg2);\n\n//node.warn(text);\n\n//let position = text.search(\"Up\");\n\n//node.warn(position);\n\nreturn msg2;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":300,"wires":[["97e5c5e6f7b8313a"]]},{"id":"97e5c5e6f7b8313a","type":"debug","z":"9dc4f25c091456e7","name":"debug 55","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":300,"wires":[]}]

Disregard that I'm talking at crossed purposes.

msg.payload is required for MQTT node to work.

Yeah, ok.

Thanks.

But (I know: I'm taking this too literally) when I did what you said, it crashed and burned.

(No offense)

But it throws a spanner in the works for me.

Using your node as best I can understand.

My node

Code:

[{"id":"12e8f83cb0ca06a5","type":"function","z":"9dc4f25c091456e7","name":"function 8","func":"msg = \n    { \"delay\": 120000, \"origPayload\": \"[\\\"Marys_Farm_2.4\\\", \\\"Marys_Farm_2.4\\\", \\\"Optus_ABA172_5GHz\\\", \\\"Marys_Farm_5\\\", \\\"Optus_ABA172\\\", \\\"TelstraCA5E31\\\", \\\"\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\\\\\\x00\\\", \\\"WiFi-AFB6\\\", \\\"PiNet\\\", \\\"MyRepublic 49D9\\\", \\\"WiFi-F852-5G\\\", \\\"WiFi-F852\\\", \\\"WiFi-AFB6-5G\\\"]\", \"settings\": { \"input\": \"2022-12-29T01:05:09.987Z\", \"input_format\": \"\", \"input_tz\": \"Australia/Sydney\", \"output_format\": \"YYYY-MM-DD HH:mm:ss\", \"output_locale\": \"en_AU\", \"output_tz\": \"Australia/Sydney\" }, \"time\": \"2022-12-29 12:05:09\", \"payload\": \"2022-12-29 12:05:09 Main    - Up\", \"parts\": { \"id\": \"65a01a72889a1b8b\", \"type\": \"string\", \"ch\": \",\", \"index\": 1, \"count\": 13 }, \"_msgid\": \"78d70cd5afd842b2\", \"_event\": \"node:59d4324dd537c9e8\", \"state\": \"Up\", \"filename\": \"/media/pi/9020-9C27/events/main.db\", \"device\": \"Main\" }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":480,"wires":[["5ba78680e4db67c3","3ac66f31b7991a30","a0ad6fcbc14eefed","4d22b8e3dfa753bc"]]},{"id":"c611da12474cc64f","type":"inject","z":"9dc4f25c091456e7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":480,"wires":[["12e8f83cb0ca06a5"]]},{"id":"5ba78680e4db67c3","type":"debug","z":"9dc4f25c091456e7","name":"Payload","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":400,"wires":[]},{"id":"f01425be1c6b6365","type":"mqtt out","z":"9dc4f25c091456e7","name":"","topic":"test","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"8941f4c3.0f151","x":690,"y":480,"wires":[]},{"id":"f626d563d1d359cd","type":"mqtt in","z":"9dc4f25c091456e7","name":"","topic":"test","qos":"2","datatype":"auto-detect","broker":"8941f4c3.0f151","nl":false,"rap":true,"rh":0,"inputs":0,"x":840,"y":480,"wires":[["0bbde8f77929d592","e1d27bc5107c467e"]]},{"id":"97e5c5e6f7b8313a","type":"debug","z":"9dc4f25c091456e7","name":"debug 55","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1150,"y":480,"wires":[]},{"id":"3ac66f31b7991a30","type":"debug","z":"9dc4f25c091456e7","name":"FULL","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":540,"y":360,"wires":[]},{"id":"4d22b8e3dfa753bc","type":"function","z":"9dc4f25c091456e7","name":"My function node here","func":"let msg2 = {};\nmsg2.payload = {\"payload\":msg.payload,\"state\":msg.state};\nreturn msg2;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":560,"wires":[["ad528e22a4e5ca79","f01425be1c6b6365"]]},{"id":"0bbde8f77929d592","type":"debug","z":"9dc4f25c091456e7","name":"debug 56","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":430,"wires":[]},{"id":"ad528e22a4e5ca79","type":"debug","z":"9dc4f25c091456e7","name":"My effort on what to send.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1000,"y":610,"wires":[]},{"id":"a0ad6fcbc14eefed","type":"debug","z":"9dc4f25c091456e7","name":"State","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"state","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":440,"wires":[]},{"id":"e1d27bc5107c467e","type":"function","z":"9dc4f25c091456e7","name":"function 12","func":"let text = msg.payload;\nlet msg2 = {};\nmsg2 = msg.payload;\n\nnode.warn(msg2);\n\n//node.warn(text);\n\n//let position = text.search(\"Up\");\n\n//node.warn(position);\n\nreturn msg2;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":480,"wires":[["97e5c5e6f7b8313a"]]},{"id":"42cc99af2bd04069","type":"function","z":"9dc4f25c091456e7","name":"Your function node here.","func":"msg = { \"payload\": msg.payload, \"state\": msg.state };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":610,"wires":[["f01425be1c6b6365","ad528e22a4e5ca79"]]},{"id":"8941f4c3.0f151","type":"mqtt-broker","name":"Local","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Sorry, but what am I missing?

(But I do have it working now.)
Though for the sake of learning, I now want to understand what your code is supposed to do.

Ah, that may explain what I just asked in the other post.

But please, if you don't mind.... Check.
:slight_smile:

Just ignore me having a senior moment :wink:

No problems.

I have them all the time.
That is why I am asking the questino I am asking. :giggle:

Only excuse is its after 3am :sleeping:

{{hugs}}

I miss the days when I was working ok at 03:00.

I'll leave you alone now.
Look/check what I asked when you can.
No rush. I think you answered it anyway, but just to be 100% sure.

So what I was trying to say was this -

msg.payload = {payload:msg.payload,state:msg.state};

and in this case the property "payload" (within the msg.payload object) can be called anything :wink:

eg msg.payload = {wibble:msg.payload,state:msg.state};

Oh and this is Sean not Steve BTW :wink:

1 Like

Sorry. I am not good with names either.

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