Hi Friends, am currently learning node-red and this might be basic knowledge. I understand that I can have multiple outputs on a node and send the msg through different outputs. My doubt is, can I use single output, and send the output to different nodes based on some condition in my js file.
(am familiar with switch node, but was just wondering if this was possible.)
Yes, you can
return [msg1, msg2 msg3]; , you can add outputs at the bottom of the function node, msg1 would go to output 1, msg2 to output 2, etc.
You can skip outputs by sending null
[null, msg2] - output 1 would get nothing, and output 2 would get msg2
More info here
You can do the same with
As drawn the message will go to both nodes, of you want it to go to one then use multiple outputs or use a switch node.
pretty much anything is possible... You can do it this way if you really want to employ function nodes for the sake of using function nodes. Is this what you're going for?
If I were doing programming this way, you can load a new "msg.TheValue" with a value which then in each of the following function Output1 and function Output2 can evaluate and then pass or do whatever the function Output1, Output2 does.
Let's say you want to pass a value to then make a decision in your Output1 and also Output2.
var myvalue= 1;
msg.TheValue = myvalue;
Then in the succeeding functions Output1, Output2 check to see what that is and then do something if its a 1 or a 2.
According to your statements, you have a reason for doing this without the use of the multiple outputs.
HI hp_apcc, this is what I implemented as I couldn't find any other way to route to a specific node without the usage of switch or multiple outputs. Thank you.
Question for you... why do you not want to "route" the message with the multiple outputs... or even a switch? I guess you're doing more than you are telling us. The multiple outputs are for the purpose of switching and/or the switch is just that.. switching or routing...
Ha ha, I am just new to node-red and trying things around. My thought was, if there was an way to select the node to send the msg without multiple outputs, then I wont' have to worry about structuring the message accordingly
node.send("nodename",msg) is easier than [out1,[out2],null]
etc., Also, in future if I decide to add another node, then I think I have to change the message structure again, or that is my understanding. Or, is there a better way to do it?
If you think of the connections between nodes as wires carrying electricity or water pipes, then without switches or valves to control the flow, the messages are going to be sent to all connected nodes.
Depending on your message contents and structure the best choice may be either a switch node or using multiple outputs from the first function. Currently there is no way to select which node receives an output, and to be honest I can't see that your idea is actually simpler than using multiple outputs.
Yes, you might be correct. Am learning things, and my understanding might be wrong. I just thought about writing multiple functions each with msgs routed to different outputs (using multiple outputs), but then If I had to modify them all in case I have to add a new output later on. Again, this is just my understanding, and I still prefer to learn better ways to do it.
There are trade-off's which will always exist. You just need to have a sense of the work you are avoiding or creating for yourself when you choose a path. Typically, you should use the tools at your disposal. These are switch, change, etc. You can do quite a bit with what is available inside these tools. It is amazing what you can do without a line of code.
You are correct. I will start using the available nodes one by one and reduce using code. Thanks for your suggestion.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.