HomeNodeREDNodeRED - multiple payloads

NodeRED – multiple payloads

More about Function node in NodeRED

I touched on basic function node functionality in my beginner’s guide to NodeRED. In this addendum, I want to focus on dealing with multiple payloads in a single function node. Even if you haven’t come across this before, sooner or later you’ll find yourself in a situation where sending multiple payloads is useful.

Multiple outputs, multiple payloads…

A function node can have more than one output. It’s handy when we have to branch out or split a single flow into multiple paths. You can also turn that very function node into a sophisticated switch node or send multiple payloads using a single output.

It’s up to you to decide when it’s best to use each case (some usability can be replicated using flow style programming) I will simply present you with the toolset needed to make it happen. When speaking about multiple payloads, these scenarios come to my mind:

  • use one of many outputs
  • use all outputs at the same time
  • send multiple payloads through a single output
  • mixing

When you open a function node, below the editor there is an option to add multiple outputs to your node. You could add a lot of them, not sure what would be the most impractical number but if you have to do more than 20, I think you need to rethink your approach.


A function node returns an object. The default object is named msg, but you can change that if you wish (be consistent). It uses the JSON structure (tutorial about JSON) to pass the information to the next node. The return function can return more complex information – like arrays or even nested arrays. If your head hurts already, don’t worry, it will all make sense soon.

Multiple outputs, single payload

I mentioned that the function node can act just like a more complex switch node. You can evaluate the payload inside your script and send the msg.payload to a selected output. To achieve this, you have to return an array which has one element for each output.

Let’s say you want to send a msg object to one of 3 outputs. Your return function would have the following format (assuming 3 outputs are selected):

//send to output 1
return [msg, null, null];

//send to output 3
return [null, null, msg];

//send to output 1 & 3
return [msg, null, msg];

Multiple outputs, multiple payloads

msg object isn’t anything special. In fact, we can define multiple objects within a single function node and pass it over with a return function. To define another object, use the following template following JSON rules

var msg1 = {payload: "payload_value", topic: "topic_value"};

Multiple payloads can be sent at the same time to different outputs. Using information about arrays from the previous example you can compose the return function as follows:

//send msg to output 1 & 3, and msg1 to output 2
return [msg, msg1, msg];

//send msg1 to output 1, nothing to output 2, and msg to output 3
return [msg1, null, msg];

Single output, multiple payloads

Now, that we know how to send multiple messages, let’s send a couple of them from a single function node in a row. A very useful thing when playing with charts in the dashboard. First, you have to define a couple of msg objects. We already know the template:

var msg1 = {payload: "payload_value1", topic: "topic_value1"};
var msg2 = {payload: "payload_value2", topic: "topic_value2"};
var msg3 = {payload: "payload_value3", topic: "topic_value3"};

Now, that we have the objects to send, let’s send first to output 1 and 3rd one to output 2 – take a look at this template for return function:

//msg1, msg2 to output1, msg3 to output 3
return [[msg1, msg2], msg3];

We have nested an array inside the array. Output 1 will resolve both objects msg1 & msg2 before dealing with msg3. If you wanted to send all 3 messages to the same output, you can send like this:

//msg1, msg2, msg3 to output 1
return [[msg1, msg2, msg3]];

Mix and match

All these methods can be used together. Take a look at this example. The function node has 3 outputs and I will be sending various objects:

//msg1, msg2, to output 1, nothing to output 2, and msg1 to output 3
return [[msg1, msg2], null, msg1];

As you see, you can pick where payloads go. Just remember if you use a function node with multiple outputs, use null if one of the payloads isn’t in use. It makes little sense if you define the return parameters manually, but the return parameters can be passed as part of the script, creating a dynamic node which creates different payload structure each time the script is evaluated.

Final thoughts

I hope this short recap helps you. It’s easy enough to grasp and follow and if you have other questions about NodeRED consider checking the full 7-page guide for beginners and additional write-ups about security and preserving variables. If you have any questions, please leave it in this Reddit thread.


Nothing says "Thank you" better than keeping my coffee jar topped up!


Support me on Patreon and get an early access to tutorial files and videos.


Bitcoin (BTC)

Use this QR to keep me caffeinated with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

Smart Ideas with

Automate your space in with these ecosystems and integrate it with other automation services


Learn NodeRED

NodeRED for beginners: 1. Why do you need a NodeRED server?

To server or not to server? That's a very silly question!

Best Automation Projects

NEST your old thermostat under $5

Nest-ing up your older thermostat under $5

Sonoff R3 DIY mode & why it sucks

Long awaited Sonoff R3 DIY mode is even worse than I expected.

Nora – Google Assistant in NodeRED

Integrate Google Assistant with NodeRED thanks to Nora - NodeRED home automation

IKEA TRADFRI Wireless dimmer in NodeRED

Thanks to NodeRED you can use IKEA TRADFRI Wireless Dimmer to interact with everything

Things they don’t tell you about IKEA Trådfri

There are things you should know about IKEA Tradfri before you make your purchase

Smart Home

Electrolama in my network – flashing CC2652R

Flashing CC2652R Zigbee Coordinator to enable ZigBee2MQTT for your home automation

How to wake on LAN computers and put it to sleep with Power Menu, Alexa and Google Assistant

How to Wake on LAN properly via Android, Alexa, Google Assistant and Web

Power Menu – All sensors

Taking all the sensor data and adding it to a single Power Menu tile!

Smarting up Comfee Miniplus Dishwasher

Can a vibration sensor turn Comfee TD305-W dishwasher into a smart appliance? Let's see

M5Stack UnitV2 – Edge computing and vision projects with a mini dev board

M5Stack UnitV2 is upon us - everything you should know about the latest dev board for Edge computing and vision projects.