Function with 2 outputs

#1

Hi,

I know it's stupid, but I have found allready different 'methods' to control 2 outputs, and for me, none of them work.

What is the idea?
I have 1 input & 2 outputs.

If the input is 'True', I want a '1' on output 1,
if thhe output is 'false', I want a '1' on output 2.

That's all...

I've tried this, but nothing passes the function

var x = msg.payload;

if (x =="TRUE"){
  var msg_o = {payload : 1}
  return [msg_o, null]}
else if (x =="FALSE"){
   var msg_o = {payload : 1}
  return [null, msg_o]}

I think my returns, are good, this is what I've found on the node-red functions page

thx

0 Likes

#2

You should debug input value

   var x = msg.payload;
    node.warn(x)
    //... rest of your code

And look what you see at debug tab.
"TRUE" does not equal "True"

0 Likes

#3

Good tip, I need lower case letters like true and false, but still no result at the end.

var x = msg.payload;
 
if (x =="true"){
  var msg_o = {payload : 1} 
  return [msg_o, null]}
else if (x =="false"){
  var msg_o = {payload : 1} 
  return [null, msg_o]}

There really is a true or false at the input

image

0 Likes

#4

Those are not the string values "true" and "false" - they are boolean values true and false.

So the test you want is

if (x == true) {

which is equivalent to:

if (x) {

All told, you can simplify your function to:

var x = msg.payload;
msg.payload = 1;
if (x) {
  return [msg, null]
} else {
  return [null, msg]
}

Note how it reuses the msg passed into the function - it is generally a best practise to pass on the message you receive rather than create a new message object as there may be other message properties on it that you want to keep ahold of.

0 Likes

#5

Hi Knolleary,

that worked and I understand that it now passes the original message and this solution works in my application

But I don't get why my value (payload :1) does not pass, because I do exactely the same, I only have a 'fixed' value that will go to the output.

Br

Nick

0 Likes

#6

If you share the code you have, we can help.

0 Likes