Modpoll - An alternative to read modbus device registers on HASSIO

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:

  1. Download ModPoll and extract the folder x86_64_linux (or other version, according to your system)
  2. Open File Editor on HA and upload modpoll binary file of step 1 in the folder /config/packages/ (or where you want). If it doesn’t exist, create it.
  3. In the folder /config/packages/ create a blank file modpoll.sh and copy the followindg code:
#!/bin/bash
/config/packages/modpoll $1 | grep '^\[' || echo '[0]: 0'
  1. Save file
  2. 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.

4 Likes
#!/bin/sh

# rtu over tcp
HOST="-m enc -p 9502 10.1.0.37"

# serial
#HOST="-m rtu /tty...."

function four()
{
/config/apps/modpoll -a 1 -0 -1 -r $1 -c $2 -t 3:int -e $HOST \
| grep '^\[' | awk -F ": " '{ print $2 "," }'

}

result=${result}$(four 38 3)
result=${result}$(four 22 2)

echo $result | sed -e 's/ //g'

##
#
#/config/pkg/modpool.yaml

sensor:

  - platform: command_line
    name: "modpoll data"
    command: "/config/apps/modpoll.sh"
    scan_interval: 20
    command_timeout: 10
    
template:

  - sensor:

    - name: "modpool test"
      state: >-
        {% set x = states('sensor.modpoll_data').split(',')[0] | float(0) %}
        {% if x > 0 %}
          {{ (x/1000)|round(1) }}
        {% endif %}

#eof

Hi Paolo,

This sounds great.

I am struggling to connect my epever to HA via a RJ45-USB cable (rs485 to USB converter) (https://www.epever.com/wp-content/uploads/2021/05/LS-B-SMS-EL-V2.5.pdf).

I am trying to get it to work with these settings, but with no success:

Do you think Modpoll will work?

Try on a PC first. If it works on a PC, it will work on HA

1 Like

Hi Messier, have you managed to make it work? I’m strugglin with it :disappointed_relieved:

1 Like

Yes I actually did get it to work! What is not working for you?