hello all , im having input of array , in which displayname is different from one function node i want to seperates the message or elements into two outputs.
i want complete object to be seperated.
Hello ..
you can loop throught the array and if the DisplayName startsWith either MM2 or MM3 push the array element to newly defined arrays and then output them to the seperate outputs.
let mm2 = []
let mm3 = []
msg.payload.forEach(el => {
if (el.DisplayName.startsWith("MM2.")) {
mm2.push(el)
}
else if (el.DisplayName.startsWith("MM3.")) {
mm3.push(el)
}
})
return [{ payload: mm2 }, { payload: mm3 }]
Test Flow :
[{"id":"4d3938590c6cf87d","type":"inject","z":"54efb553244c241f","name":"data","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"nodeID\":1,\"applicationUri\":\"aaaa\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":2,\"applicationUri\":\"bbb\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":3,\"applicationUri\":\"ccc\",\"DisplayName\":\"MM3.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":4,\"applicationUri\":\"ddd\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":5,\"applicationUri\":\"eee\",\"DisplayName\":\"MM3.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":6,\"applicationUri\":\"fff\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"}]","payloadType":"json","x":330,"y":1860,"wires":[["7a5436554cf3129e","496dfc1050b6b95e"]]},{"id":"e6ef0f9ea7d918ec","type":"debug","z":"54efb553244c241f","name":"mm2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1800,"wires":[]},{"id":"7a5436554cf3129e","type":"function","z":"54efb553244c241f","name":"","func":"let mm2 = []\nlet mm3 = []\n\nmsg.payload.forEach(el => {\n\n if (el.DisplayName.startsWith(\"MM2.\")) {\n mm2.push(el)\n }\n\n else if (el.DisplayName.startsWith(\"MM3.\")) {\n mm3.push(el)\n }\n\n})\n\nreturn [{ payload: mm2 }, { payload: mm3 }]","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1860,"wires":[["e6ef0f9ea7d918ec"],["7b1be89f672a7ce4"]]},{"id":"7b1be89f672a7ce4","type":"debug","z":"54efb553244c241f","name":"mm3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1920,"wires":[]},{"id":"496dfc1050b6b95e","type":"debug","z":"54efb553244c241f","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":440,"y":1760,"wires":[]}]
Or a Split node to split the array into discrete messages, and a Switch node to route each message depending on the message property.
Thank you for your help , just wanted a small correction to handle when in a packet sometimes there may not be mm2 or sometimes may not be mm3
(post deleted by author)
{
"NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0055",
"ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
"DisplayName": "MM2.D0055",
"Value": {
"Value": 256.33554,
"SourceTimestamp": "2022-04-19T12:53:19.551Z"
}
},
{
"NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0056",
"ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
"DisplayName": "MM2.D0056",
"Value": {
"Value": 52.6,
"SourceTimestamp": "2022-04-19T12:52:40.558Z"
}
},
{
"NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0058",
"ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
"DisplayName": "MM2.D0058",
"Value": {
"Value": -0.061722744,
"SourceTimestamp": "2022-04-19T12:53:19.551Z"
}
},
{
"NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0289",
"ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
"DisplayName": "MM2.D0289",
"Value": {
"Value": 256.33554,
"SourceTimestamp": "2022-04-19T12:53:19.549Z"
}
},
{
"NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0290",
"ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
"DisplayName": "MM2.D0290",
"Value": {
"Value": 79.8,
"SourceTimestamp": "2022-04-19T12:53:01.561Z"
}
},
{
"NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0302",
"ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
"DisplayName": "MM2.D0302",
"Value": {
"Value": 256.72006,
"SourceTimestamp": "2022-04-19T12:53:16.565Z"
}
},
{
"NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0133",
"ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
"DisplayName": "MM2.D0133",
"Value": {
"Value": true,
"SourceTimestamp": "2022-04-19T12:53:46.552Z"
}
}
]```
Test flow to handle case when some values are missing ( array length is 0 )
[{"id":"4d3938590c6cf87d","type":"inject","z":"54efb553244c241f","name":"data without mm3","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0055\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0055\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0056\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0056\",\"Value\":{\"Value\":52.6,\"SourceTimestamp\":\"2022-04-19T12:52:40.558Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0058\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0058\",\"Value\":{\"Value\":-0.061722744,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0289\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0289\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.549Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0290\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0290\",\"Value\":{\"Value\":79.8,\"SourceTimestamp\":\"2022-04-19T12:53:01.561Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0302\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0302\",\"Value\":{\"Value\":256.72006,\"SourceTimestamp\":\"2022-04-19T12:53:16.565Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0133\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0133\",\"Value\":{\"Value\":true,\"SourceTimestamp\":\"2022-04-19T12:53:46.552Z\"}}]","payloadType":"json","x":260,"y":1840,"wires":[["7a5436554cf3129e"]]},{"id":"e6ef0f9ea7d918ec","type":"debug","z":"54efb553244c241f","name":"mm2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":1840,"wires":[]},{"id":"7a5436554cf3129e","type":"function","z":"54efb553244c241f","name":"","func":"let mm2 = { topic: \"mm2\", payload: [] }\nlet mm3 = { topic: \"mm3\", payload: [] }\n\nmsg.payload.forEach(el => {\n\n if (el.DisplayName.startsWith(\"MM2.\")) {\n mm2.payload.push(el)\n }\n\n else if (el.DisplayName.startsWith(\"MM3.\")) {\n mm3.payload.push(el)\n }\n\n})\n\nif (mm2.payload.length == 0) mm2 = null\nif (mm3.payload.length == 0) mm3 = null\n\nreturn [mm2, mm3]","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":1880,"wires":[["e6ef0f9ea7d918ec"],["7b1be89f672a7ce4"]]},{"id":"7b1be89f672a7ce4","type":"debug","z":"54efb553244c241f","name":"mm3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":1920,"wires":[]},{"id":"deeef12456b127cf","type":"inject","z":"54efb553244c241f","name":"data with both","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0055\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0055\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0056\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0056\",\"Value\":{\"Value\":52.6,\"SourceTimestamp\":\"2022-04-19T12:52:40.558Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0058\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM3.D0058\",\"Value\":{\"Value\":-0.061722744,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0289\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0289\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.549Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0290\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM3.D0290\",\"Value\":{\"Value\":79.8,\"SourceTimestamp\":\"2022-04-19T12:53:01.561Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0302\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0302\",\"Value\":{\"Value\":256.72006,\"SourceTimestamp\":\"2022-04-19T12:53:16.565Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0133\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0133\",\"Value\":{\"Value\":true,\"SourceTimestamp\":\"2022-04-19T12:53:46.552Z\"}}]","payloadType":"json","x":270,"y":1920,"wires":[["7a5436554cf3129e"]]}]