Multiple output based on payload byte

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:

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:

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 !!!

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.)

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:

[{"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.

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.

1 Like

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.

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

1 Like

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