# OpcUaDevice

## class OpcUaDevice( name: str, config: OpcUaDeviceConfig | dict[str, object] | Path | str, publishers: list[Publisher] | None = None, *, autostart: bool = False, )[source]

Bases: [`Instrument`](https://dev.nominal.io/instro/ref/library/instro.lib.instrument.Instrument.md#instro.lib.instrument.Instrument "instro.lib.instrument.Instrument")

Own a configured OPC UA client and publish samples from a configured subscription.

Validate a config and initialize the device.

Parameters:
:   - **name** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"))

      Instrument name.
    - **config** ([*OpcUaDeviceConfig*](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDeviceConfig.md#instro.opcua.OpcUaDeviceConfig "instro.opcua.OpcUaDeviceConfig") *|* [*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")*[*[*str*](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")*,* [*object*](https://docs.python.org/3/builtins/functions.html#object "(in Python v3.15)")*]* *|* *Path* *|* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"))

      Config model,
      mapping, or JSON file path.
    - **publishers** ([*list*](https://docs.python.org/3/builtins/stdtypes.html#list "(in Python v3.15)")*[*[*Publisher*](https://dev.nominal.io/instro/ref/library/instro.lib.publishers.publisher.Publisher.md#instro.lib.publishers.publisher.Publisher "instro.lib.publishers.publisher.Publisher")*]* *|* *None*)

      Publishers owned by the instrument.
    - **autostart** ([*bool*](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)"))

      Open the connection and start any configured subscription
      during construction.

Examples

```
>>> with OpcUaDevice("line_1", "line_1.json") as device:
...     sample = device.client.get_node("/Objects/2:Setpoint").value
```

Attributes

| [`background_interval`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.background_interval "instro.opcua.OpcUaDevice.background_interval") | Seconds between background daemon iterations (0 = no wait). |
| --- | --- |
| [`background_running`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.background_running "instro.opcua.OpcUaDevice.background_running") | Whether the background daemon thread is running. |
| [`channel_names`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.channel_names "instro.opcua.OpcUaDevice.channel_names") | Return an insertion-ordered snapshot of all published channel names. |
| [`client`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.client "instro.opcua.OpcUaDevice.client") | Return the client for the open device. |

Methods

| [`add_background_daemon_function`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.add_background_daemon_function "instro.opcua.OpcUaDevice.add_background_daemon_function") | Append `method` to the daemon's call list. |
| --- | --- |
| [`add_publisher`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.add_publisher "instro.opcua.OpcUaDevice.add_publisher") | Register a publisher to receive this instrument's Measurement/Command data. |
| [`close`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.close "instro.opcua.OpcUaDevice.close") | Drain subscription samples, close publishers, and disconnect the client. |
| [`define_background_daemon`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.define_background_daemon "instro.opcua.OpcUaDevice.define_background_daemon") | Replace all daemon functions with a single `method` (called with the given args). |
| [`get_channel`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.get_channel "instro.opcua.OpcUaDevice.get_channel") | Return the most recent `length` samples for `channel_name` from the in-memory buffer. |
| [`get_single_channel_value`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.get_single_channel_value "instro.opcua.OpcUaDevice.get_single_channel_value") | Return the most recent sample for `channel_name` from the in-memory buffer. |
| [`open`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.open "instro.opcua.OpcUaDevice.open") | Connect and start publishing data, if configured. |
| [`publish`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.publish "instro.opcua.OpcUaDevice.publish") | Fan `data` out to every configured publisher; `kwargs` pass through. |
| [`read`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.read "instro.opcua.OpcUaDevice.read") | Read a node's Value attribute and publish one measurement. |
| [`start`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.start "instro.opcua.OpcUaDevice.start") | Start the background daemon thread. |
| [`stop`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.stop "instro.opcua.OpcUaDevice.stop") | Signal the background daemon to stop and join it. |
| [`write`](https://dev.nominal.io/instro/ref/protocols/instro.opcua.OpcUaDevice.md#instro.opcua.OpcUaDevice.write "instro.opcua.OpcUaDevice.write") | Publish a command and write the node's Value attribute. |

### property client: OpcUaClient

Return the client for the open device.

Raises:
:   [**RuntimeError**](https://docs.python.org/3/builtins/exceptions.html#RuntimeError "(in Python v3.15)") – The device has not opened or has closed.

### open() → None[source]

Connect and start publishing data, if configured.

Browse paths are resolved in configuration order, with translation requests
split as needed to fit the client’s decoded array limit.

### add_background_daemon_function( method: Callable, *args, **kwargs, )

Append `method` to the daemon’s call list. Use `define_background_daemon` to replace instead.

### add_publisher( publisher: Publisher, )

Register a publisher to receive this instrument’s Measurement/Command data.

### property background_interval

Seconds between background daemon iterations (0 = no wait).

### property background_running: bool

Whether the background daemon thread is running.

### property channel_names: tuple[str, ...]

Return an insertion-ordered snapshot of all published channel names.

Names are complete after one full daemon iteration: {name}.loop_time publishes
every iteration, so call get_channel(“loop_time”, wait_for_new_samples=True)
then read channel_names to ensure the complete set has arrived.

Raises:
:   [**RuntimeError**](https://docs.python.org/3/builtins/exceptions.html#RuntimeError "(in Python v3.15)") – No background buffer; `start()` was not called.

### define_background_daemon( method: Callable, *args, **kwargs, )

Replace all daemon functions with a single `method` (called with the given args).

### get_channel( channel_name: str, length: int = 1, wait_for_new_samples: bool = False, timeout: float = 10.0, ) → Measurement

Return the most recent `length` samples for `channel_name` from the in-memory buffer.

If the channel does not exist yet, or no sample is available, the code will always block until `timeout` expires.

Parameters:
:   - **channel_name**

      Name of the channel to retrieve.
    - **length**

      Number of trailing samples to return.
    - **wait_for_new_samples**

      Block until at least `length` new values arrive.
    - **timeout**

      Seconds to wait when insufficient data exists or `wait_for_new_samples=True`.

Raises:
:   - [**RuntimeError**](https://docs.python.org/3/builtins/exceptions.html#RuntimeError "(in Python v3.15)") – No background buffer; `start()` was not called.
    - [**ChannelNotFoundError**](https://dev.nominal.io/instro/ref/library/instro.lib.publishers.channel_buffer.ChannelNotFoundError.md#instro.lib.publishers.channel_buffer.ChannelNotFoundError "instro.lib.publishers.channel_buffer.ChannelNotFoundError") – channel had no values and no data appeared before `timeout`.
      `wait_for_new_samples=True` and channel did not appear within `timeout`.
    - [**ChannelValueTimeoutError**](https://dev.nominal.io/instro/ref/library/instro.lib.publishers.channel_buffer.ChannelValueTimeoutError.md#instro.lib.publishers.channel_buffer.ChannelValueTimeoutError "instro.lib.publishers.channel_buffer.ChannelValueTimeoutError") – `wait_for_new_samples=True` and values did not arrive within `timeout`.

### get_single_channel_value( channel_name: str, ) → float | str | None

Return the most recent sample for `channel_name` from the in-memory buffer.

This will not wait, if data is not available then `None` is returned.

Parameters:
:   **channel_name**

    Name of the channel to retrieve.

Raises:
:   [**RuntimeError**](https://docs.python.org/3/builtins/exceptions.html#RuntimeError "(in Python v3.15)") – No background buffer; `start()` was not called.

### publish( data: Measurement | Command, **kwargs, )

Fan `data` out to every configured publisher; `kwargs` pass through.

### read( node_or_path: str | RelativePath | Node, ) → Measurement[source]

Read a node’s Value attribute and publish one measurement.

Use the source timestamp when available, then the server timestamp,
then the local receipt time.

Parameters:
:   **node_or_path** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)") *|* [*RelativePath*](https://dev.nominal.io/instro/ref/protocols/instro.opcua.types.RelativePath.md#instro.opcua.types.RelativePath "instro.opcua.types.RelativePath") *|* [*Node*](https://dev.nominal.io/instro/ref/protocols/instro.opcua.Node.md#instro.opcua.Node "instro.opcua.Node"))

    Rooted browse path or node from this client.

Returns:
:   *Measurement* – The published sample with a Unix timestamp in nanoseconds.

Raises:
:   - [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – The node belongs to another client, the sample has non-Good
      quality, or the integer value would lose precision.
    - [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – The sample value cannot be published as a scalar.

### start()

Start the background daemon thread. No-op if already running.

### stop()

Signal the background daemon to stop and join it. No-op if not running.

### write( node_or_path: str | RelativePath | Node, value: bool | int | float | str, *, dtype: DataType | None = None, ) → None[source]

Publish a command and write the node’s Value attribute.

Publication records intent before submission. A rejected write still
raises, and a publisher error prevents submission.

Parameters:
:   - **node_or_path** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)") *|* [*RelativePath*](https://dev.nominal.io/instro/ref/protocols/instro.opcua.types.RelativePath.md#instro.opcua.types.RelativePath "instro.opcua.types.RelativePath") *|* [*Node*](https://dev.nominal.io/instro/ref/protocols/instro.opcua.Node.md#instro.opcua.Node "instro.opcua.Node"))

      Rooted browse path or node from this client.
    - **value** ([*bool*](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)") *|* [*int*](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)") *|* [*float*](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)") *|* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"))

      Scalar value to write and publish.
    - **dtype** (*DataType* *|* *None*)

      Wire type override for the write.

Raises:
:   - [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – The node belongs to another client or the integer value would lose precision.
    - [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – The value cannot be published as a scalar.
    - [**OpcUaError**](https://dev.nominal.io/instro/ref/protocols/opcua.md#instro.opcua.OpcUaError "instro.opcua.OpcUaError") – The server rejects the write or the session fails.

### close() → None[source]

Drain subscription samples, close publishers, and disconnect the client.
