# Adding items to opc-ua server contrib-opcua

**URL:** <https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906>\
**Category:** Industrial\
**Created:** [28 April 2022 06:32 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906 "2022-04-28T06:32:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [28 April 2022 06:32 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/1 "2022-04-28T06:32:09Z")

</div>

Hi all,

we installed counters for compressed-air-usage here in our factory, after getting their values into nodered,  
now i want to make their values available for third party software by opc-ua.

I set up a OPC-UA server using contrib opc-ua. Now i am having trouble to add items/variables to the server.

this is what my flow looks like:

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

Item Node:

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b295a3c810d080fe2c03cfff94f25f1aeaea27d1.png)

Server-Node:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/10979df6abbc94e7fd9de5318d8c303d0a2af3df.png)

Available tree in third party software:

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/956bbdd3ab362473fe3e8a2a7dc8dc109d99f3f6.png)

Are there additonal things in my flow needed to have the variables appear in the tree?  
Maybe somebody can provide a sample flow, where variables are added to the OPC-server.  
Server connection works fine.

---

<div class="post-metadata">

**Author:** ![mikakaraila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikakaraila/32/37496_2.png) [@mikakaraila](https://discourse.nodered.org/u/mikakaraila)\
**Post date:** [3 May 2022 04:42 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/2 "2022-05-03T04:42:06Z")

</div>

Look examples folder and file OPCUA-TEST-NODES.json  
It contains multiple examples and also addFolder and addVariable commands.

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [6 May 2022 04:59 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/3 "2022-05-06T04:59:56Z")

</div>

You meant the examples section in the nodered GUI? OPCUA doesn´t appear there for me.  
Would you be so kind and copy paste a flow here for me?

Edit: Found it on your github 🙂

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [10 May 2022 06:56 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/4 "2022-05-10T06:56:49Z")

</div>

After some playing around I was able to create a folder on the OPCUA-Server and store variables/values there. So first progress 😃 Now I am curious about the servers behaviour in case of powerloss/ nodered crash.  
I adressed the variables by their indentifiers after creating them on serverside.

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

Are these identifiers persistent after being created once or am I going to run into trouble?  
Because I tried restarting the server and saw that it initially came back "empty", and all folders and variables had to be set again?

---

<div class="post-metadata">

**Author:** ![mikakaraila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikakaraila/32/37496_2.png) [@mikakaraila](https://discourse.nodered.org/u/mikakaraila)\
**Post date:** [20 June 2022 16:15 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/5 "2022-06-20T16:15:18Z")

</div>

Use inject node that will always inject once as flow starts.  
Another option is to save XML and then load it always as server starts.  
Study examples and OPCUA-TEST-NODES.json there is tab with server.

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [21 June 2022 08:01 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/6 "2022-06-21T08:01:36Z")

</div>

Hi Mika,

I am trying the XML-way right now, but when try to save the xml, i get a warning and nothing is written to the xml:

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4a0ab624ef5a8a970ed0b23d7efd43d58a761fd3.png)

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a86bf41c0c765d288bab6c8a37e10bc299dddb3.png)

---

<div class="post-metadata">

**Author:** ![mikakaraila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikakaraila/32/37496_2.png) [@mikakaraila](https://discourse.nodered.org/u/mikakaraila)\
**Post date:** [21 June 2022 08:15 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/7 "2022-06-21T08:15:26Z")

</div>

This one works, so check your path that folder is existing and you have access rights to write file.

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

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [22 June 2022 07:00 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/8 "2022-06-22T07:00:04Z")

</div>

I tried again and made sure there is access to the file, but i´m still stuck with the same error.

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1df38e3b94de4feaab48a40595fabad0077f9c15.png)

I know the following screenshot is in german but it says: Access: Read & Write

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

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e514b0403c0ff3054cee9a3fe3de358df9331aae.png)

---

<div class="post-metadata">

**Author:** ![mikakaraila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikakaraila/32/37496_2.png) [@mikakaraila](https://discourse.nodered.org/u/mikakaraila)\
**Post date:** [22 June 2022 08:41 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/9 "2022-06-22T08:41:28Z")

</div>

Try relative path as I have tested this case.

---

<div class="post-metadata">

**Author:** ![mikakaraila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikakaraila/32/37496_2.png) [@mikakaraila](https://discourse.nodered.org/u/mikakaraila)\
**Post date:** [22 June 2022 08:45 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/10 "2022-06-22T08:45:12Z")

</div>

Code checks only msg.payload & msg.filename are defined. Optional msg.topic is used to save different namespaces.

```nohighlight
 case "saveAddressSpace":
                        if (msg.payload && msg.filename) {
                            // Save current server namespace objects to file
                            var namespace = addressSpace.getOwnNamespace(); 
                            // Use msg.topic to select namespace
                            if (msg.topic) {
                                verbose_log("Saving namespace index: " + msg.topic);
                                namespace = addressSpace.getNamespace(parseInt(msg.topic));
                            }
                            if (namespace) {
                                const xmlstr = namespace.toNodeset2XML();
                                fs.writeFileSync(msg.filename, xmlstr.toString(), {encoding: "utf8"});
                            }
                            else {
                                verbose_warn("No namespace to save to XML file, check namespace index: " + msg.topic);
                            }
                        }
                        else {
                            verbose_warn("Check msg object, it must contain msg.filename for the address space in XML format!");
                        }
                        break;

```

---

<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:** [21 August 2022 08:45 UTC](https://discourse.nodered.org/t/adding-items-to-opc-ua-server-contrib-opcua/61906/11 "2022-08-21T08:45:56Z")

</div>

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