---
jupytext:
  text_representation:
    format_name: myst
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

# OPC UA

{.lead}
OPC UA is a standard for reading data from industrial systems by name. This page covers how a client finds a value, the two ways to get it, and how to send it to Nominal.

{download}`Download this page as a notebook <opc-ua.md>` to run its code in JupyterLab. [Running the notebooks](notebooks/running.md) explains how to set it up.

## How OPC UA works

OPC UA is "a platform independent service-oriented architecture" that integrates all the functionality of the individual OPC Classic specifications into one extensible framework.[^opc-page] It models data with its meaning, such as units and relationships, and supports both client-server requests and publish-subscribe.[^opc-page]

A server holds its data in an address space, which represents data hierarchically, like files and folders.[^opc-page] A node is a "fundamental component of an AddressSpace".[^opc-part1] A client gets a node by its node ID, such as `i=85` in asyncua's example, and reads its value.[^asyncua]

## Polling and subscriptions

A client can get a value in two ways:

- **Polling.** The client reads the node on its own schedule, and gets a value each time, changed or not.
- **A subscription.** A subscription is a client-defined endpoint in the server, used to return notifications to the client.[^opc-part1] The client adds monitored items to it, each of which is a client-defined entity that monitors an attribute for new values.[^opc-part1] The server reports by exception: when a value changes, according to the client's criteria.[^opc-page]

Two intervals control a subscription. The sampling interval is the fastest rate at which the server samples its source for changes. The publishing interval is the rate at which the subscription tries to send a message to the client.[^opc-part4]

This simulation compares the two for a temperature that sits at one value for a while and then steps to another. It reads the temperature 10 times a second, and counts the values a simple report-on-change rule would send, which are the values that differ from the last one:

```{code-cell} ipython3
import matplotlib.pyplot as plt
import numpy as np
from learning.style import set_matplotlib_style

set_matplotlib_style()
```

```{code-cell} ipython3
t = np.arange(0, 60, 0.1)  # s: a read every 0.1 s
rng = np.random.default_rng(3)
temperature = np.where(t < 20, 21.0, np.where(t < 40, 24.5, 22.0))  # °C, in steps
temperature = np.round(
    temperature + rng.normal(0, 0.02, t.size), 1
)  # 0.1 °C resolution

changed = np.r_[
    True, np.diff(temperature) != 0
]  # values that differ from the last one

fig, ax = plt.subplots()
ax.plot(t, temperature, color="0.7", label=f"Polled: {t.size} values")
ax.plot(
    t[changed],
    temperature[changed],
    "o",
    label=f"Reported on change: {changed.sum()} values",
)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Temperature (°C)")
ax.legend()
plt.show()
```

The changed values describe the same temperature as the polled ones with far fewer values. The cost is that a value that hasn't changed isn't re-sent, so a quiet signal and a dead connection look alike unless the server's keep-alive messages, which it sends when there's nothing to report, tell them apart.[^opc-part4] Which to use depends on the data: poll when you want a value at a steady rate, and subscribe when changes matter more than the rate.

## In Nominal

Nominal's channels hold timestamped values, so each value you read becomes a point. This code reads a node with the asyncua library and streams it, polling once a second:[^asyncua]

```python
import asyncio
from datetime import datetime, timezone

from asyncua import Client
from nominal.core import NominalClient

client = NominalClient.from_profile("default")
dataset = client.get_dataset("<dataset RID>")


async def main():
    async with Client(url="opc.tcp://localhost:4840/freeopcua/server/") as opc:
        node = opc.get_node("ns=2;s=Furnace.Temperature")  # your node's ID
        with dataset.get_write_stream() as stream:
            while True:
                value = await node.read_value()
                now = datetime.now(timezone.utc)  # when the computer read the value
                stream.enqueue("furnace.temperature", now, float(value))
                await asyncio.sleep(1.0)


asyncio.run(main())
```

`get_write_stream` sends the values in batches.[^nominal-client] The timestamp is the time of the read, not of the measurement, so it can lag the real change by up to the polling period. [What the link means for your data](communications.md#what-the-link-means-for-your-data) explains why that matters.

## Further reading

- [Communications](communications.md), on the other links a test system uses, and how to check what each does to your data.
- OPC Foundation, [Unified Architecture](https://opcfoundation.org/about/opc-technologies/opc-ua/).
- OPC Foundation, [OPC 10000-4: Services](https://reference.opcfoundation.org/specs/OPC-10000-4/1), the full definitions of subscriptions and monitored items.
- opcua-asyncio, [the asyncua library](https://github.com/FreeOpcUa/opcua-asyncio).

## References

[^opc-page]: OPC Foundation, ["Unified Architecture"](https://opcfoundation.org/about/opc-technologies/opc-ua/). Retrieved 2026-11-30.
[^opc-part1]: OPC Foundation, ["OPC 10000-1: Overview and Concepts"](https://reference.opcfoundation.org/specs/OPC-10000-1/1), the definitions of Node, Subscription and MonitoredItem. Retrieved 2026-11-30.
[^opc-part4]: OPC Foundation, ["OPC 10000-4: Services"](https://reference.opcfoundation.org/specs/OPC-10000-4/1), sections 5.13.1.2 and 5.14.1.1. Retrieved 2026-11-30.
[^asyncua]: opcua-asyncio, [README](https://github.com/FreeOpcUa/opcua-asyncio), the `Client` example: `get_node` and `read_value`. Retrieved 2026-11-30.
[^nominal-client]: Nominal, `Dataset.get_write_stream` in the [nominal](https://pypi.org/project/nominal/) Python client, version 1.186.0. Retrieved 2026-11-30.
