How do I write to 2nd output on a function node

7,253 views
Skip to first unread message

Toshi Bass

unread,
Nov 8, 2015, 11:40:13 AM11/8/15
to Node-RED
Hi

I have a function node that outputs a 1 or 0 depending on the incoming mqtt message.

if (msg.payload=="Lamp=1"){
  msg.payload= 1;
  return msg}
else if (msg.payload=="Lamp=0"){
  msg.payload = 0;
  return msg}

ok but I want to convert the function node to have 2 outputs first output as above  Lamp=1 .. 1st message payload is1 and 2nd message.payload is 0      if Lamp=0 .. 1st massage payload is 0  2nd message payload is 1

Imagine 2 leds connected to the 2 outputs from the function node a red led and a green led when Lamp=1 the green led is on the red off the when the Lamp=0  the green led is off and the red led is on

Ive banged my head against this afternoon but I just don't get how to send the message payload to the second output on the function node.


Nicholas O'Leary

unread,
Nov 8, 2015, 11:55:53 AM11/8/15
to Node-RED

Have a read of the Multiple Outputs section of http://nodered.org/docs/writing-functions.html and let me know if that solves it for you.

Nick


--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave C-J

unread,
Nov 8, 2015, 11:55:57 AM11/8/15
to node...@googlegroups.com
see the section on multiple messages...

Mark Setrem

unread,
Nov 8, 2015, 12:19:33 PM11/8/15
to Node-RED
The bit that is not explicitly stated on in the instructions is that you need to declare the object first.
.
So for example if you are using msg2 as the message to send to the second output, you need:

msg2={};

at the start of the function.

Otherwise you'll get the error message that msg is not defined.

Dave C-J

unread,
Nov 8, 2015, 6:20:31 PM11/8/15
to node...@googlegroups.com
Thanks - have made that example more explicit.

Toshi Bass

unread,
Nov 9, 2015, 2:47:35 PM11/9/15
to Node-RED
Strange I did post a response to Marks post yesterday but obviously it went astray anyway essential it said :

Thanks Mark for the  msg2={};  thats what I was missing all the time ..... for anyone following here's what I ended up with (Im a python man don't have much experience with other languages but learning slowly.

msg1={};
msg2={};
if (msg.payload=="Lamp,1"){
  msg1.payload = 1;
  msg2.payload = 0;
  return [msg1,msg2]}
else if (msg.payload=="Lamp,0"){
  msg1.payload = 0;
  msg2.payload = 1; 
  return [msg1,msg2]}

Toshi
Reply all
Reply to author
Forward
0 new messages