# OPC Communication

**URL:** <https://discourse.nodered.org/t/opc-communication/95789>\
**Category:** General\
**Tags:** function-node, opcua, join\
**Created:** [4 March 2025 13:55 UTC](https://discourse.nodered.org/t/opc-communication/95789 "2025-03-04T13:55:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [4 March 2025 13:55 UTC](https://discourse.nodered.org/t/opc-communication/95789/1 "2025-03-04T13:55:52Z")

</div>

Hey,

I am new to Node-Red and trying to learn.  
What i am trying to do is read 6 signals from the OPC. I send then with a Join block and read them out in array.

Code looks like this after the Join block:

_let msg1 = {};_  
_let msg2 = {};_  
_let msg3 = {};_  
_let msg4 = {};_  
_let msg5 = {};_

_if (msg.payload[5].payload === true && msg.payload[0].payload === false) {_

- msg1.payload = true;\*  
_} else {_
- msg1.payload = false;\*  
_}_

_if (msg.payload[5].payload === true && msg.payload[1].payload === false) {_

- msg2.payload = true;\*  
_} else {_
- msg2.payload = false;\*  
_}_

_if (msg.payload[5].payload === true && msg.payload[2].payload === false) {_

- msg3.payload = true;\*  
_} else {_
- msg3.payload = false;\*  
_}_

_if (msg.payload[5].payload === true && msg.payload[3].payload === false) {_

- msg4.payload = true;\*  
_} else {_
- msg4.payload = false;\*  
_}_

_if (msg.payload[5].payload === true && msg.payload[4].payload === false) {_

- msg5.payload = true;\*  
_} else {_
- msg5.payload = false;\*  
_}_

_return [msg1, msg2, msg3, msg4, msg5];_

This all works fine, what bothers me is sometimes it somehow jumps thrue every IF statement and makes a false +1 to the counter even tho the IF statement is not true.  
It seems to happen when one off the IF statements are true it jumps thrue all of them untill it comes to the correct one.

I am sorry if the explanation was bad , let me know and i will try my best to explain what my problem is. 🙂

Thank you!

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [4 March 2025 22:25 UTC](https://discourse.nodered.org/t/opc-communication/95789/2 "2025-03-04T22:25:12Z")

</div>

This code is a pain to look at. Can you put it in normal code block and format it correctly? And can you capture an incoming message which makes the code not work fine?

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 06:06 UTC](https://discourse.nodered.org/t/opc-communication/95789/3 "2025-03-05T06:06:02Z")

</div>

Hey,  
Sorry i will try to provide better information.

 ![node1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0da82c991d35c4c6e0627dae7924b1886f8d896c.png)

This is the blocks how the blocks are put togheter.

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

Here a TRUE signal gets send on [2] msg from the array in one cycle then goes back to false even tho the OPC did not send the signals so the IF statment in function 6 should not have been true. ( 5 messages every secound thats why i split it with the red line to seperate the cycle time )

Function 5 example :

```js
var msg1 = {};
var msg2 = {};
var msg3 = {};
var msg4 = {};
var msg5 = {};
var msg6 = {};

msg1.topic = "ns=2;s=A";
msg2.topic = "ns=2;s=B";
msg3.topic = "ns=2;s=C";
msg4.topic = "ns=2;s=D";
msg5.topic = "ns=2;s=E";
msg6.topic = "ns=2;s=F";

return [msg1, msg2, msg3, msg4, msg5, msg6];

```

Function 6 example :

```js
let msg1 = {};
let msg2 = {};
let msg3 = {};
let msg4 = {};
let msg5 = {};

if (msg.payload[5].payload === true && msg.payload[0].payload === false) {
    msg1.payload = true;
} else {
    msg1.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[1].payload === false) {
    msg2.payload = true;
} else {
    msg2.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[2].payload === false) {
    msg3.payload = true;
} else {
    msg3.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[3].payload === false) {
    msg4.payload = true;
} else {
    msg4.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[4].payload === false) {
    msg5.payload = true;
} else {
    msg5.payload = false;
}

return [msg1, msg2, msg3, msg4, msg5];

```

I am not sure what you ment by normal code block and to format it correctly sorry , its my first time with node-red and json coding. Only been coding PLCs before this. Hopefully its easier to read now ?

Thank you for your time!

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 06:51 UTC](https://discourse.nodered.org/t/opc-communication/95789/4 "2025-03-05T06:51:51Z")

</div>

I just now saw when debugging the JOIN node that the objects gets swaped around. This is most likely to cause the problem. But i don't know how to fix it.

Example :

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

So on one cycle 5th object is looking at the right signal and secound cycle it swaps and looks at another signal then back again for next cycle.

---

<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:** [5 March 2025 07:03 UTC](https://discourse.nodered.org/t/opc-communication/95789/5 "2025-03-05T07:03:07Z")

</div>

> [@Mysmoo](#):
>
> I just now saw when debugging the JOIN node that the objects gets swaped around

This i can happen due to the async nature of network communications. In short, do not use array mode on the join node when async responses are possible since results can arrive out of order. In these cases, use key/Val object mode.

Better still, if you use readmultiple mode on the opcua node, you can avoid using the join node altogether. That has the benefit of providing all results in one message and more importantly, consistent data.

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 07:12 UTC](https://discourse.nodered.org/t/opc-communication/95789/6 "2025-03-05T07:12:19Z")

</div>

Hey Steve, Thanks for the reply.

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

If i set the OPC Client to Read Multiple the node says " nodeid stored" and does not send anything to the debugg node.

---

<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:** [5 March 2025 07:16 UTC](https://discourse.nodered.org/t/opc-communication/95789/7 "2025-03-05T07:16:01Z")

</div>

In read multiple node, you send the topics once , then for polling, you only need to send a topic of "readmultiple" and all results will arrive once in the bottom output payload.

I'm pretty sure there is an example flow built in - examples are available when you hit `CTRL + I`

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 07:39 UTC](https://discourse.nodered.org/t/opc-communication/95789/8 "2025-03-05T07:39:38Z")

</div>

I will try to figure it out 😃 . I will let you know if i succeed or not. Thanks alot for the help!

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [5 March 2025 08:21 UTC](https://discourse.nodered.org/t/opc-communication/95789/9 "2025-03-05T08:21:49Z")

</div>

Maybe too late now, but the code you shared originally could be replaced with something like this:

```javascript
const messages = [];
for(let i = 0; i < 5; i++){
  messages.push({
    payload: msg.payload[5].payload === true && msg.payload[i].payload === false
  });
}

return [messages];

```

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [5 March 2025 08:29 UTC](https://discourse.nodered.org/t/opc-communication/95789/10 "2025-03-05T08:29:59Z")

</div>

> [@Mysmoo](#):
>
> I am not sure what you ment by normal code block and to format it correctly sorry , its my first time with node-red and json coding. Only been coding PLCs before this. Hopefully its easier to read now ?
> 
> Thank you for your time!

Compare the code you posted in OP and later. It's hard to read in your first post. You made it very easy to read in the 2nd post, thanks!

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 08:38 UTC](https://discourse.nodered.org/t/opc-communication/95789/11 "2025-03-05T08:38:00Z")

</div>

Yeah i looked at other posts to see what the correct way was 😃 Thank you all for the amazing help. I am still trying to figure out OPC node with multiple read.

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 09:44 UTC](https://discourse.nodered.org/t/opc-communication/95789/12 "2025-03-05T09:44:42Z")

</div>

I am still trying my best to figure this out with the examples,

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/06b66a6bb3d57b9f7057c82caab90235cd318e19.png)

The problem i face now is that the message is not in an array in objects as it was before with the objects.

The "array of nodeids" function looks like this :

```auto
msg.topic = "readmultiple"
msg.payload = ["ns=2;s=A",
"ns=2;s=B",
"ns=2;s=C",
"ns=2;s=D",
"ns=2;s=E",
"ns=2;s=F"
];
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 March 2025 10:03 UTC](https://discourse.nodered.org/t/opc-communication/95789/13 "2025-03-05T10:03:28Z")

</div>

> [@Steve-Mcl](#):
>
> In read multiple node, you send the topics once , then for polling, you only need to send a topic of "readmultiple" and all results will arrive once in the bottom output payload.

Did you read Steve's post carefully? He said:  
In read multiple node, you send the topics once , then for polling, you only need to send a topic of "readmultiple" and all results will arrive once in the bottom output payload.

---

<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:** [5 March 2025 10:15 UTC](https://discourse.nodered.org/t/opc-communication/95789/14 "2025-03-05T10:15:46Z")

</div>

> [@Colin](#):
>
> Did you read Steve's post carefully

Also...

> [@Steve-Mcl](#):
>
> there is an example flow built in - examples are available when you hit `CTRL + I`

And in case you cannot find that - its here: [node-red-contrib-opcua/examples/OPCUA\_READMULTIPLE.json at master · mikakaraila/node-red-contrib-opcua · GitHub](https://github.com/mikakaraila/node-red-contrib-opcua/blob/master/examples/OPCUA_READMULTIPLE.json)

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 10:42 UTC](https://discourse.nodered.org/t/opc-communication/95789/15 "2025-03-05T10:42:08Z")

</div>

Thank you I will look into that.  
I did read it altho i did not really understand it and felt stupid to ask what you ment by polling.  
This is my bad sorry.

I will look at the example you sent the one i found was abit different as i did not find it with ctrl + I.

Thank you so much for your time 🙂

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 10:58 UTC](https://discourse.nodered.org/t/opc-communication/95789/16 "2025-03-05T10:58:51Z")

</div>

> [@Colin](#):
>
> all results will arrive once in the bottom output payload

The resault are the same as the other example i did. Problem is i need to use the payloads in a funktion where if one is true and one is false send true that then goes to a counter.  
But i don't know how to use it in a funkction when the outputs i get are like this :

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

as all i see is "msg payload : boolean - false " instad of object[1] topic "variable" paload : false "

I might be explaning it bad, but basicly what I am trying to make is :  
If variable 1 is true and not 2 then send signal to counter to start counting.

---

<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:** [5 March 2025 11:01 UTC](https://discourse.nodered.org/t/opc-communication/95789/17 "2025-03-05T11:01:37Z")

</div>

show me a screenshot of your flow

---

<div class="post-metadata">

**Author:** ![Mysmoo](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mysmoo](https://discourse.nodered.org/u/Mysmoo)\
**Post date:** [5 March 2025 11:05 UTC](https://discourse.nodered.org/t/opc-communication/95789/18 "2025-03-05T11:05:18Z")

</div>

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

I have read multiple items on interval every 1 sec

---

<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:** [5 March 2025 11:06 UTC](https://discourse.nodered.org/t/opc-communication/95789/19 "2025-03-05T11:06:37Z")

</div>

As stated before:

> [@Steve-Mcl](#):
>
> all results will arrive once in the bottom output payload.

> [@Colin](#):
>
> and all results will arrive once in the bottom output payload.

---

<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:** [5 March 2025 11:12 UTC](https://discourse.nodered.org/t/opc-communication/95789/20 "2025-03-05T11:12:44Z")

</div>

Also, your node does not look right, it should have 3 outputs.

Ensure it is set to "READ MULTIPLE" mode  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/92049ad9b8ce23f69988c3cbe228a722ea640036.png)

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

[Next page](https://discourse.nodered.org/t/opc-communication/95789.md?page=2)
