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.
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?
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 :
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];
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 ?
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.
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.
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
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.
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.
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.
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 :