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.
- 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
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Protocol Level | len(ns) | int | R | Number of namespaces (using Python's built-in len()) |
ns[i] | Optional[str] | R | Index 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 → Indexreload()(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:
| Node | NodeId | Note |
|---|---|---|
| Root | i=84 | Root |
| Objects | i=85 | Business Data Root |
| Types | i=86 | Type Definition Root |
| Server | i=2253 | The Server Object Itself |
| ServerStatus | i=2256 | Server Status Structure |
| ServerStatus.CurrentTime | i=2258 | Current server time |
| NamespaceArray | i=2255 | Namespace Array |
| ServerArray | i=2254 | Server-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.