Output OPC UA variables in one line without "null"

Hello,
I have two different variables that I query via the OPC UA Client. The current value of the two variables is to be written to a CSV file later. Both variables are initialised by two timestamp nodes. At the moment the variables are output like this "1667,3465576171875;null" and "null;386" but I want them to be output together. For example "1667,3465576171875;386".
Is it possible to query two variables in one timestamp with OPC UA? How can I query the variables at the same time so that they are output together?
I would be very grateful for your help.

[{"id":"8e7b8428.f9bb48","type":"OpcUa-Client","z":"d5733a5e.9e0868","endpoint":"90c88704.55a6d8","action":"read","deadbandtype":"a","deadbandvalue":1,"time":"1","timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","name":"","x":400,"y":160,"wires":[["4e92f94e.0d3b68"]]},{"id":"4e92f94e.0d3b68","type":"function","z":"d5733a5e.9e0868","name":"","func":"\nvar val1 = (msg.topic === \"ns=5;s=Arp.Plc.Eclr/Integer1\" ? msg.payload : null);\n\nvar val2 = (msg.topic === \"ns=5;s=Arp.Plc.Eclr/Real1\" ? msg.payload : null);\n\ncapa_log = (String(val2));\ncapa_log = capa_log.replace(\".\",\",\");\n\nvar log  = capa_log + \";\" + val1;\n\n\nreturn [{payload: log}];\n//return [val1];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":200,"wires":[["a7547ecf.d3731"]]},{"id":"a7547ecf.d3731","type":"debug","z":"d5733a5e.9e0868","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":280,"wires":[]},{"id":"cdbdea49.287418","type":"inject","z":"d5733a5e.9e0868","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Integer1","payload":"","payloadType":"date","x":210,"y":200,"wires":[["8e7b8428.f9bb48"]]},{"id":"4eeed648.c00038","type":"inject","z":"d5733a5e.9e0868","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Real1;datatype=Float","payload":"","payloadType":"date","x":230,"y":120,"wires":[["8e7b8428.f9bb48"]]},{"id":"90c88704.55a6d8","type":"OpcUa-Endpoint","endpoint":"opc.tcp://192.168.178.134:4840","secpol":"Basic256Sha256","secmode":"SignAndEncrypt","login":true}]

Output

why you don t save your variables as context variable?
Your problem seems to be that you want to define a variable which is not defined.
you can do sth like that:

context.set("val1",val1);

and now if you get the data from val2 you can handle val1 with

val1 = context.get("val1");

Hello,
my problem is that I want to trigger two different variables from OPC UA at the same time, how can I solve this with an inject node?


csv_file

I'm not sure if you can do this with only one client.
2 sugestions what you can do:
1.use a second client and use flow variables to get the data togehter in a string, array or object
2. to use the subscribe option of the client. so you will get every change of topic you set from the server
you just have inject once if the client runs after that you will get every change
example:
grafik

Or do it like that:

[{"id":"3863d872.5e6d78","type":"function","z":"48461642.eb259","name":"","func":"\nif (msg.topic == \"from your first ns register\")\n{\n    val1 = msg.payload;\n    context.set(\"val1\",val1);\n    return 0;\n}\n\nelse if (msg.topic == \"from your second ns register\")\n{\n    val2 = msg.payload;\n    val1 = context.get(\"val1\");\n\n    capa_log = (String(val2));\n    capa_log = capa_log.replace(\".\",\",\");\n    var log  = capa_log + \";\" + val1;\n\n    if (val2 === undefined)\n    {\n        return 0\n    }\n    else\n    {\n    return [{payload: log}];\n    }\n\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":320,"wires":[["96e8e147.90956"]]},{"id":"96e8e147.90956","type":"debug","z":"48461642.eb259","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":360,"wires":[]},{"id":"27734e05.85afa2","type":"inject","z":"48461642.eb259","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"from your first ns register","payload":"","payloadType":"date","x":140,"y":320,"wires":[["3863d872.5e6d78"]]},{"id":"fba7da4b.f2c2d8","type":"inject","z":"48461642.eb259","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"from your second ns register","payload":"157","payloadType":"num","x":220,"y":380,"wires":[["3863d872.5e6d78"]]}]

I have tried the code from (do it like that) but unfortunately I don't get any debugging states displayed.

can you set a debug node (with entire msg) behind the opcua client and show us what it says in both cases.

I also changed my example that you can have a look what i mean, cause i don t know your real msg.topic.

If I put the debug node after the ua client, both numbers arrive. Nothing is displayed after the function, not even an error message.

The modified code from you works the way I imagined it, now I just have to figure out how to do it with opc ua variables.

grafik

the info in the red box is your topic, set it in the example for the "from your first ns register"

if (msg.topic == "ns=5;s=Arp.Plc.Eclr/Real1")
{
    val1 = msg.payload;
    context.set("val1",val1);
    return 0;
}

else if (msg.topic == "ns=5;s=Arp.Plc.Eclr/Integer1")
{
    val2 = msg.payload;
    val1 = context.get("val1");

    capa_log = (String(val2));
    capa_log = capa_log.replace(".",",");
    var log  = capa_log + ";" + val1;

    if (val2 === undefined)
    {
        return 0
    }
    else
    {
    return [{payload: log}];
    }

}

or as i mentioned use the subscribe function of the opc client and save both different datas in differenz variables (flow- or global variable) then put the inject node to your function node and get your data in a time you need it

sth like that:

[{"id":"cd760f7b.868758","type":"OpcUa-Client","z":"48461642.eb259","endpoint":"90c88704.55a6d8","action":"subscribe","deadbandtype":"a","deadbandvalue":1,"time":"1","timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","name":"","x":400,"y":460,"wires":[["8ec243a9.a41d8"]]},{"id":"8ec243a9.a41d8","type":"function","z":"48461642.eb259","name":"","func":"if (msg.topic == \"ns=5;s=Arp.Plc.Eclr/Real1\")\n{\n    flow.set(\"var1\",msg.payload);\n}\nelse if (msg.topic == \"ns=5;s=Arp.Plc.Eclr/Integer1\")\n{\n    flow.set(\"var2\",msg.payload);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":460,"wires":[["ff320502.873978"]]},{"id":"ff320502.873978","type":"debug","z":"48461642.eb259","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":460,"wires":[]},{"id":"a70fafe6.7fdb68","type":"inject","z":"48461642.eb259","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Integer1","payload":"","payloadType":"date","x":210,"y":500,"wires":[["cd760f7b.868758"]]},{"id":"2a8f9cfb.a17c2c","type":"inject","z":"48461642.eb259","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Real1;datatype=Float","payload":"","payloadType":"date","x":210,"y":440,"wires":[["cd760f7b.868758"]]},{"id":"fc6a91b6.c6c71","type":"inject","z":"48461642.eb259","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":600,"wires":[["48904d18.6fe434"]]},{"id":"48904d18.6fe434","type":"function","z":"48461642.eb259","name":"","func":"var val1 =flow.get(\"var1\");\nvar val2 =flow.get(\"var2\");\n\n\ncapa_log = (String(val2));\ncapa_log = capa_log.replace(\".\",\",\");\nvar log  = capa_log + \";\" + val1;\nreturn [{payload: log}];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":410,"y":600,"wires":[[]]},{"id":"90c88704.55a6d8","type":"OpcUa-Endpoint","endpoint":"opc.tcp://192.168.178.134:4840","secpol":"Basic256Sha256","secmode":"SignAndEncrypt","login":true}]

Thank you very much, you have helped me a lot.
I think I will use the last code from you because I will add more variables later and they will be easier to add.

You re welcome!
Please click in the solved button under the message which solved your problem so that your question will be closed and added to solved list

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