Messages distribution

I have "text input" node and I need that the 1st message appear in Output 1, then 2nd message in output 2 and so on. And after 4 messages, the 5th message will again in output 1.
Below picture shows how the sketch will be look like, but what I need to write in "function node".

Have a read through the node red docs page Writing Functions. That will show you how to have multiple outputs on a function node and also how to use the node context to remember a value from one message to the next, so that you can remember which output to send the next message to.

As Colin said - have a read through the docs page, well worth the time and effort.

To get you started here's a piece of code to direct a message to an output depending on Topic value.

if (msg.topic == "monitor_A") {
    return [msg, null, null, null, null];
else if (msg.topic == "monitor_B") {
    return [null, msg, null, null, null];
else if (msg.topic == "monitor_C") {
    return [null, null, msg, null, null];
else if (msg.topic == "monitor_D") {
    return [null, null, null, msg, null];
else if (msg.topic == "main_display") {
    return [null, null, null, null, msg];
else return null;

Screen Shot 06-23-21 at 02.13 PM

Thanks for your reply, actually, msg.payload is count based instead of topic based

Why do you want to do that, it seems an odd requirement.

If count base then try this

[{"id":"e28d32af.7b1b6","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":1440,"wires":[["d796969f.c0815"]]},{"id":"d796969f.c0815","type":"function","z":"c74669a0.6a34f8","name":"","func":"let count = context.get(\"count\") || 0;\nlet msgArray =[null,null,null,null];\nmsgArray[count] = msg ;\n\ncontext.set(\"count\", (count >= 3 ? 0 : count+1));\nreturn msgArray;","outputs":4,"noerr":0,"initialize":"","finalize":"","x":310,"y":1440,"wires":[["2ab3b436.e9ebf4"],["a2ce40b1.b93808"],["a286052a.56158"],["6ac62665.f01dc"]]},{"id":"2ab3b436.e9ebf4","type":"debug","z":"c74669a0.6a34f8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":500,"y":1400,"wires":[]},{"id":"a2ce40b1.b93808","type":"debug","z":"c74669a0.6a34f8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":1440,"wires":[]},{"id":"a286052a.56158","type":"debug","z":"c74669a0.6a34f8","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":1480,"wires":[]},{"id":"6ac62665.f01dc","type":"debug","z":"c74669a0.6a34f8","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":1520,"wires":[]}]
let count = context.get("count") || 0;
let msgArray =[null,null,null,null];
msgArray[count] = msg ;
context.set("count", (count >= 3 ? 0 : count+1));
return msgArray;
1 Like

There are several packages available at the palette.

keyword = content based router or router in common

There will be light...


Actually, I have input node at Nod-red dashboard to enter the values and sending these values to S7 plc and I need the values in series (rows)... Means 1st payload will show 1output (store in 1st row),, 2nd payload will show in 2nd output (store in 2nd row) and so on. And In dashboard, I don`t want to show individual nodes for individual inputs.

Thanks, It is working

thanks for your suggestion @Swen

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