Hi,
I want to share an alternative to HA modbus integration. I always had problems to integrate some device using HA modbus (error connection, incompatibility etc), so I tried to use a famous free binary called ModPoll (Free Modbus Master Simulator and Test Tool) and use it on HA to read modbus register.
Simply follow these steps:
- Download
ModPoll
and extract the folderx86_64_linux
(or other version, according to your system) - Open
File Editor
on HA and uploadmodpoll
binary file of step 1 in the folder/config/packages/
(or where you want). If it doesn’t exist, create it. - In the folder
/config/packages/
create a blank filemodpoll.sh
and copy the followindg code:
#!/bin/bash
/config/packages/modpoll $1 | grep '^\[' || echo '[0]: 0'
- Save file
- Add a new command_line sensor into your configuration. This is an example:
- platform: command_line
name: "Conductivity"
command: "sh /config/packages/modpoll.sh '-t4 -r 14 -o 5 -i -c 1 -1 192.168.1.199'"
value_template: "{{ value.split()[1] | int }}"
scan_interval: 30
unique_id: conductivity
unit_of_measurement: 'µS'
That’s it!
Please, note that '-t4 -r 14 -o 5 -i -c 1 -1 192.168.1.199'
is the string will be passed to modpoll binary. Visit https://www.modbusdriver.com/modpoll.html for more information.
Anyway:
-r 14
is the register yout want to read: 14 in this case.
192.168.1.199
is your modbus device.
Naturally, you can change this code according to your configuration, as you want.
Note that if the device is not reachable, you will get the value 0. You can handle this event as you want editing modpoll.sh
file
This method can be powered adding write functionality.
Hope this will be useful.