OPC UA multiple subscription

With node-RED v.3.0.2 using node-red-contrib-opcua v.0.2.289 I have tried to subscribe to one variable and device from an OPC-UA server using msg.topic of an injection node and all is right, receiving data from the subscription.

As I need about 20 variables (combining HValues and Values) from 16 devices I have decided not to do it individually but using node-red-contrib-loop-processing v.0.5.1 and a function node. In this case I am not successful. Can some one contribute with some help?

[{"id":"0a6d152430586c6e","type":"tab","label":"OPC UA subscriptions","disabled":false,"info":"","env":[]},{"id":"db84297ca9205371","type":"function","z":"0a6d152430586c6e","name":"Parametres","func":"msg.topic = [];\nvar una = '';\nvar sensors = ['NI_0044', 'NI_0078'];\nfor (let i = 8; i < 9; i++) {\n    una = 'ns=2;s=DL' + '00'.substring(i.toString(10).length) + i;\n    for (let sensor of sensors) {\n        msg.topic.push(una + '.' + sensor + '.Value');\n        msg.topic.push(una + '.' + sensor + '.HValue');}}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":120,"wires":[["88acce5167fb1216","c6d9b08839d3b782"]]},{"id":"92575b6cd58a8858","type":"inject","z":"0a6d152430586c6e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":680,"y":120,"wires":[["db84297ca9205371"]]},{"id":"88acce5167fb1216","type":"debug","z":"0a6d152430586c6e","name":"debug 14","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1020,"y":80,"wires":[]},{"id":"c6d9b08839d3b782","type":"array-loop","z":"0a6d152430586c6e","name":"Cada parametre","key":"al6ba672f3823eea96","keyType":"msg","reset":true,"resetValue":"value-null","array":"topic","arrayType":"msg","x":1040,"y":120,"wires":[["9d5c911767fec06d"],["8205748b7616c874","04ddf73e2b6dde8a"]]},{"id":"9d5c911767fec06d","type":"debug","z":"0a6d152430586c6e","name":"debug 15","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1240,"y":100,"wires":[]},{"id":"a55fb040cf3288cb","type":"debug","z":"0a6d152430586c6e","name":"debug 16","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1220,"y":240,"wires":[]},{"id":"04ddf73e2b6dde8a","type":"OpcUa-Client","z":"0a6d152430586c6e","endpoint":"5e8523f827b31b5a","action":"subscribe","deadbandtype":"a","deadbandvalue":1,"time":10,"timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","folderName4PKI":"","name":"","x":1040,"y":200,"wires":[["a55fb040cf3288cb","c6d9b08839d3b782"]]},{"id":"8205748b7616c874","type":"debug","z":"0a6d152430586c6e","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1240,"y":140,"wires":[]},{"id":"5e8523f827b31b5a","type":"OpcUa-Endpoint","endpoint":"opc.tcp://172.16.3.55:4840","secpol":"None","secmode":"None","none":false,"login":true,"usercert":false,"usercertificate":"","userprivatekey":""}]

First there is almost always a way to process without looping. Loops introduce risk to node-red (a run-away loop will crash node-red) - in simple terms, uninstall the loop node. If you ever need to "loop" an array, use the built in split and join nodes.

Next, there is multiple examples built in to node-red-contrib-opcua .

CTRL-I → examples → node-red-contrib-opcua

In particular OPCUA-MULTI-SUB and OPCUA_READMULTIPLE

I suspect you need to subscribe to multiple nodesids by sending a msg.topic = multiple and the msg.payload as...

[ 
  { "nodeId": "ns=1;s=NodeIdString1" },
  { "nodeId": "ns=1;s=NodeIdString2" },
  { "nodeId": "ns=1;s=NodeIdString3" }
]

Thanks Steve-Mcl, as you say there is a risk using this kind of loops but in this case, if you see the function code in the flow, this is only a limited array of elements (by crossing limited and explicit, variables and devices).
I have tried to do what you explain using split and join nodes. With them I have get the same array of elements and then also the same failure trying to inject them to the OPC UA Client.
Also I tried msg.topic=multiple and msg.payload with the array. I will continue by testing the examples you explain and your particular suspect.
Thanks again for your help.

You would split --> modify --> join

Or in your case, instead of an array of strings, you create and array of objects in the format I specified and skip the loop/split/join altogether.

msg.topic = "multiple"
msg.payload = [];
const sensors = ['NI_0044', 'NI_0078'];
for (let i = 8; i < 9; i++) {
    const una = 'ns=2;s=DL' + '00'.substring(i.toString(10).length) + i;
    for (let sensor of sensors) {
        const n1 = { "nodeId": una + '.' + sensor + '.Value' }
        const n2 = { "nodeId": una + '.' + sensor + '.HValue' }
        msg.payload.push(n1)
        msg.payload.push(n2)
    }
}
return msg;
2 Likes

Great Steve-Mcl, directly from de function with the array at the msg.payload to the OPC UA Client do the job.

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