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.

msg.payload

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.

PayPal

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

Patreon

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

image/svg+xml

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

client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image

Learn NodeRED

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

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

Best Automation Projects

Tuya SDK for beginners: Intro to Tuya Cloud API

0
Working with Tuya Cloud API. A guide to Cloud automation for beginners, get started with REST!

NEST your old thermostat under $5

0
Nest-ing up your older thermostat under $5

Sonoff Zigbee Bridge – review

0
Sonoff line up will soon include Sonoff Zigbee Bridge and more Zigbee sensors - here is the first look

Nora – Google Assistant in NodeRED

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

DIY Smart Washing Machine – for about 15 bucks!

0
Learn how to add washing machine notifications to your Google Home on the cheap

Smart Home

I damaged the cheapest Smart Socket with power metering for you

0
Sonoff S60 has an exeptional price for a smart socket with a power meter - I decided to check it out and see how flashable it is

The end of Tasmota? Sonoff SwitchMan M5 Matter

0
These are one of the least expensive Matter devices to automate your lights. Will Sonoff SwitchMan M5 Matter put an end to Tasmota?

Meros TRV to the rescue?

0
I got my hands on another TRV - this time from Meross. I heard good things about the brand so I wanted to see if Meross TRV would be good to manage smart heating.

Aqara brings Thread sensors but…

0
Aqara brings new Thread sensors to their ecosystem. First sensors to support Matter this way are Aqara Motion and Light Sensor P2 and Aqara Contact Sensor P2

Multi-lights for your ceiling from Aqara

0
This is the biggest light I held in my hands so far. It's ZigBee and it comes from Aqara - meet Aqara Ceiling Light T1M