Calculate real sensor value from Modbus

Hi everyone,

I have installed a water level sensor which provides a 4-20 mA signal.
I am already reading the holding register from a Modbus TCP, succesfully.

#configuration.yaml entry
modbus:
  - name: ZisternenPi
    type: tcp
    host: 192.168.178.63
    port: 502
    sensors:
      - name: Zisterne1
        slave: 1
        address: 1024
        input_type: holding
        data_type: int
        scan_interval: 5
        precision: 2

The value stored in the Modbus holding register is not the actual value (unit) of the measurement.
To get the “mA”-value, the integer value from Modbus holding register hast to be trasformed by calculation (value*20/4095)
I have tried different solutions from various forums, but didnt manage to implement the calculation.
I assume that it is just one line of appropriate code, but i am obvisouly to nooby.
Maybe someone can help me with this?

Dont hesitate to ask if something’s unclear.

kind regards,
Jonas

Hi Jonas,
did you solve your problem?
I have the same issue at the moment and before starting a new thread I wanted to ask if you did your correction factor calculated correctly in the mean time.

I also only need to apply a static factor (raw_value / 5 * 3)
This are my first steps in HA…

Thanks and best,
Matthias

If somebody is interested how to read and calculate values, here is my code. This might not be very elegant nor done in a beautiful way. I am working with HA since 4 hours, so I would call myself a newbie.
I wanted to read the Values out of a Janitza UMG96:


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

modbus:
  - name: modbus
    type: serial
    baudrate: 38400
    bytesize: 8
    method: rtu
    parity: N
    port: /dev/ttyUSB0
    stopbits: 1
    sensors:
# Voltages
    - name: umg96_sr01_voltage_l1
      slave: 1
      address: 200
      count: 1
      input_type: holding
      precision: 2
      scan_interval: 5
      data_type: int16
      device_class: voltage
      state_class: measurement
    - name: umg96_sr01_voltage_l2
      slave: 1
      address: 201
      count: 1
      input_type: holding
      precision: 0
      scan_interval: 5
      data_type: int16
      device_class: voltage
      state_class: measurement
    - name: umg96_sr01_voltage_l3
      slave: 1
      address: 202
      count: 1
      input_type: holding
      precision: 1
      scan_interval: 5
      data_type: int16
      device_class: voltage
      state_class: measurement
# Actual Power
    - name: umg96_sr01_power_l1
      slave: 1
      address: 209
      count: 1
      input_type: holding
      precision: 1
      scan_interval: 5
      data_type: int16
      device_class: power
      state_class: measurement
    - name: umg96_sr01_power_l2
      slave: 1
      address: 210
      count: 1
      input_type: holding
      precision: 1
      scan_interval: 5
      data_type: int16
      device_class: power
      state_class: measurement
    - name: umg96_sr01_power_l3
      slave: 1
      address: 211
      count: 1
      input_type: holding
      precision: 1
      scan_interval: 5
      data_type: int16
      device_class: power
      state_class: measurement
    - name: umg96_sr01_power_total
      slave: 1
      address: 279
      count: 1
      input_type: holding
      precision: 1
      scan_interval: 5
      data_type: int16
      device_class: power
      state_class: measurement
    - name: umg96_sr01_energy_total
      slave: 1
      address: 422
      count: 2
      input_type: holding
      precision: 0
      scan_interval: 5
      data_type: int32
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "Wh"
template: 
    - sensor:
        - name: "Serverraum Reihe 1-2 Leistung L1"
          unit_of_measurement: "kW"
          device_class: power
          state: >
            {% set p_l1 = states('sensor.umg96_sr01_power_l1') | float %}
            {{ (p_l1 / 500 * 3) | round(1, default=0)}}
        - name: "Serverraum Reihe 1-2 Leistung L2"
          unit_of_measurement: "kW"
          device_class: power
          state: >
            {% set p_l2 = states('sensor.umg96_sr01_power_l2') | float %}
            {{ (p_l2 / 500 * 3) | round(1, default=0)}}
        - name: "Serverraum Reihe 1-2 Leistung L3"
          unit_of_measurement: "kW"
          device_class: power
          state: >
            {% set p_l3 = states('sensor.umg96_sr01_power_l3') | float %}
            {{ (p_l3 / 500 * 3) | round(1, default=0)}}
        - name: "Serverraum Reihe 1-2 Leistung Summe"
          unit_of_measurement: "kW"
          device_class: power
          state: >
            {% set p_total = states('sensor.umg96_sr01_power_total') | float %}
            {{ (p_total / 50 * 3) | round(1, default=0)}}
        - name: "Serverraum Reihe 1-2 Gesamtbezug"
          unit_of_measurement: "kWh"
          device_class: energy
          state_class: total_increasing
          state: >
            {% set e_total = states('sensor.umg96_sr01_energy_total') | float %}
            {{ (e_total / 50 * 3) | round(1, default=0)}}
1 Like

Hallo Matthias,
I am also new at HA - I wanted to read values out of Janitza UMG 508.
The conversion between RS485 and ETH is a Waveshare RS485 to ETH converter.

Now I get hundrets of packets each second - they seem to be empty.

Could you help me to connect to the UMG 508 (it´s the same interface like UMG 96.
Setting at Janitza are RS485: Modbus Master/Gateway, adress is 200, Baudrate 9600.

Could you help please with more informations or manuals how to read the data.
Thanks in advance,
nobi