I have a lot of change nodes which sets new properties to the msg.payload as shown below. I want to use function node instead of multiple change nodes which are complex to read! How can I achieve this? Please help, I am new to Node-Red!
When I connect this function node between time trigger node & OPC UA client node, its not returning anything like its returned with individual change node for each variable (as shown in my original first post)!
I am trying to read all the available values from opc server running on siemens PLC and store them all (on value change) in Influx DB as measurements.
In order to use OPC Ua Client node which reads & stores them all into the database server (Influxdb), I have to set the msg.topic & msg.measurement for each variable which I want to store! Flow:
FYI From OPC UA Client node & afterwards everything works fine!
Since I don't have a Siemens opc server or influxdb, or even necessarily know what they are, and you have not provided sample data, I will only point out that you probably only get one message from the function node versus two from the two change nodes.
You can send multiple messages from a function with node.send(msg);, or with return [msg1, msg2];
So this might work
msg.topic = 'a long string';
msg.payload = 'another long string';
node.send(msg);
msg.topic = 'a third long string';
msg.payload = 'yet another long string';
return msg;
Based on the function that you have listed i think you are missing a fundamental concept in relation to NR
Each message is a distinct entity - so in the case of the function code above - you have set the msg.topic and the msg.measurement twice. The function node will overwrite the first one of each that you set with the 2nd one - and you will get a single message spat out that has the last values - in your case
What we need is a debug node on the incoming data from the Siemens PLC showing the complete message object that comes in and we can then advise you on the best way to tackle it
Thank you so much for your suggestions/Help @craigcurtin
The OPC Client node can't read out the entire structure or PLC datablock (extension object), I tried to read out the entire datablock, but it returns null value! (see below screenshot)!
So far the only possibility is that we can read each & every variables/items individually
I tried using the opc items node also, to read out the entire structure (MachineState), but I received "TypeError: Cannot read property 'length' of undefined"
Please feel free to advice anything on this topic.
You may wish to note that doing it that way can occasionally catch you out since msg is a REFERENCE to the data and so making changes to it changes everywhere that the msg happens to be in use at the time of change.
While that won't probably impact you here, it can sometimes catch you out if your downstream nodes process things asynchronously.
it works perfect. I want to program it as a loop, because its easier if my machine has 1000 variables. I just have to define an array with 1000 elements (topic & measurment) and the msg array formation will be done by the function (future oriented), no need to form 1000 dictionary inside the array in hard coded way.
You can, of course, use node.send inside your loop and that is absolutely fine. Just create the output object for each send rather than reusing the incoming msg object.
Yes, well, nearly. You aren't getting any output because you've not passed a msg object to the send function. Send takes the same data that return does.