Skip to main content

Namespaces — ua.namespaces

OPC UA A node consists NamespaceIndex + Identifier of NodeId, but NamespaceIndex Unstable — A server restart may cause changes. Production code must use NamespaceUri reverse lookup indexes; do not hard-code them ns=2.

Prerequisite Reading
  • Standard = ns0; please refer to the node constants WellKnownNodes.
  • For information on node and Browse path resolution, please refer to Nodes.

NamespaceCollection Public Members​

OpcUaSession.namespaces is NamespaceCollection; on connect() it is automatically loaded from the server NamespaceArray (i=2255).

Authentic signature:

class NamespaceCollection:
def __init__(self, session: OpcUaSession): ...
def __len__(self) -> int: ... # Number of Namespaces
def __getitem__(self, index: int) -> Optional[str]: ... # Index → URI, Returns None if out of bounds
def index_of(self, uri: str) -> int: ... # URI → Index; if not found, return -1
def reload(self) -> StatusCode: ... # Reload the server NamespaceArray
def __iter__(self): ... # Yield one by one URI
CategoryPropertyTypeAccessDescription
Protocol Levellen(ns)intRNumber of namespaces (using Python's built-in len())
ns[i]Optional[str]RIndex access; returns the URI corresponding to this Index (None if out of bounds)

Methods​

  • index_of(uri) (int, -1 if not found) — Reverse Lookup URI → Index
  • reload()(StatusCode) — Proactive server re-read NamespaceArray (when the server changes dynamically)

Iteration Support: for uri in ua.namespaces: ...

Common Uses​

Reverse Lookup: Index Spelling NodeId​

ua.connect()
idx = ua.namespaces.index_of("urn:my-company:plc1")
if idx < 0:
raise RuntimeError("Server does not have my namespace")

node_id = f"ns={idx};s=Temperature"
with ua.read(node_id) as dv:
print(dv.value)

List all namespaces (debug)​

for i, uri in enumerate(ua.namespaces):
print(f" [{i}] {uri}")

Output example:

[0] http://opcfoundation.org/UA/
[1] urn:server-host
[2] urn:my-company:plc1
[3] http://my-company.com/UA/

Dynamic Namespace Changes​

Some servers register their namespaces only after they start up (plug-in style), so the information obtained by connect is incomplete NamespaceArray. In this case, call re-read reload()reload():

ua.namespaces.reload()
idx = ua.namespaces.index_of("urn:plugin:loaded-after-startup")

Standard namespace (ns=0)​

OPC Foundation It is specified that Index 0 is always http://opcfoundation.org/UA/, and all standard nodes have ns=0:

NodeNodeIdNote
Rooti=84Root
Objectsi=85Business Data Root
Typesi=86Type Definition Root
Serveri=2253The Server Object Itself
ServerStatusi=2256Server Status Structure
ServerStatus.CurrentTimei=2258Current server time
NamespaceArrayi=2255Namespace Array
ServerArrayi=2254Server-side array

Or use a constant (recommended):

from darra_opcua import well_known_nodes as wkn   # Module Name darra_opcua

with ua.read(wkn.SERVER_SERVER_STATUS_CURRENT_TIME) as dv:
print(dv.value)

For more information, see WellKnownNodes.

Next Step​