[{"id":"f73bbce9.98ce6","type":"tab","label":"Loop with IO Status","disabled":false,"info":""},{"id":"498c6c69.672984","type":"function","z":"f73bbce9.98ce6","name":"Simple array","func":"var StrIO = msg.payload // String from Netiom 16 characters long\n\n\n/////Run the loop\n\nvar i;\n\nlet Temp = [];\nlet T = [];\n \nfor (i = 0; i < StrIO.length; i++) {\n\n/////////////////////////////////////////////////////////\n\n\n\n Temp[i]='Tempin'+i; \n T[i]='T'+i; \n\nTemp[i] = context.get(T[i]);\nif (Temp[i] === undefined){\ncontext.set(T[i], \"0\");\n}\nTemp[i] = context.get(T[i]);\n \n \n\n// New trigger\nif (Temp[i] === \"0\" && StrIO[i] === \"1\") {\n\n context.set ('T100', \"0\");\n context.set(T[i], \"1\"); // new\n context.set ('ID', [i]);\n //\n //Reset of trigger\n \n \n \n } else if (Temp[i] === \"1\" && StrIO[i] === \"0\"){\n\n context.set ('ID', [i]);\n context.set ('T100', \"1\");\n context.set(T[i], \"0\");\n Temp[i] = context.get(T[i]);//Reset\n context.set ('ID', [i])\n \n \n}\n \nelse if (Temp[i] === \"0\" && StrIO[i] === \"0\"){\n\n { continue; }\n \n}\n\n\nelse if (Temp[i] === \"1\" && StrIO[i] === \"1\"){\n\n { continue; }\n \n}\n\n\n\n\n\n\n\n\nvar out = {payload: \"Device ID = \" + [i] + \" \" + Temp[i] + StrIO[i] }\n\n\nnode.send({payload: out});\n\n \n///////////////////////////////////////////////////////////////// \n \n }\n \n \n\n\n\n\n\n\n\n\n \n\n\n\n\n\n","outputs":1,"noerr":0,"x":350,"y":300,"wires":[["2d9f9409.018abc"]]},{"id":"8a555f4c.63a4f","type":"inject","z":"f73bbce9.98ce6","name":"","topic":"","payload":"0000000000000000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":260,"wires":[["498c6c69.672984"]]},{"id":"a3dd4a85.ad3638","type":"inject","z":"f73bbce9.98ce6","name":"","topic":"","payload":"1111111111111111","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":320,"wires":[["498c6c69.672984"]]},{"id":"2d9f9409.018abc","type":"debug","z":"f73bbce9.98ce6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":550,"y":400,"wires":[]},{"id":"8117de1f.29779","type":"comment","z":"f73bbce9.98ce6","name":"This simple array output creates a sql string that i feed into a node-mssql (Changed to a shorter output for the purpose of help)","info":"","x":900,"y":60,"wires":[]},{"id":"c55ff86d.1b1178","type":"comment","z":"f73bbce9.98ce6","name":"I use the following in my function to output the data","info":"","x":670,"y":100,"wires":[]},{"id":"da8b25b6.3fa5b8","type":"comment","z":"f73bbce9.98ce6","name":"var out = {payload: \"Device ID = \" + [i] + \" \" + Temp[i] + StrIO[i] }; ","info":"","x":710,"y":140,"wires":[]},{"id":"2cbaddc1.74a032","type":"comment","z":"f73bbce9.98ce6","name":"node.send({payload: out});","info":"","x":590,"y":180,"wires":[]},{"id":"f42db4f.d81bf48","type":"comment","z":"f73bbce9.98ce6","name":"Using this output way fails to inject into SQL as the debug shows The msg.payload is showing as object","info":"","x":830,"y":220,"wires":[]},{"id":"facd9243.19e9a","type":"comment","z":"f73bbce9.98ce6","name":"10/11/2020, 12:19:26node: 57171b15.b5d274 msg.payload : Object { payload: \"Device ID = 10 00\" }","info":"","x":820,"y":280,"wires":[]},{"id":"4b89d7c5.e2c558","type":"comment","z":"f73bbce9.98ce6","name":"Where as if I used a insert and create a msg.payload it reports as msg.payload string and this populates the data base.","info":"","x":880,"y":340,"wires":[]}]
Your function node is sending a message that has the following structure:
msg = {
payload: {payload:"Your String"}
}
In this way, your message payload is equal to the following Object:
{
payload:"Your String"
}
To return directly the string, you have to replace
node.send({payload:out}
with
node.send(out)
Good Afternoon
I just wanted to thank you for you help not only did it work instantaneously but it has saved be stopping up for a third night, I really do struggle with the whole payload message part of node red, your explanation was very intuitive.
Again thank you
Regards
Andrew
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.