Quo Vadis: Function nodes with multible outputs

Hi guys,

again probably a really simple question, but, again, I can´t figure it how somehow :roll_eyes:

Here is what I want to accomplish. I have a function node which is triggered when my outdoor light sensor falls under a certain threshold. This function node should now trigger two things

  1. Open my roller shutter (if closed) AND
  2. Switch of the lights (when on)

This is what I tried in order to "split" the two comments for the different actions:

if  (global.get("DayMode")== true)
    {
        msg.payload = "put:/nodes/74/attributes/271?target_value=0";
        return [msg, null]
        msg.payload = {"on": false};
        return [null, msg];
    }else{
        return null
    }

Obviously this didn´t work but maybe someone has an idea how to put that right :slight_smile:

When you use ‘return’ you leave the function. Take a look in the documentation about the function node and it use of asynchronous messages

Or return both payloads in same array.

if  (global.get("DayMode")== true) {
    return [{payload: "put:/nodes/74/attributes/271?target_value=0"},  {payload: {"on": false}}]`;
}else{
    return null;
}

If you need to return other message parts, then you will need to clone the msg and then return the two messages with the altered payloads.

1 Like

Beautiful @E1cid - that was exactly what I was looking for :smiley: Btw: Way more helpful than these constant "look into the documentation" answers :wink:

1 Like

But have you learnt as much as if you had spent a little while reading the docs, reading relevant parts of the w3schools site and working out how to do it yourself?

No @Colin, in fact I learned more. Nothing is more helpful than learning by solving actual problems rather than just reading through documentations that might or might not address the issue one is trying to solve. Therefore, I´m really grateful if people take their precious time to drop me some lines of code that I can then use to go on with self-teaching.

1 Like

I agree with you 100% but in this case, I gave you the reason your code wasn’t doing what you wanted it to do. Then I pointed you to the documentation that would give you your solution so you could learn more by solving your actual problem.

You did point out that the return ended the function, so second return is unreachable, but asynchronous messages was not the most relevant answer. All the OP really needed was pushing to send both responses in the same return, he could of read about asynchronous messages all day with out understanding that he could send both payloads in one go.

Some people could read documents all day long and still not understand, then when they get a simple example it clicks. Either way the OP learnt what their issue was and learnt how to resolve it, so this discussion is just moot

Or use node.send()which emits a message without exiting the function

There are always many roads leading to Rome :wink:

RTFM: Finding and reading the docs is in many cases not easy. But pointing to the relevant pieces of the doc may help to A) solve the current problem and B) learn where to find information (in the future).

Yes there are many ways to skin a cat, but As said not the most relevant. there was no need to use asynchronous messages. Again this is moot.

1 Like

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