Using Modbus RTU Relay with Homeassistant
From Waveshare Wiki
Preparation
Hardware Preparation
- Raspberry Pi: https://www.waveshare.com/product/raspberry-pi/boards-kits/raspberry-pi-4/pi4b-starter-kit.htm
- Modbus RTU Relay 16CH: https://www.waveshare.com/modbus-rtu-relay-16ch.htm
- RS485 CAN HAT: https://www.waveshare.com/rs485-can-hat.htm
- Raspberry Pi 4 USB-C Power Supply: https://www.waveshare.com/rs485-can-hat.htm
- Power Supply 5V: https://www.waveshare.com/psu-5v-4a-5.5-2.1-us.htm
- 【Optional】USB TO RS485: https://www.waveshare.com/usb-to-rs485.htm
1. Install Raspberry Pi OS System
- Download Raspberry Pi OS: https://www.raspberrypi.com/software/operating-systems/#raspberry-pi-os-64-bit
- After installation, the Raspberry Pi OS system environment is as follows:
2. Install Home Assistant Core
- Install Home Assistant Core, See the tutorial: Raspberry Pi - Home Assistant (home-assistant.io) (This tutorial installs version 2024.3.0)
- After installation, open the browser and run Home Assistant:
3. Environment Configuration
- Hardware connection diagram:
- On the Raspberry Pi, configure RS485 CAN HAT, refer to: https://www.waveshare.com/wiki/RS485_CAN_HAT#RS485_Usage
Execute the following command to enter the Raspberry Pi configuration:
sudo raspi-config
Choose Interfacing Options -> Serial -> No -> Yes:
You need to disable the login shell and enable the serial port hardware:
- Configure Homeassistant's configuration.yaml on Raspberry Pi, file path: /home/homeassistant/.homeassistant/configuration.yaml
sudo nano /home/homeassistant/.homeassistant/configuration.yaml
Add the following content at the end of the file:
modbus: - name: modbus_hub type: serial port: /dev/ttyS0 baudrate: 9600 bytesize: 8 method: rtu parity: N stopbits: 1 binary_sensors: - name: my_relay address: 0 device_class: plug input_type: coil scan_interval: 15 slave: 1 slave_count: 15 unique_id: my_relay lights: - name: "Light_1" address: 0 write_type: coil slave: 1 - name: "Light_2" address: 1 write_type: coil slave: 1 - name: "Light_3" address: 2 write_type: coil slave: 1 - name: "Light_4" address: 3 write_type: coil slave: 1 - name: "Light_5" address: 4 write_type: coil slave: 1 - name: "Light_6" address: 5 write_type: coil slave: 1 - name: "Light_7" address: 6 write_type: coil slave: 1 - name: "Light_8" address: 7 write_type: coil slave: 1 - name: "Light_9" address: 8 write_type: coil slave: 1 - name: "Light_10" address: 9 write_type: coil slave: 1 - name: "Light_11" address: 10 write_type: coil slave: 1 - name: "Light_12" address: 11 write_type: coil slave: 1 - name: "Light_13" address: 12 write_type: coil slave: 1 - name: "Light_14" address: 13 write_type: coil slave: 1 - name: "Light_15" address: 14 write_type: coil slave: 1 - name: "Light_16" address: 15 write_type: coil slave: 1
Then save and exit.
- Configuration.yaml download link:https://files.waveshare.com/wiki/Modbus%20RTU%20Relay/configuration.yaml
- After the configuration is complete, restart the Raspberry Pi and re-run Homeassistant:
- Please see the video for actual measurement results:
- See the video of the test: https://files.waveshare.com/wiki/Modbus%20RTU%20Relay/Modbus-Relay-Homeassistant-Test-Video.mp4
- Turn on all Relay.
- Turn on Relay 1~4, 9~12: