# OPC UA multiple subscription

**URL:** <https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633>\
**Category:** General\
**Tags:** opcua\
**Created:** [6 October 2022 09:24 UTC](https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633 "2022-10-06T09:24:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexAbril](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@AlexAbril](https://discourse.nodered.org/u/AlexAbril)\
**Post date:** [6 October 2022 09:24 UTC](https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633/1 "2022-10-06T09:24:13Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f36a5a9855ed20033e9fc0984296463c55a602c.png)

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?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23f241c87cf58567d986b5d286ef735e2b9abbee.png)

```auto
[{"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":""}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 October 2022 09:46 UTC](https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633/2 "2022-10-06T09:46:18Z")

</div>

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...

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

```

---

<div class="post-metadata">

**Author:** ![AlexAbril](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@AlexAbril](https://discourse.nodered.org/u/AlexAbril)\
**Post date:** [6 October 2022 11:23 UTC](https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633/3 "2022-10-06T11:23:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 October 2022 11:38 UTC](https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633/4 "2022-10-06T11:38:20Z")

</div>

> [@AlexAbril](#):
>
> 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.

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c4d395763b10852c23a6b94eb6123f8d6ebfbb1.png)

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![AlexAbril](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@AlexAbril](https://discourse.nodered.org/u/AlexAbril)\
**Post date:** [6 October 2022 12:47 UTC](https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633/5 "2022-10-06T12:47:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 October 2022 12:48 UTC](https://discourse.nodered.org/t/opc-ua-multiple-subscription/68633/6 "2022-10-20T12:48:23Z")

</div>

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