# Multiple output based on payload byte

**URL:** <https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371>\
**Category:** General\
**Created:** [20 March 2020 20:53 UTC](https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371 "2020-03-20T20:53:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ortegafernando](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ortegafernando/32/98049_2.png) [@ortegafernando](https://discourse.nodered.org/u/ortegafernando)\
**Post date:** [20 March 2020 20:53 UTC](https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371/1 "2020-03-20T20:53:34Z")

</div>

Hi, I have been fighting with something and I cannot solve it.

I have a system of reception of messages by MQTT of several sensors. One of the bytes of this message (it is a buffer) indicates the "TYPE" of the message/sensor.

I want to receive that buffer in a function node. That function node has as many outputs as there are types of sensors / messages I have.

That is, the function node must read the payload's byte, identify the type/sensor, and output the payload again by the output corresponding to the type/sensor number. So then, each output manages the payload considering the type of sensor that originally sends the message.

This is the function code:

```auto
var maxType = 11;
var type = msg.payload [3];
node.warn (type);
var output = new Array (maxTipo) .fill (null);
node.warn (output);
// output [type] = {payload: msg.payload};
output [type] = msg;
node.warn (output);
return [output];

```

But everything comes out from the 1st output.

In the console, based on the two node.warn that you see in the code, I get this:

```auto
3/20/2020 21: 42: 28node: Switch Type
function: (warn)
[null, null, null, null, null, null, null, null, null, null…]
3/20/2020 21: 42: 29node: Switch Type
function: (warn)
[null, object, null, null, null, null, null, null, null, null ...]

```

You see that in the second warn, the second element of the array is an object (a msg).  
But I don't get what I want, the function sends all those nulls and the object in the 1st output, and not, as it should, output nulls for all the outputs except the 2nd, which should come out with the "object" (the msg )

Anyone think what can be wrong?

I have tried return [[output]]; but it gives error.

Thanks in advance. Nice challenge !!!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2020 21:05 UTC](https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371/2 "2020-03-20T21:05:54Z")

</div>

> [@ortegafernando](#):
>
> But everything comes out from the 1st output.

I think that is because you only have one _return_. If your `function` node has (say) 6 outputs, you need to return 6 _messages_ (Outputs - as per your example.)  
So it would be (something like):  
`return [one] [two] [three] [four] [five] [six];`

I'll stick my hand up and make a slight suggestion which you may (or not) like:  
(And believe me, I am an out there thinker with some of the stuff I do. There's nothing wrong with it, just sometimes you are your own worst enemy.)

> [@ortegafernando](#):
>
> var type = msg.payload [3]; node.warn (type);

Ok, so this the the _byte_ which determines where the message should be sent (Which output of the `function` node is to be used) Yes?

Please understand I really don't always understand things the way I should/others do.

But how about this idea:

```auto
[{"id":"78e6da88.8196ac","type":"switch","z":"1d85b106.5dfcd7","name":"Rather than a function node.","property":"payload[3]","propertyType":"msg","rules":[{"t":"eq","v":"one","vt":"str"},{"t":"eq","v":"two","vt":"str"},{"t":"eq","v":"three","vt":"str"},{"t":"eq","v":"(and so on)","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":510,"y":2940,"wires":[[],[],[],[]]}]

```

I've used simple examples in the node.

You will need to change them as needed.

But I think this would be a lot easier than what you are wanting to do.  
Though there is nothing wrong with wanting to do it some other way.  
It just comes down to how much time you are prepared to spend on the problem.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 March 2020 21:17 UTC](https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371/3 "2020-03-20T21:17:12Z")

</div>

You are returning `[output]` where `output` is an array, so you are returning an array consisting of one element (output) which itself consists of multiple objects. The result is that all of `output` is sent to output 1. You want to send just an array so  
`return output`  
Then each element of `output` is sent to a different output.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2020 21:26 UTC](https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371/4 "2020-03-20T21:26:56Z")

</div>

Shame I couldn't find those words Colin.

Thanks for clearing that up. I knew something was awry, but just couldn't put my finger on it.

---

<div class="post-metadata">

**Author:** ![ortegafernando](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ortegafernando/32/98049_2.png) [@ortegafernando](https://discourse.nodered.org/u/ortegafernando)\
**Post date:** [21 March 2020 07:40 UTC](https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371/5 "2020-03-21T07:40:17Z")

</div>

Aaaah, what easy !!!. Sorry for my question and thanks a lot for your solution !!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 April 2020 07:40 UTC](https://discourse.nodered.org/t/multiple-output-based-on-payload-byte/23371/6 "2020-04-04T07:40:20Z")

</div>

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