Modbus basic configuration - Modbus addressing

Hi everybody!

Taking first steps in HA and decided to connect solar inverter directly to HA Modbus integration.
Seems so easy, if you read the HA manual but in reality there is something I missing.

Inverter has RS 485 connector, which is connected to HA host (Odroid) with RS485 to USB serial connector.

Code:

modbus:
- name: Sofar
type: serial
method: rtu
baudrate: 9600
bytesize: 8
parity: N
port: /dev/ttyUSB0
stopbits: 0

When trying to restart the HA to get connection working there will be error, states Key error type
I really have no idea what am I doing wrong.

Logger: aiohttp.server
Source: components/modbus/validators.py:232
First occurred: July 23, 2023 at 22:39:19 (7 occurrences)
Last logged: 13:50:11

Error handling request
Traceback (most recent call last):
File “/usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py”, line 433, in _handle_request
resp = await request_handler(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/aiohttp/web_app.py”, line 504, in _handle
resp = await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/aiohttp/web_middlewares.py”, line 117, in impl
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/http/security_filter.py”, line 85, in security_filter_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/http/forwarded.py”, line 100, in forwarded_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/http/request_context.py”, line 28, in request_context_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/http/ban.py”, line 80, in ban_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/http/auth.py”, line 236, in auth_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/http/view.py”, line 148, in handle
result = await handler(request, **request.match_info)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/api/init.py”, line 329, in post
await hass.services.async_call(
File “/usr/src/homeassistant/homeassistant/core.py”, line 1965, in async_call
response_data = await coro
^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/core.py”, line 2005, in _execute_service
return await cast(
^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/hassio/init.py”, line 565, in async_handle_core_service
errors = await conf_util.async_check_ha_config_file(hass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/config.py”, line 978, in async_check_ha_config_file
res = await check_config.async_check_ha_config_file(hass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/check_config.py”, line 183, in async_check_ha_config_file
config = config_schema(config)
^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/voluptuous/schema_builder.py”, line 272, in call
return self._compiled([], data)
^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/voluptuous/schema_builder.py”, line 595, in validate_dict
return base_validate(path, iteritems(data), out)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/voluptuous/schema_builder.py”, line 387, in validate_mapping
cval = cvalue(key_path, value)
^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/voluptuous/validators.py”, line 229, in _run
return self._exec(self._compiled, value, path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/voluptuous/validators.py”, line 353, in _exec
v = func(path, v)
^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/voluptuous/schema_builder.py”, line 818, in validate_callable
return schema(data)
^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/components/modbus/validators.py”, line 232, in duplicate_modbus_validator
if hub[CONF_TYPE] == SERIAL:
~~~^^^^^^^^^^^
KeyError: 'type`

This is a total guess, but the last part of the log is

duplicate_modbus_validator
if hub[CONF_TYPE] == SERIAL:

Is the serial port set up twice somehow?

Solved.
I had already stated modbus in configuration.yaml and so it wasn needed to state again in modbus.yaml

Continuing this topic, modbus connection is up, sensor is created, HA is sending the request but doesnt receive answer or isnt able to decode it.
Can anybody help with Modbus addressing?
We are talking now about the Sofar HYD - KTL type inverter.
Lets take a sample, reading the information:
Device Modbus documentation says:

Read Format
Device Address Function Code Start Address Read Register No. CRC
1 Byte 1 Byte Hi Byte Low Byte Hi Byte Low Byte Low Byte Hi Byte
Device Address:1-247 ReadFunction Code:0x03 Modbus-CRC16

From register list we can find:
0584 Voltage_PV1 U16 0,1 V R

My sensor code:

  sensors:
    - name: PV1
      slave: 01
      address: 0584
      input_type: input
      data_type: uint16

How should I address the request to get the response?

Update with debug:

2023-08-02 18:59:28.804 DEBUG (SyncWorker_15) [pymodbus.logging] Running transaction 182
2023-08-02 18:59:28.805 DEBUG (SyncWorker_15) [pymodbus.logging] SEND: 0x1 0x4 0x2 0x48 0x0 0x1 0xb0 0x64
2023-08-02 18:59:28.805 DEBUG (SyncWorker_15) [pymodbus.logging] Changing state to IDLE - Last Frame End - None Current Time stamp - 1690991968.805096
2023-08-02 18:59:28.809 DEBUG (SyncWorker_15) [pymodbus.logging] New Transaction state "SENDING"
2023-08-02 18:59:28.810 DEBUG (SyncWorker_15) [pymodbus.logging] Changing transaction state from "SENDING" to "WAITING FOR REPLY"
2023-08-02 18:59:38.826 DEBUG (SyncWorker_15) [pymodbus.logging] No response received, Expected 7 bytes Received 0 bytes !!!!
2023-08-02 18:59:38.826 DEBUG (SyncWorker_15) [pymodbus.logging] Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
2023-08-02 18:59:38.826 DEBUG (SyncWorker_15) [pymodbus.logging] RECV: 
2023-08-02 18:59:38.826 DEBUG (SyncWorker_15) [pymodbus.logging] Getting transaction 1
2023-08-02 18:59:38.827 DEBUG (SyncWorker_15) [pymodbus.logging] Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
2023-08-02 18:59:38.827 DEBUG (SyncWorker_15) [homeassistant.components.modbus.modbus] Pymodbus: sofarMODBUSRTU: Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
2023-08-02 18:59:43.806 DEBUG (SyncWorker_8) [pymodbus.logging] Current transaction state - TRANSACTION_COMPLETE

Might be easier to just install Universal Solar Inverter over Modbus RS485 / TCP custom_component (Growatt, Sofar, SolaX, Solis) assuming you have a working communications to your Inverter.

Saves you having to go through all of the docs, as it’s already done for you.

Hi Wills106!
Thanks, this one is installed and works great, thanks for your work!
But…this is a one-way train, I need want also to send commands to inverter, and make automations for that in HA, that is the target here.

Just so you know Sofar docs are in hex.
You should be reading 0x584 or 1412 in decimal.
You are actually trying to read 0x248

My Integration supports writing to the Inverter and you can still automate it.

If anything is missing you can either submit a PR or let me know what’s missing and I can look into adding it when I get chance.

I have tried also 1412 - didnt work. What is that what Im missing here about the connection to Sofar inverter Modbus?
I have tried to access also via ESPHome Modbus component, also no success - no response from slave.

1 Like

Really? Might be that you have updated the integration since I have installed it.
I will check it, thanks!

I would double check if the baud is actually 9600 on your Inverter LCD. It might be set to 57600. Also worth double checking A&B wires are not reversed.

All that is done, baud rate is 9600 on inverter.
A and B wires are changed several times.
I out of ideas, so I will check your integration.

These registers are holding registers and not input registers.

I have tried also with holding…

Have you tried it with the register correctly set for decimal though?

  sensors:
    - name: PV1 Voltage
      slave: 1
      address: 1412
      input_type: holding
      data_type: uint16

Yes.
Now I installed above mentioned integration, checking it.
I had Solarman integration installed, mixed up with that one.