Sending multiple payloads to different hosts at once

Hi! I have two input text fields where the user enters two variables: replySource and replyCode. The replySource variable acts as the ID of a single device in the network. For instance, in the code below, replySource "20" sends the payload replyCode AND replySource upon the click of a button to device 20 on that specific IP address. The payload ends up being msg.url for a POST request node. So far this part is working (I am not including here the rest of the flow, as the flow doesn't seem to be the problem).

However, there is a case in which I need to send the same payload in replyCode to all three devices in the network at the same time when the value in replySource is equal to 00 along with their original ID as replySource (that's why I use flow. set). I thought about the function below but does not seem to work. I am very new to NodeRed and Java, so I appreciate any guidance in this regard.

This is how the flow is organized

var replySourceVal = flow.get('replySource');

//SEND TO ONE DEVICE
if (replySourceVal == "20") { //Red
msg.payload = "192.168.0.20/reply";
}

//SEND TO ALL DEVICES
if (replySourceVal == "00") { 
for (var i = 0; i < 3; i++) {
        if (i==1){
          flow.set('replySource', "20");
          msg.payload = "192.168.0.20/reply";
        }
        if (i==2){
           flow.set('replySource', "30");
           msg.payload = "192.168.0.30/reply";
       }
       if (i==3){
           flow.set('replySource', "40");
           msg.payload = "192.168.0.40/reply";
       }
    }
}
return msg;

A very quick look at this, you can use node.send()
node.send does exactly that, sends without breaking out of the function node.

//SEND TO ALL DEVICES
if (replySourceVal == "00") { 
for (var i = 0; i < 3; i++) {
        if (i==1){
          flow.set('replySource', "20");
          msg.payload = "192.168.0.20/reply";
          node.send(msg);
        }
        if (i==2){
          flow.set('replySource', "30");
          msg.payload = "192.168.0.30/reply";
          node.send(msg);
       }
       if (i==3){
          flow.set('replySource', "40");
          msg.payload = "192.168.0.40/reply";
          node.send(msg);
       }
    }
}
//return msg - you don't need this (unless you do :-)

Hi Marcus, thank you so much for the suggestion. Adding node.send(); doesn't seem to work. For some reason, the payload is not sent, but instead, remains to be the same payload from the previous successful delivery to a single device (hope I am being clear). Is as if the function does not enter into the for loop. I tried using node.warn to do some debugging, but oddly enough it only works once.

Mmm.

I changed the loop slightly - and got the expected result.

for (var i = 0; i < 3; i++) {
    if (i == 0) {
        flow.set('replySource', "20");
        msg.payload = "192.168.0.20/reply";
        node.send(msg);
    }
    if (i == 1) {
        flow.set('replySource', "30");
        msg.payload = "192.168.0.30/reply";
        node.send(msg);
    }
    if (i == 2) {
        flow.set('replySource', "40");
        msg.payload = "192.168.0.40/reply";
        node.send(msg);
    }
}

Oh! hang on - you may need to include replySource or replyCode in your msg - what ever should be sent
node.send is async I believe

maybe attach it to msg.replySource or msg.replyCode - then use that in the next node along, as the flow var maybe changed at the time of the other sends (given its asynchronous nature)

Good suggestion. Indeed the changes in the code seem to work. However, I think the issue with the loop is somewhere in the button, as once it successfully delivers a message, then it stops sending the subsequent ones. For instance, if the host in 192.168.0.20/reply is down, then it moves to the next host which will deliver the right payload to the right device. Thank you @marcus-j-davies anyway for the time :slight_smile:

1 Like

why not make flow replySource an array of objects, then use a split node to separate the http requests.
e.g.

[{"id":"b3864866e9cd631c","type":"ui_text_input","z":"bf9e1e33.030598","name":"replySource","label":"Source","tooltip":"","group":"dfc9abd922154e9a","order":3,"width":0,"height":0,"passthru":true,"mode":"number","delay":"300","topic":"source","sendOnBlur":true,"className":"","topicType":"str","x":160,"y":2940,"wires":[["4f7ca82a.bab14"]]},{"id":"4f7ca82a.bab14","type":"join","z":"bf9e1e33.030598","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":310,"y":2980,"wires":[["008fea195280df1e"]]},{"id":"36d4bdfbad777b18","type":"ui_text_input","z":"bf9e1e33.030598","name":"replyCode","label":"Code","tooltip":"","group":"dfc9abd922154e9a","order":2,"width":0,"height":0,"passthru":true,"mode":"number","delay":"300","topic":"code","sendOnBlur":true,"className":"","topicType":"str","x":150,"y":3000,"wires":[["4f7ca82a.bab14"]]},{"id":"008fea195280df1e","type":"function","z":"bf9e1e33.030598","name":"","func":"if (msg.payload.source == \"00\") { \n msg.payload = [\n     {url: \"192.168.0.20/reply\", source:\"20\", code: msg.payload.code},\n     {url: \"192.168.0.30/reply\", source: \"30\", code: msg.payload.code},\n     {url: \"192.168.0.40/reply\", source: \"40\", code: msg.payload.code}\n         ]\n}else{\n    msg.payload = [{\n        url: `192.168.0.${msg.payload.source}/reply`, \n        source: msg.payload.source,\n        code: msg.payload.code\n    }];\n}\nflow.set(\"replySource\", msg.payload);\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":2980,"wires":[[]]},{"id":"fac0982110da23a1","type":"ui_button","z":"bf9e1e33.030598","name":"","group":"dfc9abd922154e9a","order":2,"width":0,"height":0,"passthru":false,"label":"Send","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"replySource","payloadType":"flow","topic":"topic","topicType":"msg","x":150,"y":3100,"wires":[["2cebd59f.44b05a"]]},{"id":"2cebd59f.44b05a","type":"switch","z":"bf9e1e33.030598","name":"ceck for one valid payload","property":"$count(payload[0].*)","propertyType":"jsonata","rules":[{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":3100,"wires":[["94bc3175.605be"]]},{"id":"94bc3175.605be","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":390,"y":3160,"wires":[["dbbd9ac25d0245b7"]]},{"id":"dbbd9ac25d0245b7","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload.url","pt":"msg","to":"url","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":3160,"wires":[["8a53079e89f84ec9"]]},{"id":"8a53079e89f84ec9","type":"debug","z":"bf9e1e33.030598","name":"http","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":3160,"wires":[]},{"id":"dfc9abd922154e9a","type":"ui_group","name":"InputCommand","tab":"e562a4a.39a9e58","order":4,"disp":true,"width":"6","collapse":false,"className":""},{"id":"e562a4a.39a9e58","type":"ui_tab","name":"Non-myth of the Noble Red's Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Needs a bit more validation on accepted sources.

1 Like

Thank you @E1cid. Indeed the flow as you suggested works perfectly. Just in case anyone is interested, I did a couple of tweaks but you can get the flow here or directly from E1cid's original reply:

[{"id":"2e55dac334a66988","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"749c787253047e22","type":"ui_text_input","z":"2e55dac334a66988","name":"replySource","label":"Source","tooltip":"","group":"aa948ca87c80c30e","order":3,"width":0,"height":0,"passthru":true,"mode":"number","delay":"0","topic":"source","sendOnBlur":true,"className":"","topicType":"str","x":150,"y":100,"wires":[["a82420ade7ebdddc","fc2794d0878bd9cc"]]},{"id":"a82420ade7ebdddc","type":"join","z":"2e55dac334a66988","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":320,"y":140,"wires":[["5bc5e4e4b6a9b2ae","36e6554f953b6115"]]},{"id":"d516b7b6b5cd4d12","type":"ui_text_input","z":"2e55dac334a66988","name":"replyCode","label":"Code","tooltip":"","group":"aa948ca87c80c30e","order":2,"width":0,"height":0,"passthru":true,"mode":"number","delay":"0","topic":"code","sendOnBlur":true,"className":"","topicType":"str","x":150,"y":160,"wires":[["a82420ade7ebdddc","a603847c0eb40766"]]},{"id":"5bc5e4e4b6a9b2ae","type":"function","z":"2e55dac334a66988","name":"","func":"if (msg.payload.source == \"00\") { \n msg.payload = [\n     {url: \"192.168.0.20/reply\", source:20, code: msg.payload.code},\n     {url: \"192.168.0.75/reply\", source: 30, code: msg.payload.code},\n     {url: \"192.168.0.38/reply\", source: 40, code: msg.payload.code}\n         ]\n}else{\n    msg.payload = [{\n        url: `192.168.0.${msg.payload.source}/reply`, \n        source: msg.payload.source,\n        code: msg.payload.code\n    }];\n}\nflow.set(\"replySource\", msg.payload);\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":140,"wires":[["dbef62bd002b01b7"]]},{"id":"f9216aaa8d826537","type":"ui_button","z":"2e55dac334a66988","name":"","group":"aa948ca87c80c30e","order":2,"width":0,"height":0,"passthru":false,"label":"Send","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"replySource","payloadType":"flow","topic":"topic","topicType":"msg","x":160,"y":260,"wires":[["0c8b81b7014b0d22"]]},{"id":"0c8b81b7014b0d22","type":"switch","z":"2e55dac334a66988","name":"ceck for one valid payload","property":"$count(payload[0].*)","propertyType":"jsonata","rules":[{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":260,"wires":[["e673b50d731f5ddc"]]},{"id":"e673b50d731f5ddc","type":"split","z":"2e55dac334a66988","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":400,"y":320,"wires":[["60a454b9bcf4151f"]]},{"id":"60a454b9bcf4151f","type":"change","z":"2e55dac334a66988","name":"","rules":[{"t":"move","p":"payload.url","pt":"msg","to":"url","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":320,"wires":[["15b1db897bd10129","0616499c7de59f2a"]]},{"id":"0616499c7de59f2a","type":"debug","z":"2e55dac334a66988","name":"http","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":360,"wires":[]},{"id":"36e6554f953b6115","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":80,"wires":[]},{"id":"dbef62bd002b01b7","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":140,"wires":[]},{"id":"a603847c0eb40766","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":170,"y":220,"wires":[]},{"id":"fc2794d0878bd9cc","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":130,"y":40,"wires":[]},{"id":"15b1db897bd10129","type":"http request","z":"2e55dac334a66988","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":810,"y":320,"wires":[[]]},{"id":"aa948ca87c80c30e","type":"ui_group","name":"Dev2","tab":"e562a4a.39a9e58","order":5,"disp":true,"width":"6","collapse":false,"className":""},{"id":"e562a4a.39a9e58","type":"ui_tab","name":"Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

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