Async command node.send(msg), can one send to two outputs?

3,914 views
Skip to first unread message

Jéan Roux

unread,
Sep 11, 2016, 3:42:05 PM9/11/16
to Node-RED
Hi, using the async command in a function node, but cannot seam to send to two outputs with the async send command?

As per async in http://nodered.org/docs/writing-functions "Sending messages asynchronously".

Ben Hardill

unread,
Sep 11, 2016, 4:40:43 PM9/11/16
to Node-RED
 Showing us what you are actually doing is going to help here.

Are trying to call the node.send() more than once or passing an array of messages? Also how many output tabs does the function node have?

Jéan Roux

unread,
Sep 11, 2016, 5:07:12 PM9/11/16
to Node-RED
Ben, 

Want 2 outputs, no-1 for control logic, no-2 for status messages. I am doing a 4 leg sprinkling system, using setTimeout(), where a global variable which carries the time the sprinkler is switched on, before the timeout time switches it off. The function block:

       interval(function(){
            msg.payload = 'event,allsprinkleroff';
            msg.Smsg    = 'Leg ' + msg.nextleg + ' switched off, ready for waiting for borehole.';
            msg.nextleg = msg.nextleg;
            updateNodeStatus('green', 'Ready');
            node.send(msg);
        }, (legtime * 1000 * 60), 1);

Calls a function shown below.

The node-send shown in Red above, rather want to send to 2 outputs, msg.payload to the first output, and msg.Smsg to the 2nd.

function interval(func, wait, times){
    var interv = function(w, t){
        return function(){
            if(typeof t === "undefined" || t-- > 0){
                setTimeout(interv, w);
                try{
                    func.call(null);
                }
                catch(e){
                    t = 0;
                    throw e.toString();
                }
            }
        };
    }(wait, times);
    setTimeout(interv, wait);
}

Ben Hardill

unread,
Sep 11, 2016, 5:16:49 PM9/11/16
to Node-RED
You need to look higher up that same page http://nodered.org/docs/writing-functions#multiple-messages

node.send will take the same array of messages as mentioned there.

So to send 2 messages 1 to each output tab you would call

node.send([[msg1],[msg2]]);

note the double dimension array to signify the output tabs not to just send 2 messages to the first output

Jéan Roux

unread,
Sep 11, 2016, 5:23:36 PM9/11/16
to Node-RED
Did not see that ! Thanks, will try quickly.

Jéan Roux

unread,
Sep 11, 2016, 6:11:04 PM9/11/16
to Node-RED
Ii is work Ben, thank you.


OnI Sunday, September 11, 2016 at 11:23:36 PM UTC+2, Jéan Roux wrote:
IDid not see that ! Thanks, will try quickly.
Reply all
Reply to author
Forward
0 new messages