pythonopc-ua

How to check whether a specific opc ua node already exists with asyncua?


How to check with asyncua (python) whether a node with a specific name exists already on the server?

If it exists it should be used if it doesn't exist then the node should be created.

Use case: The opcua-client has to write data on a opcua-server. The client also creates the objects and nodes for the data. If the client goes offline the server keeps the nodes and objects. When the client comes back online it has to check whether the server has the objects or has been reseted.


Solution

  • You can try to read any attribute from it:

    async def check_if_node_exists(client, node_id):
        node = client.get_node(node_id)
        try:
            _ = await node.read_browse_name()
            return True
        except BadNodeIdUnknown as e:
            return False
    
    node_id = "ns=4;s=ABC"
    if(check_if_node_exists(client, node_id)):
       print('found')
    else:
       print('not found')