Sending more than one keyname:value to MQTT out

699 views
Skip to first unread message

Jon Murphy

unread,
Aug 11, 2016, 4:15:08 PM8/11/16
to Node-RED
How do I send more than one keyname & value to a MQTT out?  I feel like there is a simpler way then what I have below.

Within the function I am sending one value to emon/test/topic1 and the second value to emon/test/topic2.  Please ignore the delay node, I am trying to debug something else.



function block code
ValueToOutput1 = Math.round(Math.random()*10*100)/100;
ValueToOutput2 = Math.round(Math.random()*10*100)/100;
//  ^^^   thank you Paul R !!
msg1 = {};
msg1.topic = "emon/test/topic1";
msg1.payload = ValueToOutput1;
msg2 = {};
msg2.topic = "emon/test/topic2";
msg2.payload = ValueToOutput2;
return [msg1,msg2];

Nicholas O'Leary

unread,
Aug 11, 2016, 4:18:48 PM8/11/16
to Node-RED Mailing List
By returning an array of messages, each one is sent from the corresponding output of the Function node.

However, if one of those messages is itself an array of messages, then each of those messages will be sent from the same output.

That's a long way of saying, if you did:

return [ [msg1,msg2] ];

Then you'd only need one output from the Function and it would send msg1 and msg2 to the MQTT Out node in turn.

More info on multiple outputs vs multiple messages in the docs: http://nodered.org/docs/writing-functions#multiple-outputs

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+unsubscribe@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.

Jon Murphy

unread,
Aug 11, 2016, 4:37:58 PM8/11/16
to Node-RED
Hi Nick!

I'm not sure I quite understand multiple messages but I will do it!!  Out of curiosity - Is there a limit on how many messages can be sent to the MQTT out via multiple messages?

Thank you!!
Jon

Jon Murphy

unread,
Aug 11, 2016, 4:50:04 PM8/11/16
to Node-RED
I should have added some boundaries to my last question.  I am talking about 5 to 10 messages sent to MQTT out via multiple messages (not 1000's of messages!).

Nicholas O'Leary

unread,
Aug 11, 2016, 4:52:41 PM8/11/16
to Node-RED Mailing List
No limit

Nick

Dave C-J

unread,
Aug 11, 2016, 4:55:05 PM8/11/16
to node...@googlegroups.com
apart from memory on your device etc... 

Jon Murphy

unread,
Aug 11, 2016, 7:14:31 PM8/11/16
to node...@googlegroups.com
I read thru the multiple messages multiple times and did the return [ [msg1,msg2] ].  All work great!  Thank you!

I was going down the path of these:

msg.payload = {"topic1":"5.24","topic2":"4.32”}
-or-
msg.payload = {"emon/test": {"topic1": "5.24","topic2": "3.24”}}

Should one of those worked?

Thanks again!  Jon


From: Nicholas OLeary
Sent: August 11, 2016 at 3:52:24 PM CDT
To: Node-RED Mailing List
Subject: Re: [node-red] Sending more than one keyname:value to MQTT out

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/Kz3jGB861rI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages