Node Red Adding two payloads into one message

25,068 views
Skip to first unread message

Piko

unread,
May 28, 2016, 8:32:07 AM5/28/16
to Node-RED

Hello there ioT enthusiasts ; )

Have fairly simple task to do.  Need to join two MQTT messages together in one. 


Where I can find more information about "context.data" , or stuff like "msg.payload" ,  "new Date().toString()" 
I assume there must be a library somewhere, but was unable to find it. 

Any advise would be much appreciated 

 thank you 
 P

Colin Law

unread,
May 28, 2016, 8:40:08 AM5/28/16
to node...@googlegroups.com
On 28 May 2016 at 13:32, Piko <unstabl...@gmail.com> wrote:
>
> Hello there ioT enthusiasts ; )
>
> Have fairly simple task to do. Need to join two MQTT messages together in
> one.

Can you give more detail on what you mean by this. An example might be helpful.

Colin

Piko

unread,
May 28, 2016, 8:48:11 AM5/28/16
to Node-RED


 So on the left hand side I have two mqtt topics. 

 On the next stop I'm adding a simple information to the payload just to label it (ID_ TEMP & ID_HUMIDITY)

msg.payload =  " TEMP " + " || " + msg.payload ;
return msg;

On the third step I need to add both payloads together and not sure how to do that. 
Screen Shot 2016-05-28 at 13.42.16.png

Mark Setrem

unread,
May 28, 2016, 9:28:35 AM5/28/16
to Node-RED
Have you thought of looking at the documentation?

http://nodered.org/docs/

Or at the flows on http://flows.nodered.org ?

Piko

unread,
May 28, 2016, 11:08:30 AM5/28/16
to Node-RED

Yes I did, that's where I found examples using context.data for example that I'm not familiar with and for some reason can't seem to find it anywhere documented. 
Nor the rest of the internal functions. 

Did I miss something ? 

Nicholas O'Leary

unread,
May 28, 2016, 11:14:14 AM5/28/16
to Node-RED

The page about writing Functions should explain the context object and shows how to use it: http://nodered.org/docs/writing-functions

You also mention "new Date().toString()" - that is standard JavaScript.

A quick search of the flow library finds this: http://flows.nodered.org/flow/8ba7f90f3ea8d92b1e01 which is essentially what you want.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Mattias Marder

unread,
May 29, 2016, 4:16:21 PM5/29/16
to Node-RED
This question pops up frequently and I would like to propose an alternative flow that would avoid the risk of joining messages from separate sources.
The attached flow gives each incoming message an unique ID and the join node cares for checking that the 3 responses that it is waiting for all come from the same original message.

[{"id":"bdb52f32.3c0e4","type":"function","z":"d3334211.d1d0c8","name":"Wait for all tasks to finish","func":"QueryID = msg.ID;\ncontext.data = context.data || {};\ncontext.data[QueryID] = context.data[QueryID] || {task1 : null, task2: null, task3: null};\n\nswitch (msg.topic) {\n case \"task1\":\n context.data[QueryID].task1 = msg.payload;\n msg = null;\n break;\n case \"task2\":\n context.data[QueryID].task2 = msg.payload;\n msg = null;\n break;\n case \"task3\":\n context.data[QueryID].task3 = msg.payload;\n msg = null;\n break;\n \n default:\n msg = null;\n \tbreak;\n\n}\n\nif(context.data[QueryID].task1 !== null && context.data[QueryID].task2 !== null && context.data[QueryID].task3 !== null) {\n\tmsg2 = {};\n msg2 = context.data[QueryID];\n delete context.data[QueryID];\n\treturn msg2;\n} else return msg;","outputs":1,"noerr":0,"x":777,"y":1403,"wires":[["a2f95d9.d67ada"]]},{"id":"a501db75.5b02e","type":"delay","z":"d3334211.d1d0c8","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":519,"y":1312,"wires":[["bdb52f32.3c0e4"]]},{"id":"a2f95d9.d67ada","type":"debug","z":"d3334211.d1d0c8","name":"","active":true,"console":"false","complete":"true","x":984,"y":1402,"wires":[]},{"id":"620c74c9.0b758c","type":"delay","z":"d3334211.d1d0c8","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":523,"y":1408,"wires":[["bdb52f32.3c0e4"]]},{"id":"aabee8b4.2422d8","type":"delay","z":"d3334211.d1d0c8","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":1492,"wires":[["bdb52f32.3c0e4"]]},{"id":"166aae9.07a3651","type":"function","z":"d3334211.d1d0c8","name":"Task1","func":"msg.ID = msg.payload; //a unique ID given by the creator of this message.\nmsg.topic=\"task1\";\nmsg.payload=\"Task1's payload\"\nreturn msg;","outputs":1,"noerr":0,"x":351,"y":1312,"wires":[["a501db75.5b02e"]]},{"id":"3d8ee315.fafcdc","type":"function","z":"d3334211.d1d0c8","name":"Task2","func":"msg.ID = msg.payload; //a unique ID given by the creator of this message.\nmsg.topic=\"task2\";\nmsg.payload=\"Task2's payload\"\nreturn msg;","outputs":1,"noerr":0,"x":354,"y":1407,"wires":[["620c74c9.0b758c"]]},{"id":"f6e54c8e.12128","type":"function","z":"d3334211.d1d0c8","name":"Task3","func":"msg.ID = msg.payload; //a unique ID given by the creator of this message.\nmsg.topic=\"task3\";\nmsg.payload=\"Task3's payload\"\nreturn msg;","outputs":1,"noerr":0,"x":349,"y":1491,"wires":[["aabee8b4.2422d8"]]},{"id":"8db3e0ae.203a48","type":"inject","z":"d3334211.d1d0c8","name":"Start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":185,"y":1396,"wires":[["166aae9.07a3651","3d8ee315.fafcdc","f6e54c8e.12128"]]}]

Dave C-J

unread,
May 29, 2016, 5:21:36 PM5/29/16
to Node-RED
We are working on a pair of split/join nodes that will be released soon that will hopefully help with this sort of situation.

Piko

unread,
Jun 4, 2016, 10:27:07 AM6/4/16
to Node-RED


Thank you, let me rephrase the question. 

Lets say I have a function that takes 3 different payloads and puts them into separate VAR's. How to do that please ? 

Mark Setrem

unread,
Jun 4, 2016, 11:25:23 AM6/4/16
to Node-RED

So a nodered node will only take in one payload at a time, so if you want to do something with all three you will have to store the results and only then do something.

So if you take the example that Nick gave above  http://flows.nodered.org/flow/8ba7f90f3ea8d92b1e01

The "Wait for all tasks to finish" function takes 3 different payloads based on them all having different msg.topic values and stores them as three different variables : context.data.task1 context.data.task2 context.data.task3

 when all three have been received (i.e. aren't null)  it then does something with all three combined (lines 23 onwards).


If this doesn't make sense you will need to be more specific with what you are trying to do.


Mark Setrem

unread,
Jun 4, 2016, 12:25:01 PM6/4/16
to Node-RED
Here's an example that just uses 2.

Where it waits until you have got both a new temperature reading and a new humidity reading before sending out a message that contains both values.
If for example you wanted to add date and time to the output message just add the time functionality into the function using standard javascript.


[{"id":"87a71d9f.7858e","type":"function","z":"876c26c6.7893d8","name":"Merge the two","func":"context.data = context.data || {};\n\nswitch (msg.topic) {\n case \"temp\":\n context.data.temp = msg.payload;\n msg = null;\n break;\n case \"humidity\":\n context.data.humidity = msg.payload;\n msg = null;\n break;\n \n default:\n msg = null;\n \tbreak;\n\n}\n\nif(context.data.temp != null && context.data.humidity != null ) {\n\tmsg2 = {};\n msg2.payload = \"Temp is:\"+context.data.temp+\" Humidity is:\"+context.data.humidity;\n \n context.data=null;\n\treturn msg2;\n} ","outputs":1,"noerr":0,"x":398,"y":108,"wires":[["2a27f572.d5d80a"]]},{"id":"2a27f572.d5d80a","type":"debug","z":"876c26c6.7893d8","name":"","active":true,"console":"false","complete":"true","x":610,"y":108,"wires":[]},{"id":"5f0e2b8f.a0f1d4","type":"function","z":"876c26c6.7893d8","name":"temp","func":"msg.topic=\"temp\";\nmsg.payload=\"25\";\nreturn msg;","outputs":1,"noerr":0,"x":242,"y":52,"wires":[["87a71d9f.7858e"]]},{"id":"e7c18028.183e8","type":"function","z":"876c26c6.7893d8","name":"humidity","func":"msg.topic=\"humidity\";\nmsg.payload=\"78\";\nreturn msg;","outputs":1,"noerr":0,"x":244,"y":193,"wires":[["87a71d9f.7858e"]]},{"id":"51134e61.aeecb","type":"inject","z":"876c26c6.7893d8","name":"Start","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":113,"y":194,"wires":[["e7c18028.183e8"]]},{"id":"6784c9b9.987b38","type":"inject","z":"876c26c6.7893d8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":111.5,"y":52,"wires":[["5f0e2b8f.a0f1d4"]]}]

Andreas Fuchs

unread,
Jun 5, 2016, 9:20:46 AM6/5/16
to Node-RED

Piko

unread,
Jun 8, 2016, 4:50:11 AM6/8/16
to Node-RED

Thank you Mark that works great !

Emanuele Schifio

unread,
Oct 17, 2017, 6:19:31 AM10/17/17
to Node-RED
It's PERFECT! thank you very match, Mark...
Reply all
Reply to author
Forward
0 new messages