question

Leon Backbier avatar image
Leon Backbier asked

Integrating Battery Data into Home Assistant's Energy Dashboard via Modbus

Hello Victron Community,

I'm currently working on integrating my battery data into the Home Assistant energy dashboard, specifically focusing on the dynamic ESS feature. I'm trying to retrieve the necessary data via Modbus, but I'm encountering some issues.

So far, I've attempted to use Slave: 100, Address: 842. This setup does provide a value, but I'm facing a problem with negative numbers. The INT conversion doesn't seem to work correctly in Home Assistant (HASS), and HASS expects two separate values: one for generation and one for consumption.

Has anyone here experienced similar issues or could offer some guidance on which Modbus register I should be reading to accurately obtain these values?

Any insights or suggestions would be greatly appreciated!

Thank you!

Modbus TCPhome assistant
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

5 Answers
elvis avatar image
elvis answered ·

I dont have any experience with HASS but the register you are querying (842) will provide either a battery negative or battery positive value (+ for charging, - for consumption)

You may find MQTT easier to use as well. Not every measurement has a Modbus register. MQTT provides a much easier way to obtain the values and its structure is simple to follow.

Here is a python Modbus example

#!/usr/bin/env python3


from pymodbus.constants import Defaults
from pymodbus.constants import Endian
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.payload import BinaryPayloadDecoder

VEsystemID = 100

ip = "192.168.20.156" # ip address of GX device or if on venus local try localhost

# Local network ip address of Cerbo GX. Default port 502
client = ModbusClient(ip, port='502')

# All modbus variable requests are sent to this function and it return's the requested value
def modbus_register(address, unit):
    msg     = client.read_input_registers(address, unit=unit)
    decoder = BinaryPayloadDecoder.fromRegisters(msg.registers, byteorder=Endian.Big)
    msg     = decoder.decode_16bit_int()
    return msg

BatteryWatts  = modbus_register(842, VEsystemID)

print(f"BatteryWatts {BatteryWatts}")

Edit Feb 2024:

FYI the above is for the older pymodbus

here is one for the newer pymodbus

                  
  1. #!/usr/bin/env python3
  2. from pymodbus.constants import Endian
  3. from pymodbus.client import ModbusTcpClient as ModbusClient
  4. from pymodbus.payload import BinaryPayloadDecoder
  5. BmvID = 223
  6. ip = "192.168.20.156"
  7. client = ModbusClient(ip, port='502')
  8. def modbus_register(address, slave):
  9.     msg     = client.read_holding_registers(address, slave=slave)
  10. decoder = BinaryPayloadDecoder.fromRegisters(msg.registers, byteorder=Endian.BIG)
  11. msg = decoder.decode_16bit_int()
  12. return msg
  13. BatterySOC = modbus_register(266, BmvID) / 10
  14. print(f"BatterySOC {BatterySOC}")
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Leon Backbier avatar image
Leon Backbier answered ·

Thanks Elvis, Indeed in Python i managed to get the data in the right format. However, MQTT sounds prommising is there an example where i can work from?


2 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

elvis avatar image elvis commented ·
0 Likes 0 ·
elvis avatar image elvis elvis commented ·

get MQTT explorer to look at all the values.

I think You will still need a keepalive

0 Likes 0 ·
dmsims avatar image
dmsims answered ·
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Leon Backbier avatar image
Leon Backbier answered ·

Okay, using the examples provided i got it working in Home assistant, if anybode interested i can post the yaml files. However, i still struggling reading the critical load from modbus anyone knows which register i should read? I used the excel but outside ID 100 i got reading exceptions. Exception Response(131, 3, GatewayPathUnavailable). also when i use a python script to read



2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

maek avatar image
maek answered ·

Yes, please post your yaml files. I‘m currently struggling with the same issue.

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Related Resources

Additional resources still need to be added for this topic

Modbus TCP Basics