Handling many JSON msg payload

Hi, i have multiple MQTT topics and i need to send individual payload at same time to different MQTT topic.

The issue is if i use msg.payload - the JSON frames will be overwritten. I did explore a path where i tried to create msg.test1 and msg.test2 with JSON payload but the issue is how can my MQTT node will not work with this format.

How can i manage this situation.?

code

[
    {
        "id": "46a7f55b3dc113fa",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f90132623ba1efaa",
        "type": "inject",
        "z": "46a7f55b3dc113fa",
        "name": "inject",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 200,
        "wires": [
            [
                "3120844e6d7228c6",
                "08788246c8d75e3e"
            ]
        ]
    },
    {
        "id": "3120844e6d7228c6",
        "type": "function",
        "z": "46a7f55b3dc113fa",
        "name": "test1",
        "func": "msg.test1={\n\"temperature\" : 20,\n\"pressure\" : 2\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 160,
        "wires": [
            [
                "7a010a252dc12b7b",
                "6264ca3a4d52b995"
            ]
        ]
    },
    {
        "id": "08788246c8d75e3e",
        "type": "function",
        "z": "46a7f55b3dc113fa",
        "name": "test2",
        "func": "msg.test2={\n\"humidity\" : 89,\n\"load\" : 30,\n\"time\" : \"\"\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 240,
        "wires": [
            [
                "d806c099180ea2d3"
            ]
        ]
    },
    {
        "id": "7a010a252dc12b7b",
        "type": "mqtt out",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "topic": "topic1",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "272c1aebb3ef8a9a",
        "x": 610,
        "y": 120,
        "wires": []
    },
    {
        "id": "d806c099180ea2d3",
        "type": "mqtt out",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "topic": "topic2",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "272c1aebb3ef8a9a",
        "x": 590,
        "y": 260,
        "wires": []
    },
    {
        "id": "6264ca3a4d52b995",
        "type": "debug",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "test1",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 560,
        "y": 60,
        "wires": []
    },
    {
        "id": "65c1bfd5cb2f1feb",
        "type": "debug",
        "z": "46a7f55b3dc113fa",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "test2",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 520,
        "y": 320,
        "wires": []
    },
    {
        "id": "272c1aebb3ef8a9a",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.29",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    }
]

Looking at your functions, your payloads need to be in msg.payload, so you could do.
e.g

msg.payload = {
  "test1": {
    "temperature" : 20,
    "pressure" : 2
  }
}

[edit] I am also thinking do you want to join the messages before sending to mqtt out

Im pretty sure (I don't use MQTT), the MQTT node uses msg.payload for the payload.
therefore anything else won't be used.

This is a deep set standard in Node RED.

Try the below.
This will ensure each payload is not affecting the other

[{"id":"f90132623ba1efaa","type":"inject","z":"46a7f55b3dc113fa","name":"inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":200,"wires":[["08788246c8d75e3e"]]},{"id":"08788246c8d75e3e","type":"function","z":"46a7f55b3dc113fa","name":"Send Messages","func":"const Message1 = {\n    payload: {\n        \"temperature\": 20,\n        \"pressure\": 2\n    },\n    topic: 'topic1'\n}\nnode.send(Message1)\n\nconst Message2 = {\n    payload: {\n        \"humidity\": 89,\n        \"load\": 30,\n        \"time\": \"\"\n    },\n    topic: 'topic2'\n}\nnode.send(Message2)\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":200,"wires":[["d806c099180ea2d3"]]},{"id":"d806c099180ea2d3","type":"mqtt out","z":"46a7f55b3dc113fa","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"272c1aebb3ef8a9a","x":575,"y":200,"wires":[]},{"id":"272c1aebb3ef8a9a","type":"mqtt-broker","name":"","broker":"192.168.1.29","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

EDIT
i.e as @E1cid explains

What makes you say that?
My understanding is that each message is entirely seperate.

1 Like

I think its referring to reusing payload when it may still be in transit.
given its an object, its passed as a reference

1 Like

thanks Marcus - will try and will keep you posted

I admit I'm just making assumptions here, but surely if msg was prone to being overwritten whenever a flow splits into two or more wires we would have heard more people complaining about it?

Can a developer clarify this please?

Your right, just tested!
it could well be constrained to the function block - as I'm pretty sure I have hit this situation my self.

agreed, confirmation will be nice

There was a resent issue regarding not defining vars in function nodes, the result was they were available when the function node was called again. That is if i remember correctly

There is this article from @knolleary about a change (in NR 1.0 !) to prevent problems with msg due to pass-by-reference.

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