This is an hacs integration using asyncua to connect various industrial PLC to HA. My initial approach was to develop a gateway which connect the PLC over RESTful API. After a couple of integration for projects, I found that it would be beneficial to the community to have a custom integration in HA for OPC UA servers.
I found an older project which uses the python-opcua, which is deprecated and decided to start a new integration using opcua-asyncio.
Installation
Asyncua-Coordinator
Sensor
Binary-Sensor
Switch
Directory
Donate
With HACS
- If HACS is not installed yet, download it following the instructions on https://hacs.xyz/docs/setup/download/
- Proceed to the HACS initial configuration following the instructions on https://hacs.xyz/docs/configuration/basic
- Copy this project directory, https://github.com/KVSoong/asyncua.
- Navigate to HACS in HA and select Integration.
- At the top right corner, select the vertical 3 dots and click Custom repositories.
- Paste the link in the
Repository
input field and select Integration for the Categeroy dropdown select. - Click Add to add this repository to your HA.
- Once added, you should see this custom repository installed. It should prompt you to restart.
- Once restarted, you should be able to connect to a OPCUA server.
This integration is developed with the option to connect to multiple OPCUA servers, as it is commonly used in an industrial environment.
Paste the following lines to your configuration.yaml
file. Remove or add more OPCUA servers as required. Specify a unique name for each server, under the key name
.
asyncua:
- name: "plc-01" # Unique name to identify the server. Will be used by the sensors to indicate which server to get the node value.
url: "opc.tcp://localhost:4840/" # URL of the server.
scan_interval: 10 # Optional. Interval for coordinator to read from OPCUA. Default is set at 30s.
username: admin # Optional username
password: admin # Optional password
- name: "plc-02"
url: "opc.tcp://localhost2:4840/"
scan_interval: 10
username: admin
password: admin
Options (YAML + descriptions)
Name | Type | Requirement | Description |
---|---|---|---|
name |
string | Required | Unique identifier for each OPCUA Server |
url |
string | Required | OPCUA server URL within the same network |
scan_interval |
int | Optional | Polling interval to refresh data |
username |
string | Optional | Username to connect to the server |
password |
string | Optional | Password to connect to the server |
This entity allows you to create any type of device class sensor specified in homeassistant. To add a custom sensor entity, paste the following example into your configuration.yaml
file and update the values.
sensor:
- platform: asyncua
nodes:
- name: sensor-01 # Name of the sensor
unique_id: sensor-01 # Unique ID of the sensor
device_class: temperature # Device class according to Homeassistant sensors
hub: plc-01 # OPCUA unique name defined in the coordinator that serve the data.
nodeid: "ns=1;s=3000" # Node id from OPCUA server
unit_of_measurement: Β°C # Optional unit of measurement.
- name: sensor-02
unique_id: sensor-02
device_class: temperature
hub: plc-02
nodeid: "ns=1;s=3000"
unit_of_measurement: Β°C
Options (YAML + descriptions)
Name | Type | Requirement | Description |
---|---|---|---|
name |
string | Required | Unique identifier for each sensor |
unique_id |
string | Optional | Entity ID stored in homeassistant |
device_class |
string | Optional | sensors available in homeassistant |
hub |
string | Required | Corresponded OPCUA hub defined in asyncua-coordinator name key |
nodeid |
string | Required | node id address of a single node |
unit_of_measurement |
string | Optional | to allow historical statistic graph if device_class is not defined |
This entity allows you to create any type of device class binary sensor specified in homeassistant. To add a custom binary sensor entity, paste the following example into your configuration.yaml
file and update the values.
binary_sensor:
- platform: asyncua
nodes:
- name: "binary-sensor-01" # Unique id for the binary sensor
unique_id: "binary-sensor-01" # Homeassistnat unique_id
device_class: "power" # Device class according to Homeassistant binary sensors
hub: "plc-01" # Defined Asyncua coordinator
nodeid: "ns=1;s=1000" # Node id from OPCUA server
- name: "binary-sensor-01"
unique_id: "binary-sensor-01"
device_class: "power"
hub: "plc-01"
nodeid: "ns=1;s=1000"
Options (YAML + descriptions)
Name | Type | Requirement | Description |
---|---|---|---|
name |
string | Required | Unique identifier for each binary sensor |
unique_id |
string | Optional | Entity ID stored in homeassistant |
device_class |
string | Optional | binary sensors available in homeassistant |
hub |
string | Required | Corresponded OPCUA hub defined in asyncua-coordinator name key |
nodeid |
string | Required | node id address of a single node |
This entity allow you to create a switch entity which can be a representative of a contactor or relay in a automation environment. Node id address for DO(digital output) to set the new value and also an optional DI(digital input) can be used to read the real state of the node. If DI is not specified, DO node address is used instead to check the state of the node. To add a custom binary sensor entity, paste the following example into your configuration.yaml
file and update the values.
switch:
- platform: asyncua
nodes:
- name: "switch-01" # Unique id for the binary sensor
unique_id: "switch-01" # Homeassistnat unique_id
hub: "plc-01 " # Defined Asyncua coordinator
nodeid: "ns=1;s=1000" # DO node id from OPCUA server
nodeid_switch_di: "ns=1;s=2000" # DI node id from OPCUA server
- name: "switch-02"
unique_id: "switch-02"
hub: "plc-01 "
nodeid: "ns=1;s=1001"
nodeid_switch_di: "ns=1;s=2001"
Options (YAML + descriptions)
Name | Type | Requirement | Description |
---|---|---|---|
name |
string | Required | Unique identifier for each switch |
unique_id |
string | Optional | Entity ID stored in homeassistant |
hub |
string | Required | Corresponded OPCUA hub defined in asyncua-coordinator name key |
nodeid |
string | Required | node id address for the digital output |
nodeid_switch_di |
string | Optional | node id address for the digital input. If not specified, nodeid_switch_do node is used for the latest state |
Will be included in the future.
To set a new value to a node, a service can be called from Developer Tools -> Services -> asyncua: set value.
-
Ensure that a valid asyncua-coordnator is created.
-
Paste the following code sample in
UI Mode
and make the necessary changeshub: plc-01 nodeid: ns=1;s=1000 value: false
-
In
YAML Mode
, paste the following code sample and make the necessary changesservice: asyncua.set_value target: {} data: hub: plc-01 nodeid: ns=1;s=1000 value: false
To better organize entities and avoid clumping them in a single configuration.yaml
file, you can include the following lines in the configuration.yaml
file, and create the directory stucture as shown in the tree below.
home-assistant/
βββ asyncua-binary-sensor/
| βββ sensor-01.yaml
| βββ ...
| βββ sensor-n.yaml
βββ asyncua-sensor/
| βββ sensor-01.yaml
| βββ ...
| βββ sensor-n.yaml
βββ asyncua-switch/
| βββ switch-01.yaml
| βββ ...
| βββ switch-n.yaml
βββ configuration.yaml
Configuration YAML
Include the following lines in the configuration.yaml
file to import the entites in each directory.
binary_sensor asyncua: !include_dir_merge_list asyncua-binary-sensor/
sensor asyncua: !include_dir_merge_list asyncua-sensor/
switch asyncua: !include_dir_merge_list asyncua-switch/
Binary Sensor YAML
- platform: asyncua
nodes:
- name: "binary-sensor-01" # Unique id for the binary sensor
unique_id: "binary-sensor-01" # Homeassistnat unique_id
device_class: "power" # Device class according to Homeassistant binary sensors
hub: "plc-01" # Defined Asyncua coordinator
nodeid: "ns=1;s=1000" # Node id from OPCUA server
- name: "binary-sensor-01"
unique_id: "binary-sensor-01"
device_class: "power"
hub: "plc-01"
nodeid: "ns=1;s=1000"
Sensor YAML
- platform: asyncua
nodes:
- name: sensor-01 # Name of the sensor
unique_id: sensor-01 # Unique ID of the sensor
device_class: temperature # Device class according to Homeassistant sensors
hub: plc-01 # OPCUA unique name defined in the coordinator that serve the data.
nodeid: "ns=1;s=3000" # Node id from OPCUA server
unit_of_measurement: Β°C # Optional unit of measurement.
- name: sensor-02
unique_id: sensor-02
device_class: temperature
hub: plc-02
nodeid: "ns=1;s=3000"
unit_of_measurement: Β°C
Switch YAML
- platform: asyncua
nodes:
- name: "switch-01" # Unique id for the binary sensor
unique_id: "switch-01" # Homeassistnat unique_id
hub: "plc-01 " # Defined Asyncua coordinator
nodeid: "ns=1;s=1000" # DO node id from OPCUA server
nodeid_switch_di: "ns=1;s=2000" # DI node id from OPCUA server
- name: "switch-02"
unique_id: "switch-02"
hub: "plc-01 "
nodeid: "ns=1;s=1001"
nodeid_switch_di: "ns=1;s=2001"