# 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…
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
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
Many thanks to you!
It worked with my RS485 to TCP converter.
I had almost given up on the meters.
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/ttyUSB1sensors :
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
Did you configure the meter to do two-way metering?
You can do it with freely available UCS software (MS-Windows only I guess)
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
- name: E2 L2 W
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
I have this setup now in my configuration.yaml, and it works (mostly)
mqtt:
sensor:
- name: "EM340_in"
state_topic: "EM340/kWh_in"
value_template: "{{ value | float | round (2) }}"
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
- name: "EM340_out"
state_topic: "EM340/kWh_out"
value_template: "{{ value | float | round (2) }}"
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
- name: "EM340_A1"
state_topic: "EM340/A1"
value_template: "{{ value | float | round (2) }}"
unit_of_measurement: "A"
device_class: current
state_class: measurement
- name: "EM340_A2"
state_topic: "EM340/A2"
value_template: "{{ value | float | round (2) }}"
unit_of_measurement: "A"
device_class: current
state_class: measurement
- name: "EM340_A3"
state_topic: "EM340/A3"
value_template: "{{ value | float | round (2) }}"
unit_of_measurement: "A"
device_class: current
state_class: measurement
I am publishing to mqqt with a python script on a raspberry pi that has a RS485-CAN-hat attached to it. I intend to swap out the rpi for an esp8266 later in the week. The python code :
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
import paho.mqtt.client as mqtt #import the client1
broker_address='192.168.1.90'
port=1883
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, topic):
self.kind = kind
self.unit = unit
self.address = address
self.count = count
self.converter = converter
self.label = label
self.decimals = decimals
self.topic = topic
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,'EM340/A1'),
ModbusRequestDefinition('H', 1, 0x000E, 2, '', 'A2', 1000,'EM340/A2'),
ModbusRequestDefinition('H', 1, 0x0010, 2, '', 'A3', 1000,'EM340/A3'),
ModbusRequestDefinition('H', 1, 0x0034, 2, '', 'kWh+', 10,'EM340/kWh_in'),
ModbusRequestDefinition('H', 1, 0x004E, 2, '', 'kWh-', 10,'EM340/kWh_out')
]
client = ModbusSerialClient(method='rtu', port='/dev/ttyAMA0', baudrate=9600, stopbits=1, timeout=1)
client.connect()
delay = 0.05
#period = 0.5
period = 5
mqttclient = mqtt.Client("P1") #create new instance
mqttclient.username_pw_set("your mqqt user", "your mqtt password") # !! insert your own credentials here !!
mqttclient.connect(broker_address) #connect to broker
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" )
# print(req.topic, end ="\t" )
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big, wordorder=Endian.Little)
val = decoder.decode_32bit_int()/req.decimals
# print(val)
mqttclient.publish(req.topic,val)
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()
this is old pymodbus v2.5 as I couldnt yet port to the new release v3. Anyone have a working example there that I could start from then I would be happy to adapt.
Then submit the EM340_in and EM340_out sensors to the energy wizard and after a few hours of running you can see the stats generating a nice energy graph.
Now I have to mimic the same RS485 > ESP8266 > MQTT > HA for my inverter
Sounds good and well. Go for it!
Just wanted to quickly mention that I am a very happy user of the Protoss RS485 product family, specifically the PE11-H. Ethernet or Wifi, 230V or 5V, Plug and play for 10-30€
Anyone willing to help here with a very basic question for me to get things started.
I do have a Gavazzi EM330 with ModBus interface connected to a Wallbox Pulsar EV charger. I believe that the Wallbox acting as a host and EM330 client. Could I just plug i.e. this in parallel to the EM330 data ports to steal the readings to HA?
Here is my configuration.
modbus.yaml
# in configuration.yaml insert the following row which includes this file
# modbus: !include modbus.yaml
- name: modbus_serial1
delay: 5
timeout: 5
message_wait_milliseconds: 1000
type: serial
method: rtu
baudrate: 9600
bytesize: 8
stopbits: 1
parity: N
# To find the path to the port, login using SSH and use "ls -al /dev/serial/by-id/" to
# find the path to the USB-interface (ttyUSB<x> where x is a number)
port: /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_B002EGCF-if00-port0
# Template for Carlo Gavazzi EM340-S1 Modbus MID RS-485
# Valid for Home Assistant v2024.7.4
# Register documentation for sensors: https://gavazzi.se/app/uploads/2020/11/em330_wr_et330_et340_cp.pdf
# Feel free to improve
sensors:
- name: WR_V_L1-N
unique_id: wr_v_l1-n
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x0000
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_V_L2-N
unique_id: wr_v_l2-n
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x0002
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_V_L3-N
unique_id: wr_v_l3-n
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x0004
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_V_L1-L2
unique_id: wr_v_l1-l2
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x0006
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_V_L2-L3
unique_id: wr_v_l2-l3
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x0008
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_V_L3-L1
unique_id: wr_v_l3-l1
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x000A
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_A_L1
unique_id: wr_a_l1
unit_of_measurement: A
state_class: measurement
device_class: current
slave: 1
address: 0x000C
data_type: int32
input_type: input
precision: 1
scale: 0.001
scan_interval: 15
swap: word
- name: WR_A_L2
unique_id: wr_a_l2
unit_of_measurement: A
state_class: measurement
device_class: current
slave: 1
address: 0x000E
data_type: int32
input_type: input
precision: 1
scale: 0.001
scan_interval: 15
swap: word
- name: WR_A_L3
unique_id: wr_a_l3
unit_of_measurement: A
state_class: measurement
device_class: current
slave: 1
address: 0x0010
data_type: int32
input_type: input
precision: 1
scale: 0.001
scan_interval: 15
swap: word
- name: WR_W_L1
unique_id: wr_w_l1
unit_of_measurement: W
state_class: measurement
device_class: power
slave: 1
address: 0x0012
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_W_L2
unique_id: wr_w_l2
unit_of_measurement: W
state_class: measurement
device_class: power
slave: 1
address: 0x0014
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_W_L3
unique_id: wr_w_l3
unit_of_measurement: W
state_class: measurement
device_class: power
slave: 1
address: 0x0016
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VA_L1
unique_id: wr_va_l1
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x0018
data_type: int32
input_type: input
precision: 1
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VA_L2
unique_id: wr_va_l2
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x001A
data_type: int32
input_type: input
precision: 1
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VA_L3
unique_id: wr_va_l3
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x001C
data_type: int32
input_type: input
precision: 1
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VAR_L1
unique_id: wr_var_l1
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x001E
data_type: int32
input_type: input
precision: 1
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VAR_L2
unique_id: wr_var_l2
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x0020
data_type: int32
input_type: input
precision: 1
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VAR_L3
unique_id: wr_var_l3
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x0022
data_type: int32
input_type: input
precision: 1
scale: 0.1
scan_interval: 15
swap: word
- name: WR_V_L-N
unique_id: wr_v_l-n
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x0024
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_V_L-L
unique_id: wr_v_l-L
unit_of_measurement: V
state_class: measurement
device_class: voltage
slave: 1
address: 0x0026
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_W
unique_id: wr_w
unit_of_measurement: W
state_class: measurement
device_class: Power
slave: 1
address: 0x0028
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VA
unique_id: wr_va
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x002A
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_VAR
unique_id: wr_var
unit_of_measurement: VA
state_class: measurement
device_class: reactive_power
slave: 1
address: 0x002C
data_type: int32
input_type: input
precision: 0
scale: 0.1
scan_interval: 15
swap: word
- name: WR_PF_L1
unique_id: wr_pf_l1
state_class: measurement
device_class: power_factor
slave: 1
address: 0x002E
data_type: int16
input_type: input
precision: 3
scale: 0.001
scan_interval: 15
- name: WR_PF_L2
unique_id: wr_pf_l2
state_class: measurement
device_class: power_factor
slave: 1
address: 0x002F
data_type: int16
input_type: input
precision: 3
scale: 0.001
scan_interval: 15
- name: WR_PF_L3
unique_id: wr_pf_l3
state_class: measurement
device_class: power_factor
slave: 1
address: 0x0030
data_type: int16
input_type: input
precision: 3
scale: 0.001
scan_interval: 15
- name: WR_PF
unique_id: wr_pf
state_class: measurement
device_class: power_factor
slave: 1
address: 0x0031
data_type: int16
input_type: input
precision: 2
scale: 0.001
scan_interval: 15
- name: WR_Phase_sequence
unique_id: wr_phase_sequence
state_class: measurement
slave: 1
address: 0x010E
data_type: int32
input_type: input
scan_interval: 60
- name: WR_HZ
unique_id: wr_hz
unit_of_measurement: Hz
state_class: measurement
device_class: frequency
slave: 1
address: 0x0033
data_type: int16
input_type: input
precision: 2
scale: 0.1
scan_interval: 15
- name: WR_kWh_Positive_Total
unique_id: wr_wh_positive_total
unit_of_measurement: kWh
state_class: total_increasing
device_class: energy
slave: 1
address: 0x0034
data_type: int32
input_type: input
precision: 1
scale: 0.000001
scan_interval: 60
- name: WR_kVARh_Positive_Total
unique_id: wr_kvarh_positive_total
unit_of_measurement: kWh
state_class: total_increasing
device_class: energy
slave: 1
address: 0x0036
data_type: int32
input_type: input
precision: 1
scale: 0.000001
scan_interval: 60
- name: WR_kW_DMD
unique_id: wr_kw_dmd
unit_of_measurement: kW
device_class: power
slave: 1
address: 0x0038
data_type: int32
input_type: input
precision: 1
scale: 0.00001
scan_interval: 15
- name: WR_kW_DMD_Peak
unique_id: wr_kw_dmd_peak
unit_of_measurement: kW
device_class: power
slave: 1
address: 0x003A
data_type: int32
input_type: input
precision: 1
scale: 0.00001
scan_interval: 15
- name: WR_kWh_Positive_Partial
unique_id: wr_kwh_positive_partial
unit_of_measurement: kWh
state_class: total_increasing
device_class: energy
slave: 1
address: 0x003C
data_type: int32
input_type: input
precision: 1
scale: 0.000001
scan_interval: 60
- name: WR_kVARh_Positive_Partial
unique_id: wr_kvarh_positive_partial
unit_of_measurement: kWh
state_class: total_increasing
device_class: energy
slave: 1
address: 0x003E
data_type: int32
input_type: input
precision: 1
scale: 0.00001
scan_interval: 60
- name: WR_kWh_L1
unique_id: wr_kwh_l1
unit_of_measurement: kWh
state_class: total_increasing
device_class: energy
slave: 1
address: 0x0040
data_type: int32
input_type: input
precision: 1
scale: 0.000001
scan_interval: 60
- name: WR_kWh_L2
unique_id: wr_kwh_l2
unit_of_measurement: kWh
state_class: total_increasing
device_class: energy
slave: 1
address: 0x0042
data_type: int32
input_type: input
precision: 1
scale: 0.000001
scan_interval: 60
- name: WR_kWh_L3
unique_id: wr_kwh_l3
unit_of_measurement: kWh
state_class: total_increasing
device_class: energy
slave: 1
address: 0x0044
data_type: int32
input_type: input
precision: 1
scale: 0.000001
scan_interval: 60