Integration Modbus RTU modules with Home Assistant

1. Installation. The actions described below expect you to install Home Assistant first. Skip this step if you have installed Home Assistant already.

Brief example of installation (may vary): download Home Assistant image, download balenaEtcher, flash image to SD card, put the card into Raspberry Pi, connect Internet and then power cables, wait till the end of installation).

1

Go to address http://homeassistant.local:8123 from a browser in another computer in your local network or download a mobile app and connect from it, create an account.

2

Log in into your newly created account.

2. Adding Modbus support. Go to Supervisor - Add-on Store. Search for “File editor” add-on. Install and start it, open it (open Web UI). Open from it the “configuration.yaml” file. For adding Modbus support you need edit configuration.yaml file adding the next text:

modbus:

name: hub1

type: serial

method: rtu

port: /dev/ttyUSB0

baudrate: 9600

stopbits: 1

bytesize: 8

parity: N

timeout: 1

*Note, that in this example we use default connection parameters (baud rate 9600. 8-N-1). Be attentive typing as an extra “space” or another symbol can cause troubles.

3. Connection. Using observed modules with Modbus RTU expects you to have a serial port which is not available on Rasperry Pi so a virtual serial port with RS485 interface must be used. You may use an inexpensive USB-RS485 converter as shown below:

USB to RS485 Converter Adapter 1 Twins Chip 545x545 700x700

…or something more advanced. Pay attention: in the example above it’s expected there is only one virtual COM port in your system, if you use more than one converter at the same time, you may need to edit line “port: /dev/ttyUSB0”. For long lines we recommend to use converters with ground pin available preferably with galvanic isolation. Long wires also demand you to use terminal resistors (typically 120R) between A and B pins on the first and the last devices on the line. Some converters may include terminal resistor by default so to be installed on the one end of the line (wires). Follow other standard rules for RS485 wiring (daisy-chain topology, cable length etc.). Do not forget to power connected modules as well.

4. Read/write coil (relay/output). Go to Supervisor - Add-ons. Open “File editor” add-on (open Web UI). Open from it the “configuration.yaml” file. For adding Modbus support you need edit configuration.yaml file adding the next text (example):

switch:

platform: modbus

scan_interval: 1

coils:

   - name: Switch1

     hub: hub1

     slave: 1

     coil: 1

   - name: Switch2

     hub: hub1

     slave: 1

     coil: 2

Two switches are created in the example above (“Switch1” and “Switch2”). “Slave” parameter defines a Modbuss address of connected unit so in the example we connect only 1 module addressed 1, but work with two coils of it numbered 1 and 2. “Scan interval” defines how often the connected modules are polled by Home Assistant (1 second is the minimal interval).

In order to add created switches to overview go to Overview-Edit Dashboard-Add Card-By Entity. Choose and add the switches. If everything done right, you can switch the relays.

3

4

5. Reading Modbus register. Go to Supervisor - Add-ons. Open “File editor” add-on (open Web UI). Open from it the “configuration.yaml” file. For adding Modbus support you need edit configuration.yaml file adding the next text (example):

sensor:

platform: modbus

scan_interval: 1

registers:

   - name: Sensor1

     hub: hub1

     unit_of_measurement: events

     slave: 1

     register: 2

     count: 2

In the example above reading of a counter value placed in 2 Modbus registers (0x02 and 0x03) is being done. Module address (“slave” parameter) is 1, reading starting register is Modbus register 0x02 (“register” parameter). Created “sensor” reads 2 registers (“count” parameter). “Scan interval” defines how often the connected modules are polled by Home Assistant (1 second is the minimal interval).

To add created sensor to the “overview” go to Overview-Edit Dashboard-Add Card-By Entity. Choose and add the sensor. If everything done right, you can see counter values (updating each second).

 5

6. Writing Modbus register. In this example, DALI lighting dimmer will be connected to Home Assistant via Modbus-DALI gateway. Writing to DALI line is being done by writing a particular Modbus register of the gateway. First, add Modbus support to “configuration.yaml” file (look above). Create a slider in Home Assistant UI so there would be a way to change DALI values. For that go to Supervisor - Add-ons. Open “File editor” add-on (open Web UI). Open from it the “configuration.yaml” file. For adding Modbus support you need edit “configuration.yaml” file adding the next text:

input_number:

slider1:

   name: DALI

   initial: 30

   min: 0

   max: 255

   step: 1

We suppose here that DALI brightness values will be changed by slider in range from 0 to 255 (255 is maximal value in DALI) with a step 1. Now a Home Assistant automation must be created in order to process some actions when slider value is changed by user interface. For that purpose edit a “configuration.yaml” file again adding next text:

automation:

- alias: SET DALI

   trigger:

     platform: state

     entity_id: input_number.slider1

   action:

     - service: modbus.write_register

       data:

         hub: hub1

         unit: 1

         address: 1

         value: '{{ (states.input_number.slider1.state | int)+65024 }}'

“Unit” parameter indicates a Modbus address of Modbus-DALI gateway (default is 1), “address” parameter defines a Modbus register address where data will be written when changing slider values (0x01 is a register address in the gateway where DALI data is expected to be sent). Number 65024 added here to create a DALI mask for dimming all lamps (collective call/broadcast): “11111110 XXXXXXXX”, where XXXXXXXX is a slider value.

6

7. For more info follow the link:

https://www.home-assistant.io/integrations/modbus/