How do I get correct values from Carlo Gavazzi EM340 Modbus

I have 2 energy meters, 1st Carlo Gavazzi EM340 and 1st Carlo Gavazzi EM111 connected to HA via Modbus.
The values I receive in HA do not match the values on the meters at all. What am I doing wrong and how do I fix it?
Here is my .yaml for the energy meters:

  - name: modbus-rtu
    type: serial
    method: rtu
    port: /dev/serial/by-id/usb-1a86_USB_Serial-if00-port0
    baudrate: 19200
    stopbits: 1
    bytesize: 8
    parity: N
    sensors:

# Energymeter 1 (Lägenhet)
# SYS
      - name: energymeter-1_v-l-n-sys
        unit_of_measurement: V (L-N)
        device_class: voltage
        slave: 1
        address: 259
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_v-l-l-sys
        unit_of_measurement: V (L-L)
        device_class: voltage
        slave: 1
        address: 261
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kw-sys
        unit_of_measurement: kW
        device_class: energy
        slave: 1
        address: 263
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kva-sys
        unit_of_measurement: kVA
        device_class: energy
        slave: 1
        address: 265
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kVAR-sys
        unit_of_measurement: kVAR
        device_class: energy
        slave: 1
        address: 267
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_pf-sys
        unit_of_measurement: Pf
        device_class: energy
        slave: 1
        address: 269
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_hz
        unit_of_measurement: Hz
        device_class: voltage
        slave: 1
        address: 273
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kwh
        unit_of_measurement: kWh
        device_class: energy
        slave: 1
        address: 275
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

# L1
      - name: energymeter-1_v-l1-l2
        unit_of_measurement: V (L1-L2)
        device_class: voltage
        slave: 1
        address: 287
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_v-l1-n
        unit_of_measurement: V (L1-N)
        device_class: voltage
        slave: 1
        address: 289
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_a-l1
        unit_of_measurement: A
        device_class: current
        slave: 1
        address: 291
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kw-l1
        unit_of_measurement: kW
        device_class: energy
        slave: 1
        address: 293
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kva-l1
        unit_of_measurement: kVA
        device_class: energy
        slave: 1
        address: 295
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kVAR-l1
        unit_of_measurement: kVAR
        device_class: energy
        slave: 1
        address: 297
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_pf-l1
        unit_of_measurement: Pf
        device_class: energy
        slave: 1
        address: 299
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

#L2
      - name: energymeter-1_v-l2-l3
        unit_of_measurement: V (L2-L3)
        device_class: voltage
        slave: 1
        address: 301
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_v-l2-n
        unit_of_measurement: V (L2-N)
        device_class: voltage
        slave: 1
        address: 303
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_a-l2
        unit_of_measurement: A
        device_class: current
        slave: 1
        address: 305
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kw-l2
        unit_of_measurement: kW
        device_class: energy
        slave: 1
        address: 307
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kva-l2
        unit_of_measurement: kVA
        device_class: energy
        slave: 1
        address: 309
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kVAR-l2
        unit_of_measurement: kVAR
        device_class: energy
        slave: 1
        address: 311
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_pf-l2
        unit_of_measurement: Pf
        device_class: energy
        slave: 1
        address: 313
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

# L3
      - name: energymeter-1_v-l3-l1
        unit_of_measurement: V (L3-L1)
        device_class: voltage
        slave: 1
        address: 315
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_v-l3-n
        unit_of_measurement: V (L3-N)
        device_class: voltage
        slave: 1
        address: 317
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_a-l3
        unit_of_measurement: A
        device_class: current
        slave: 1
        address: 319
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kw-l3
        unit_of_measurement: kW
        device_class: energy
        slave: 1
        address: 321
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kva-l3
        unit_of_measurement: kVA
        device_class: energy
        slave: 1
        address: 323
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_kVAR-l3
        unit_of_measurement: kVAR
        device_class: energy
        slave: 1
        address: 325
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-1_pf-l3
        unit_of_measurement: Pf
        device_class: energy
        slave: 1
        address: 327
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32


# Energymeter 2 (Förråd)
      - name: energymeter-2_a
        unit_of_measurement: A
        device_class: current
        slave: 2
        address: 257
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-2_v-l-n
        unit_of_measurement: V (L-N)
        device_class: voltage
        slave: 2
        address: 259
        input_type: holding
        precision: 1
        count: 2
        scale: 0.1
        data_type: int32
      - name: energymeter-2_kva
        unit_of_measurement: kVA
        device_class: energy
        slave: 2
        address: 265
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-2_kVAR
        unit_of_measurement: kVAR
        device_class: energy
        slave: 2
        address: 267
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-2_pf
        unit_of_measurement: Pf
        device_class: energy
        slave: 2
        address: 269
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-2_hz
        unit_of_measurement: Hz
        device_class: voltage
        slave: 2
        address: 273
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-2_kwh
        unit_of_measurement: kWh
        device_class: energy
        slave: 2
        address: 275
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

      - name: energymeter-2_kw
        unit_of_measurement: kW
        device_class: energy
        slave: 2
        address: 293
        input_type: holding
        precision: 2
        count: 2
        scale: 0.1
        data_type: int32

The RS485 converter I use is this one.

Documentation on the Modbus protocol for the EM340
Documentation on the Modbus protocol for the EM111

1 Like

I start with a warning : I haven’t even connected my EM340 yet, I am in the research phase. I have a few questions : did you check the INFO page on the meter for the RS485 settings (baud, parity, stop bit) ? Also in the INFO page, did you verify the revision number ? Online I hv located r6, r9, r11 and r13 documentation, but not my own r12. There seems to be a difference in the registers between these, so if you don’t have the matching reference it will be a big puzzle. Do please let me know if you have made any progress it would be of great help.

I’ve checked and doublechecked the documentation. The settings are right. I have tried both RTU with a USB to RS485 converter and TCP with an Ethernet to RS485 converter.
When I connect to the meters using Carlo Gavazzi UCS 7, the configuration software for the meters, it shows the correct values using the same hardware…

I managed to connect with Python not yet with my Hassio. For Python to EM340 I had to flip my A & B ports, so that is something you can try. I also use 9600 baudrate. Code below. Now my code is running on a separate raspberry pi so I will to maybe use ser2net or some other way of getting my hassio pi to read the serial port of the RS485 pi.

# https://gitlab.com/wolutator/modbusmaster/blob/master/snippets/test7.py
from pymodbus.client.sync import ModbusSerialClient
from pymodbus.pdu import ExceptionResponse
from pymodbus.exceptions import ModbusIOException
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian
import struct
import time

class ModbusException(Exception):
  def __init__(self, resp):
    self.msg = str(result)

  def __str__(self):
    return self.msg

class ModbusRequestDefinition(object):
  def __init__(self, kind, unit, address, count, converter, label, decimals):
    self.kind = kind
    self.unit = unit
    self.address = address
    self.count = count
    self.converter = converter
    self.label = label
    self.decimals = decimals

reqs = [
 #ModbusRequestDefinition('H', 1, 0x0002, 2, '', 'V1'),
 #ModbusRequestDefinition('H', 1, 0x0002, 2, '', 'V2'),
 #ModbusRequestDefinition('H', 1, 0x0004, 2, '', 'V3'),
 ModbusRequestDefinition('H', 1, 0x000C, 2, '', 'A1', 1000),
 ModbusRequestDefinition('H', 1, 0x000E, 2, '', 'A2', 1000),
 ModbusRequestDefinition('H', 1, 0x0010, 2, '', 'A3', 1000),
 ModbusRequestDefinition('H', 1, 0x0034, 2, '', 'kWh+', 10),
 ModbusRequestDefinition('H', 1, 0x004E, 2, '', 'kWh-', 10), 
]

client = ModbusSerialClient(method='rtu', port='/dev/ttyAMA0', baudrate=9600, stopbits=1, timeout=1)
client.connect()

delay = 0.05
#period = 0.5
period = 5


while True:
  for req in reqs:
    try:
      time.sleep(delay)
      if req.kind == 'H':
        result = client.read_holding_registers(address=req.address, 
                                               count=req.count, 
                                               unit=req.unit)
        if type(result) in [ExceptionResponse, ModbusIOException]:
          raise ModbusException(result)
        print(req.label,  end ="\t" )
        decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big, wordorder=Endian.Little)
        print(decoder.decode_32bit_int()/req.decimals)

    except ModbusException as e:
      print("ERROR when querying '{0}': {1!s}".format(req.label, e))
      if client.socket is None:
        print("renew socket")
        #client.socket = getSerial()

  print("-------------")
  time.sleep(period)

client.close()

Hi Claes.
Im just wondering if you figured it out? I can’t even get the data from the meter… :thinking:

Jonas

Hi,
No, I still have the same problem. I haven’t put that much time into the problem either…

Just got mine to do what I want.

I thought posting my config here might help someone who is struggling (and serve as a backup for me :slight_smile:

I run HAOS8 on an old laptop and I have USB-RS485 adapter connected to meters.

I had some confusion with TX and RX wires but … ultimately sorted them out with trial and error.

PV meter is ET340, Grid meter is ET330

modbus:
  - name: hub1
    type: serial
    baudrate: 9600
    bytesize: 8
    method: rtu
    parity: N
    port: /dev/serial/by-id/usb-FTDI_USB-RS485_Cable_FT57RI8X-if00-port0
    stopbits: 1
    timeout : 10
    message_wait_milliseconds: 1000

### Register documentation 
### https://gavazzi.se/app/uploads/2020/11/em330_em340_et330_et340_cp.pdf
### config: 2022 Jussi Savola 

    sensors:
    
##### E2 SOLAR    
      - name: E2 L1 W
        unit_of_measurement: W
        slave: 2
        address: 18
        data_type: int32
        input_type: input
        count: 2
        scan_interval: 15
        lazy_error_count : 999
        scale: 0.1
        swap: word
        
      - name: E2 L2 W
        unit_of_measurement: W
        slave: 2
        address: 20
        data_type: int32
        input_type: input
        count: 2
        scan_interval: 15
        lazy_error_count : 999
        scale: 0.1
        swap: word

      - name: E2 L3 W
        unit_of_measurement: W
        slave: 2
        address: 22
        input_type: input
        data_type: int32
        count: 2
        scan_interval: 15
        lazy_error_count : 999
        scale: 0.1
        swap: word

      - name: E2 kWh plus TOT
        unit_of_measurement: kWh
        slave: 2
        address: 52
        input_type: input
        data_type: int32
        count: 2
        scan_interval: 60
        lazy_error_count : 999
        scale: 0.1
        swap: word
        state_class : total
        device_class : energy

        
      - name: E2 kWh minus TOT
        unit_of_measurement: kWh
        slave: 2
        address: 78
        input_type: input
        data_type: int32
        count: 2
        scan_interval: 60
        lazy_error_count : 999
        scale: 0.1
        swap: word
        state_class : total
        device_class : energy

      - name: E2 hertsit2
        unit_of_measurement: Hz
        slave: 2
        address: 51
        # input_type: input
        data_type: int16
        count: 1
        scan_interval: 120
        lazy_error_count : 999
        scale: 0.1

      - name: E2 voltage L1
        unit_of_measurement: V
        slave: 2
        address: 0
        swap: word
        data_type: int32
        count: 2
        scan_interval: 120
        lazy_error_count : 999
        scale: 0.1


##### E1 GRID
        
      - name: E1 L1 W
        unit_of_measurement: W
        slave: 1
        address: 18
        data_type: int32
        input_type: input
        count: 2
        scan_interval: 15
        lazy_error_count : 999
        scale: 0.1
        swap: word
        
      - name: E1 L2 W
        unit_of_measurement: W
        slave: 1
        address: 20
        data_type: int32
        input_type: input
        count: 2
        scan_interval: 15
        lazy_error_count : 999
        scale: 0.1
        swap: word

      - name: E1 L3 W
        unit_of_measurement: W
        slave: 1
        address: 22
        input_type: input
        data_type: int32
        count: 2
        scan_interval: 15
        lazy_error_count : 999
        scale: 0.1
        swap: word
        

        
      - name: E1 kWh plus TOT
        unit_of_measurement: kWh
        slave: 1
        address: 52
        input_type: input
        data_type: int32
        count: 2
        scan_interval: 60
        lazy_error_count : 999
        scale: 0.1
        swap: word
        state_class : total
        device_class : energy
        
      - name: E1 kWh minus TOT
        unit_of_measurement: kWh
        slave: 1
        address: 78
        input_type: input
        data_type: int32
        count: 2
        scan_interval: 60
        lazy_error_count : 999
        scale: 0.1
        swap: word
        state_class : total
        device_class : energy
        

      - name: E1 hertsit1
        unit_of_measurement: Hz
        slave: 1
        address: 51
        # input_type: input
        data_type: int16
        count: 1
        scan_interval: 120
        lazy_error_count : 999
        scale: 0.1


      - name: E1 voltage L1
        unit_of_measurement: V
        slave: 1
        address: 0
        swap: word
        data_type: int32
        count: 2
        scan_interval: 120
        lazy_error_count : 999
        scale: 0.1
3 Likes

Many thanks to you!
It worked with my RS485 to TCP converter.
I had almost given up on the meters.

1 Like

I am happy to hear this!

First I used UCS software to figure out for sure the modbus IDs (and comms params).

Then I picked “an easy” parameter, int16 and clear “50Hz” which is easy to verify.

After that it was the more complicated ones, int32 and bytes swaps and such.

It took me quite a while to figure out everything I needed but now that I got things settled out there was no reason to hide the information.

Thank you very much for your support Hogbert !
Here is my complete file for reading all the values.

modbus:

  • name: modbuspi
    delay : 5
    timeout : 5
    message_wait_milliseconds : 1000
    type: serial
    method: rtu
    baudrate: 9600
    bytesize: 8
    stopbits: 1
    parity: N
    port: /dev/ttyUSB1

    sensors :

    EM340

    SYS

    • name: EM340_test_neg
      unit_of_measurement: kWh
      state_class: total_increasing
      device_class: energy
      slave: 1
      address: 270
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_v_l_n_sys
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 36
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_v_l_l_sys
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 38
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_W_sys
      unit_of_measurement: W
      state_class: measurement
      device_class: energy
      slave: 1
      address: 40
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_VA_sys
      unit_of_measurement: VA
      state_class: measurement
      device_class: energy
      slave: 1
      address: 42
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_var_sys
      unit_of_measurement: var
      state_class: measurement
      device_class: energy
      slave: 1
      address: 44
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_pf_sys
      unit_of_measurement: PF
      state_class: measurement
      device_class: energy
      slave: 1
      address: 49
      data_type: int16
      input_type: input
      precision: 3
      count: 1
      scale: 0.001
      scan_interval: 15
      lazy_error_count : 999
      #swap : word: 999

    • name: EM340_hz_sys
      unit_of_measurement: Hz
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 51
      data_type: int16
      input_type: input
      precision: 2
      count: 1
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      #swap : word: 999

    TOTAUX

    • name: EM340_kwh_pos_tot
      unit_of_measurement: kWh
      state_class: total_increasing
      device_class: energy
      slave: 1
      address: 52
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_kvarh_pos_tot
      unit_of_measurement: kvarh
      state_class: measurement
      device_class: energy
      slave: 1
      address: 54
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_kwh_neg_tot
      unit_of_measurement: kWh
      state_class: total_increasing
      device_class: energy
      slave: 1
      address: 78
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_kvarh_neg_tot
      unit_of_measurement: kvarh
      state_class: measurement
      device_class: energy
      slave: 1
      address: 80
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    L1

    • name: EM340_v_l1_l2
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 6
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_v_l1_n
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 0
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_a_l1
      unit_of_measurement: A
      state_class: measurement
      device_class: current
      slave: 1
      address: 12
      data_type: int32
      input_type: input
      precision: 3
      count: 2
      scale: 0.001
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_w_l1
      unit_of_measurement: W
      state_class: measurement
      device_class: energy
      slave: 1
      address: 18
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_va_l1
      unit_of_measurement: VA
      state_class: measurement
      device_class: energy
      slave: 1
      address: 24
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_var_l1
      unit_of_measurement: var
      state_class: measurement
      device_class: energy
      slave: 1
      address: 30
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_kwh_l1
      unit_of_measurement: kwh
      state_class: total_increasing
      device_class: energy
      slave: 1
      address: 64
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_pf_l1
      unit_of_measurement: PF
      state_class: measurement
      device_class: energy
      slave: 1
      address: 46
      data_type: int16
      input_type: input
      precision: 3
      count: 1
      scale: 0.001
      scan_interval: 15
      lazy_error_count : 999
      #swap : word

    #L2

    • name: EM340_v_l2_l3
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 8
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_v_l2_n
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 2
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_a_l2
      unit_of_measurement: A
      state_class: measurement
      device_class: current
      slave: 1
      address: 14
      data_type: int32
      input_type: input
      precision: 3
      count: 2
      scale: 0.001
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_w_l2
      unit_of_measurement: W
      state_class: measurement
      device_class: energy
      slave: 1
      address: 20
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_va_l2
      unit_of_measurement: VA
      state_class: measurement
      device_class: energy
      slave: 1
      address: 26
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_var_l2
      unit_of_measurement: var
      state_class: measurement
      device_class: energy
      slave: 1
      address: 32
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_kwh_l2
      unit_of_measurement: kwh
      state_class: total_increasing
      device_class: energy
      slave: 1
      address: 66
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_pf_l2
      unit_of_measurement: PF
      state_class: measurement
      device_class: energy
      slave: 1
      address: 47
      data_type: int16
      input_type: input
      precision: 3
      count: 1
      scale: 0.001
      scan_interval: 15
      lazy_error_count : 999
      #swap : word

    L3

    • name: EM340_v_l3_l1
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 10
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_v_l3_n
      unit_of_measurement: V
      state_class: measurement
      device_class: voltage
      slave: 1
      address: 4
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_a_l3
      unit_of_measurement: A
      state_class: measurement
      device_class: current
      slave: 1
      address: 16
      data_type: int32
      input_type: input
      precision: 3
      count: 2
      scale: 0.001
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_w_l3
      unit_of_measurement: W
      state_class: measurement
      device_class: energy
      slave: 1
      address: 22
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_va_l3
      unit_of_measurement: VA
      state_class: measurement
      device_class: energy
      slave: 1
      address: 28
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_var_l3
      unit_of_measurement: var
      state_class: measurement
      device_class: energy
      slave: 1
      address: 34
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_kwh_l3
      unit_of_measurement: kwh
      state_class: total_increasing
      device_class: energy
      slave: 1
      address: 70
      data_type: int32
      input_type: input
      precision: 2
      count: 2
      scale: 0.1
      scan_interval: 15
      lazy_error_count : 999
      swap : word

    • name: EM340_pf_l3
      unit_of_measurement: PF
      state_class: measurement
      device_class: energy
      slave: 1
      address: 48
      data_type: int16
      input_type: input
      precision: 3
      count: 1
      scale: 0.001
      scan_interval: 15
      lazy_error_count : 999
      #swap : word

I can read all the positive values but I am not able to read the power I give back to the network.
Kwh(-)
Does someone already did that ?
My product code CARLO GAVAZZI EM340: EM340DINAV23XS1SFA

1 Like

Did you configure the meter to do two-way metering?
You can do it with freely available UCS software (MS-Windows only I guess)

1 Like

Hi,
I will add a EM 340 to Home assistent. Can a only add yaml to get it work, or do a also need some addon?
I have a USB to R485 RTU adapter.

Only yaml, no addon needed.

Btw, just posting my config here for whoever is interested.

My modbus configuration (needs to be adapted to your setup):

modbus:
  - name: Protoss PE11-H SmartMeter EM540
    type: tcp
    host: 192.168.23.133
    port: 8899
    sensors:

Direct sensors:

      # Reference: https://gavazzi.se/app/uploads/2022/03/em500-cp-v1r3-eng.pdf

      - name: SmartMeter EM540 Carlo Gavazzi Controls identification code raw
        unique_id: unique_id__em540_cgc_id_code_raw
        slave: 1
        address: 0x000B
        data_type: uint16
        scan_interval: 3600

      - name: SmartMeter EM540 Firmware raw
        unique_id: unique_id__em540_firmware_raw
        slave: 1
        address: 0x0302
        data_type: uint16
        scan_interval: 3600

      - name: SmartMeter EM540 Measurement mode raw
        unique_id: unique_id__em540_measure_mode_raw
        slave: 1
        address: 0x1103
        data_type: uint16
        scan_interval: 3600

      - name: SmartMeter EM540 Device state raw
        unique_id: unique_id__em540_dev_state_raw
        slave: 1
        address: 0x5012
        data_type: uint16
        scan_interval: 60

      ##########################################################################
      # Voltage

      - name: SmartMeter EM540 V L1-N
        unique_id: unique_id__em540_v_l1_n
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x0000
        data_type: int32 # "Data Format" -> int32 = 2 words
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 V L2-N
        unique_id: unique_id__em540_v_l2_n
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x0002
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 V L3-N
        unique_id: unique_id__em540_v_l3_n
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x0004
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      #

      - name: SmartMeter EM540 V L1-L2
        unique_id: unique_id__em540_v_l1_l2
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x0006
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 V L2-L3
        unique_id: unique_id__em540_v_l2_l3
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x0008
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 V L3-L1
        unique_id: unique_id__em540_v_l3_l1
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x000A
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      #

      - name: SmartMeter EM540 V L-N sys
        unique_id: unique_id__em540_v_l_n_sys
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x0024
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 V L-L sys
        unique_id: unique_id__em540_v_l_l_sys
        unit_of_measurement: V
        state_class: measurement
        device_class: voltage
        slave: 1
        address: 0x0026
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      ##########################################################################
      # Current

      - name: SmartMeter EM540 A L1
        unique_id: unique_id__em540_a_l1
        unit_of_measurement: A
        state_class: measurement
        device_class: current
        slave: 1
        address: 0x000C
        data_type: int32
        swap: word
        scale: 0.001
        precision: 3
        scan_interval: 15

      - name: SmartMeter EM540 A L2
        unique_id: unique_id__em540_a_l2
        unit_of_measurement: A
        state_class: measurement
        device_class: current
        slave: 1
        address: 0x000E
        data_type: int32
        swap: word
        scale: 0.001
        precision: 3
        scan_interval: 15

      - name: SmartMeter EM540 A L3
        unique_id: unique_id__em540_a_l3
        unit_of_measurement: A
        state_class: measurement
        device_class: current
        slave: 1
        address: 0x0010
        data_type: int32
        swap: word
        scale: 0.001
        precision: 3
        scan_interval: 15

      ##########################################################################
      # Power

      - name: SmartMeter EM540 W L1
        unique_id: unique_id__em540_w_l1
        unit_of_measurement: W
        state_class: measurement
        device_class: power
        slave: 1
        address: 0x0012
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 5

      - name: SmartMeter EM540 W L2
        unique_id: unique_id__em540_w_l2
        unit_of_measurement: W
        state_class: measurement
        device_class: power
        slave: 1
        address: 0x0014
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 5

      - name: SmartMeter EM540 W L3
        unique_id: unique_id__em540_w_l3
        unit_of_measurement: W
        state_class: measurement
        device_class: power
        slave: 1
        address: 0x0016
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 5

      #

      - name: SmartMeter EM540 W sys
        unique_id: unique_id__em540_w_sys
        unit_of_measurement: W
        state_class: measurement
        device_class: power
        slave: 1
        address: 0x0028
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 0 # Updated via synchronous automation

      ##########################################################################
      # Powerfactor

      - name: SmartMeter EM540 PF L1
        unique_id: unique_id__em540_pf_l1
        unit_of_measurement: ""
        state_class: measurement
        slave: 1
        address: 0x002E
        data_type: int16
        scale: 0.001
        precision: 3
        scan_interval: 15

      - name: SmartMeter EM540 PF L2
        unique_id: unique_id__em540_pf_l2
        unit_of_measurement: ""
        state_class: measurement
        slave: 1
        address: 0x002F
        data_type: int16
        scale: 0.001
        precision: 3
        scan_interval: 15

      - name: SmartMeter EM540 PF L3
        unique_id: unique_id__em540_pf_l3
        unit_of_measurement: ""
        state_class: measurement
        slave: 1
        address: 0x0030
        data_type: int16
        scale: 0.001
        precision: 3
        scan_interval: 15

        #

      - name: SmartMeter EM540 PF sys
        unique_id: unique_id__em540_pf_sys
        unit_of_measurement: ""
        state_class: measurement
        slave: 1
        address: 0x0031
        data_type: int16
        scale: 0.001
        precision: 3
        scan_interval: 15

      ##########################################################################
      # Frequency

      - name: SmartMeter EM540 Hz
        unique_id: unique_id__em540_hz
        unit_of_measurement: Hz
        state_class: measurement
        device_class: frequency
        slave: 1
        address: 0x0033
        data_type: int16
        scale: 0.1
        precision: 1
        scan_interval: 15

      ##########################################################################
      # Energy

      - name: SmartMeter EM540 kWh+ (imported) L1
        unique_id: unique_id__em540_kwh_l1
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        slave: 1
        address: 0x0040
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 kWh+ (imported) L2
        unique_id: unique_id__em540_kwh_l2
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        slave: 1
        address: 0x0042
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 kWh+ (imported) L3
        unique_id: unique_id__em540_kwh_l3
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        slave: 1
        address: 0x0044
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 kWh+ (imported) total
        unique_id: unique_id__em540_kwh_imported_total
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        slave: 1
        address: 0x0034
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

      - name: SmartMeter EM540 kWh- (exported) total
        unique_id: unique_id__em540_kwh_exported_total
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        slave: 1
        address: 0x004E
        data_type: int32
        swap: word
        scale: 0.1
        precision: 1
        scan_interval: 15

Additional complementary HA template sensor:

template:
  - sensor:
      # Übersetzung der Zustände

      - name: SmartMeter EM540 Carlo Gavazzi Controls identification code
        unique_id: unique_id__em540_cgc_id_code
        icon: mdi:axis-arrow
        state: >
          {%- set raw = states('sensor.smartmeter_em540_carlo_gavazzi_controls_identification_code_raw') -%}
          {% if raw == '1760' %}
            EM540DINAV23XS1X
          {% else %}
            unknown
          {% endif %}
        availability: >
          {{ has_value('sensor.smartmeter_em540_carlo_gavazzi_controls_identification_code_raw') }}

      - name: SmartMeter EM540 Firmware
        unique_id: unique_id__em540_firmware
        icon: mdi:ticket-confirmation-outline
        state: >
          {%- set raw = states('sensor.smartmeter_em540_firmware_raw') -%}
          {%- set lsb_revision = states('sensor.smartmeter_em540_firmware_raw') | int(0) | bitwise_and(255) -%}
          {%- set msb = ((states('sensor.smartmeter_em540_firmware_raw') | float(0) - lsb_revision) / 256) | int(0) -%}
          {%- set minor = msb | bitwise_and(7) -%}
          {%- set major = (msb / 16) | int(0) -%}
          {%- set version = major | string() + '.' + minor | string() + ' rev ' + lsb_revision | string() -%}
          {{ version }}
        availability: >
          {{ has_value('sensor.smartmeter_em540_firmware_raw') }}

      - name: SmartMeter EM540 Measurement mode
        unique_id: unique_id__em540_measure_mode
        icon: mdi:axis-arrow
        state: >
          {%- set raw = states('sensor.smartmeter_em540_measurement_mode_raw') -%}
          {% if raw == '0' %}
            A mode (PFA Absolute)
          {% elif raw == '1' %}
            B mode (PFB Counters accumulation by phase)
          {% elif raw == '2' %}
            C mode (PFC Bidirectional)
          {% else %}
            unknown
          {% endif %}
        availability: >
          {{ has_value('sensor.smartmeter_em540_measurement_mode_raw') }}

      - name: SmartMeter EM540 Device state
        unique_id: unique_id__em540_dev_state
        icon: mdi:axis-arrow
        state: >
          {%- set raw = states('sensor.smartmeter_em540_device_state_raw') -%}
          {% if raw == '0' %}
            RUN
          {% elif raw == '1' %}
            FAULT
          {% elif raw == '2' %}
            CONFIGURATION ERROR
          {% else %}
            unknown
          {% endif %}
        availability: >
          {{ has_value('sensor.smartmeter_em540_device_state_raw') }}

      #
      # Leistung
      #

      - name: SmartMeter EM540 Anschluss Leistungsflussrichtung
        unique_id: unique_id__em540_w_direction
        icon: mdi:transmission-tower
        state: >-
          {% set smartmeter_active_power = states('sensor.smartmeter_em540_w_sys') | float(0) %}
          {{ "Import" if smartmeter_active_power > 0 else "Export" }}
        availability: >-
          {{ has_value('sensor.smartmeter_em540_w_sys') }}

      - name: SmartMeter EM540 W+ (imported)
        unique_id: unique_id__em540_w_imported
        state_class: measurement
        device_class: power
        unit_of_measurement: "W"
        icon: mdi:transmission-tower-export
        state: >-
          {% set smartmeter_active_power = states('sensor.smartmeter_em540_w_sys') | float(0) %}
          {{ smartmeter_active_power if smartmeter_active_power > 0 else 0.0 }}
        availability: >-
          {{ has_value('sensor.smartmeter_em540_w_sys') }}

      - name: SmartMeter EM540 W- (exported)
        unique_id: unique_id__em540_w_exported
        state_class: measurement
        device_class: power
        unit_of_measurement: "W"
        icon: mdi:transmission-tower-import
        state: >-
          {% set smartmeter_active_power = states('sensor.smartmeter_em540_w_sys') | float(0) %}
          {{ (-1) * smartmeter_active_power if smartmeter_active_power < 0 else 0.0 }}
        availability: >-
          {{ has_value('sensor.smartmeter_em540_w_sys') }}

#

utility_meter:
  smartmeter_em540_kwh_imported_daily:
    unique_id: unique_id__em540_kwh_imported_daily
    name: "SmartMeter EM540 kWh+ (imported) daily"
    source: sensor.smartmeter_em540_kwh_imported_total
    cycle: daily

  smartmeter_em540_kwh_exported_daily:
    unique_id: unique_id__em540_kwh_exported_daily
    name: "SmartMeter EM540 kWh- (exported) daily"
    source: sensor.smartmeter_em540_kwh_exported_total
    cycle: daily

  smartmeter_em540_kwh_imported_nightly:
    unique_id: unique_id__em540_kwh_imported_nightly
    name: "SmartMeter EM540 kWh+ (imported) nightly"
    source: sensor.smartmeter_em540_kwh_imported_total
    cycle: daily
    offset:
      hours: 12

Anyone got any guide how to connect the em340 or any tips what to buy ? The easier the better :)?

My configuration.yaml is not working.

modbus:

  • name: hub1
    type: serial
    baudrate: 9600
    bytesize: 8
    method: rtu
    parity: N
    port: /dev/serial/by-id/usb-1a86_USB_Single_Serial_5586006930-if00
    stopbits: 1
    timeout : 10
    message_wait_milliseconds: 1000
    sensors:
    • name: E2 L2 W
      unit_of_measurement: W
      slave: 1
      address: 20
      data_type: int32
      input_type: input
      count: 2
      scan_interval: 15
      lazy_error_count : 999
      scale: 0.1
      swap: word

I get this error message:
Invalid config for [modbus]: E2 L2 W: count: 2 cannot be combined with data_type: int32 @ data[‘modbus’][0][‘sensors’][0]. Got {‘name’: ‘E2 L2 W’, ‘unit_of_measurement’: ‘W’, ‘slave’: 1, ‘address’: 20, ‘data_type’: ‘int32’, ‘input_type’: ‘input’, ‘count’: 2, ‘scan_interval’: 15, ‘lazy_error_count’: 999, ‘scale’: 0.1, ‘swap’: ‘word’}. (See /config/configuration.yaml, line 29).

Does anyone know what the problem is?

Hi,
Just remove “count: 2”.
Mine looks like this for the same value:

  • name: energymeter_1_w_l2
    unique_id: energymeter_1_w_l2
    unit_of_measurement: W
    device_class: power
    state_class: measurement
    slave: 2
    address: 20
    input_type: input
    data_type: int32
    scale: 0.1
    precision: 1
    scan_interval: 15
    lazy_error_count: 999
    swap: word

You can just copy my configuration example from above. Those lines are updated since count in combination with data_type is forbidden. That change came with one of the more recent HA core updates

I find this

and remove both count and salve, with this information I only remove count and it also work!
Thanks a lot