Multiple outputs of function node not working

my 1 liner does work! :slight_smile:

Proof of concept...

[{"id":"7b69f526.621aec","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["a6091beb.8838a8"]]},{"id":"2868a3fb.7658ec","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["a6091beb.8838a8"]]},{"id":"d9d7ecc3.46f0c","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":200,"wires":[["a6091beb.8838a8"]]},{"id":"94be3aca.6d5bf8","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":160,"wires":[["a6091beb.8838a8"]]},{"id":"a6091beb.8838a8","type":"function","z":"56fa9896.d15ed8","name":"","func":"return [ {payload : msg.payload == 0 ? 1 : 0}, {payload : msg.payload == 1 ? 1 : 0}, {payload : msg.payload == 2 ? 1 : 0}, {payload : msg.payload == 3 ? 1 : 0}];","outputs":4,"noerr":0,"x":350,"y":120,"wires":[["afe798d4.320b58"],["f2c45dcf.5cdad"],["25c8acdb.ebe174"],["7eadb353.c3a5dc"]]},{"id":"afe798d4.320b58","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":80,"wires":[]},{"id":"f2c45dcf.5cdad","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":120,"wires":[]},{"id":"25c8acdb.ebe174","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":160,"wires":[]},{"id":"7eadb353.c3a5dc","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":200,"wires":[]}]

That is if I'm guessing your requirements correctly.

What this does is send a message on every output BUT only one of the payloadds will be 1, the others will be 0

@afelix
Thanks for the clarification.

Is this "new" to how NR works?

I am sure I used to be able to do it the way I showed.

What I admit I missed is that how the values returned have to be "formatted".

So they have to be in a msg.payload format.

What confused me was that I could use variables rather than msg.... packets.

@Steve-Mcl
Thanks.

It isn't I don't like your reply, but I don't know that trick.

So I never thought of doing it that way.

I appreciate your replies. I just took to @afelix 's way because they explained what I did and explained my mistake.

I just hope I remember it next time.

Both: Very much appreciated.

Its all about learning & braking it down.

Several things come into play.

  1. you MUST return an object (e.g. {} )
  2. To fire multiple outputs you return an array [a,b,c,d]
  3. my "1 liner" returns 4 objects - lets look at the first one...
{payload : msg.payload == 0 ? 1 : 0} // this makes use of the ternary operator
//In words: create a new object {} & if msg.payload == 0, set  {payload:1} otherwise set {payload:0}
//this is done 4 times inside the return statement.

ternary operator

A breakdown of the ternary statement with comments...

{ //= start of a new object
  payload  :  //set property payload
  msg.payload == 0 ?  //if the incoming msg.payload == 0
  1 //set THIS objects 'payload' property to 1
  : //otherwise
  0 //set it to 0
} //end of new object 

Or perhaps you might better understand the long hand code...

var msg1 = {};        //create an empty object
if(msg.payload == 1){ //if input msg.payload == 1
  msg1.payload = 1;   //set new msg1.payload to 0
} else {              //otherwise
  msg1.payload = 0;   //set new msg1.payload to 0
}

You can see its the same but the much more verbose.

So basically, all my 1 liner does is 4 ternary statements in the return [] statement

return [ternary1,ternary2,ternary3,ternary4]
1 Like

Thanks.

Yeah. I admit I didn't know the structure.

I hope I can remember that structure (correct term?) for next time.

Yeah, I think that part has sunk in now.
But just so I am really sure....

It is ie: { }
Eg, is example. ie is THAT IS.
So that is the only way - right?
It has to be formatted as a message body.

Yes, {} is a new empty object.

However, I RARELY create a new object in my flows. You lose all of the valuable (helpful) information that can help you debug later on.

e.g. I much prefer to do this...

msg.theAnswer = msg.payload * 10;
return msg; //msg contains original .payload AND .theAnswer and everything else!

instead of ...

var newMsg = {}
newMsg.payload = msg.payload * 10;
return newMsg;//this is a new object and has only .payload!

reason...
In the 1st version, If I add a debug node after this function, I can see BOTH msg.payload AND msg.theAnswer. In the 2nd version (where I return newMsg), it contains ONLY .payload - nothing else. All prior information has been discarded and lost forever.

Sometimes - at the end of a flow, my msg has accumulated all sorts of values BUT its a massive help to be able to see it all in one debug message & helps me determine what happened to the msg as it travelled through the flow.

Yes, I agree with that fully.

I have found that happening to me many times too.

Though if the message is going a long way, after about 3 or so nodes, the original msg.payload is sometimes wiped because I can track it through a few nodes and make sure it is ok into the next node.

Then if all there is good, it is a case of tracking the new msg.payload.

No, it is not new. But until now with everything you posted about you could just use the msg object coming from the previous node. From what Iā€™ve read of your projects this is (one of the) first time(s) you create your own messages from nothing. Previously you could edit existing objects, like Steve describes above. And while you worked with the message object, you didnā€™t have to do it from scratch. Take a look at the following page from the docs if you have some spare time.

https://nodered.org/docs/user-guide/messages

As for @Steve-Mcl I understand your methods too, but try to keep in mind that youā€™re explaining more advanced techniques (ternary operators do not directly improve readability) plus one-liners to someone with only minor JavaScript knowledge and experience, as shown from previous threads where we both responded to. Itā€™s better to keep it simple for now and just make it work, then later improve it to more advanced techniques. The same with the object multiplication syntax, it definitely works, but, for example, what happens to the _msgid property on it if you do that? Start easy, and adding more advanced techniques to posts isnā€™t bad as long as the reason why it didnā€™t work in the first place is clear too :slight_smile: adding one liners and ternary isnā€™t bad, definitely not when you have to deal with JavaScript. But keep the target audience in mind and their level of understanding, if possible.

1 Like

Hi @afelix, I do agree. Normally (even in my own code )I am quite verbose & create variables for almost everything.

However, in this instance, the OP was way off (several basic misunderstandings in the code) so I got lazy & just offered a "working" solution. Though to be fair, I offered a full explanation afterwards :slight_smile:

Regarding readability, its easy to forget that things like ternary is a bit more advanced since now that I'm familiar with it, I find the compactness much easier to read than reams of if/else.

Thanks again both.

I have slacked off the past week or so.

Had a few things go pear shaped on me and I am ...... not as confident as I was.
And because I am not doing it daily, the retention is not as good as it should/could/would be.

:frowning:
Killed (supposedly) 2 Arduino's.
Lost a WAP.
About to get a $$$$$ bill for electrical re-wiring which wasn't expected.
(Need to move a power point and the consequences of that.) So it still needs to be done.

And in the mean time, other jobs are queuing up.

Bed calling.
Night!