MODBUS data from SMA Inverter

I use this in config

# SMA over modbus
modbus:
  type: tcp
  host: 192.168.178.171
  port: 502

and this in sensors

- platform: modbus
  scan_interval: 30
  registers:
    - name: Gesamtertrag
      hub: sma
      unit_of_measurement: kWh   # see register description in section 5.4.1 of the document
      slave: 3                   # use the UnitID you found in Sunny Explorer
      register: 30531            # use a register from section 5.4.1 of the document
      register_type: input       # all registers starting with 3 need to have the type input
      count: 2                   # use the correct count for the register see section 5.4.1 of the document

and get this error in my log

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 179, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/modbus/sensor.py", line 129, in setup_platform
    hub = hass.data[MODBUS_DOMAIN][hub_name]
KeyError: 'sma'

I can login to the webinterface just fine. I came here bc the standard SMA integration was not working, but am having issues with this as well. Anyone care to shed some light on this?

I think it’s because you are missing the sma name for the modbus tcp, port and ip settings.
Add “name: sma” or it might also work if you remove “hub: sma” from the registers.

You have:

# SMA over modbus
modbus:
  type: tcp
  host: 192.168.178.171
  port: 502

Should be (if you use “hub: sma”)

# SMA over modbus
modbus:
  name: sma
  type: tcp
  host: 192.168.178.171
  port: 502

I will try this too :slight_smile:

Modbus seems broken in 0.108 before in worked perfectly.
There were fixes in 0.108.2

Fix modbus default delay (@janiversen - #33877) (modbus docs)

The new default delay is 0.

delay
(integer)(Optional)
Time to sleep in seconds after connecting and before sending messages. Some modbus-tcp servers need a short delay typically 1-2 seconds in order to prepare the communication. If a server accepts connecting, but there is no response to the requests send, this parameter might help.

Default value:
0

My modbus integration looked like this

modbus:
  name: sma
  type: tcp
  host: 192.168.178.101
  port: 502

Unfortunately after adding the delay parameter with 1 or 2 doesn’t help. I get the following errors:

Logger: pymodbus.factory
Source: __main__.py:342 
First occurred: 11:26:58 (6 occurrences) 
Last logged: 11:29:32

Unable to decode response Modbus Error: Unknown response 59
Unable to decode response Modbus Error: Unknown response 90
Unable to decode response Modbus Error: Unknown response 125
index out of range
Logger: homeassistant.core
Source: /usr/src/homeassistant/homeassistant/core.py:144 
First occurred: 11:26:58 (6 occurrences) 
Last logged: 11:29:32

Error doing job: Fatal error: protocol.data_received() call failed.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 826, in _read_ready__data_received
    self._protocol.data_received(data)
  File "/usr/local/lib/python3.7/site-packages/pymodbus/client/asynchronous/asyncio/__init__.py", line 192, in data_received
    self._dataReceived(data)
  File "/usr/local/lib/python3.7/site-packages/pymodbus/client/asynchronous/asyncio/__init__.py", line 139, in _dataReceived
    self.framer.processIncomingPacket(data, self._handleResponse, unit=unit)
  File "/usr/local/lib/python3.7/site-packages/pymodbus/framer/socket_framer.py", line 165, in processIncomingPacket
    self._process(callback, error=True)
  File "/usr/local/lib/python3.7/site-packages/pymodbus/framer/socket_framer.py", line 175, in _process
    raise ModbusIOException("Unable to decode request")
pymodbus.exceptions.ModbusIOException: Modbus Error: [Input/Output] Unable to decode request

I have the same issue with 0.108.1-3.
Reverted to 0.107.7 and it works fine again.

Yes modbus is definitely broken in 0.108 1-3
I stay on 0.108.3 but I switched to NodeRed and MQTT to retrieve my modbus data.
everything works now again.

I wrote a tutorial how to do this:

I thought it was me!, nothing would make my SunGrow inverter connect reliably.

I gave up on the Modbus intergration and went node-red also

Hi Darren, welcome to the club!
I see that you use the Modbus reader with input definition via a function block. Do you mind sharing the content of the block. I would like to add this approach to my tutorial with reference to your post

regards Chris

According to the source code of the modbus flex-getter, @drmcinnes is just reading an address range. You can also take a look at the Modbus-Flex-Suite examples via Import -> Examples -> node-red-contrib-modbus.

Sorry I haven’t been checking my mail, I’ll post it later tonight.

Heres the updated flow and screenshot, it’s still a work in progress!

https://github.com/drmcinnes/sg5k-node-red

It looks like the native modbus integration is working again. in 0.108.7 they reverted back to the 0.107.7 version

1 Like

Hi All,

This is my current modbus code and template for the SMA Tripower 8.0 . With some help of the @chris_ka post above I managed to realise a good working solution for the SMA.Probably the code below is not the best code but it does the job.

Note: the ‘day power’ was wrong and after a firmware update of the inverter the values made sense :slight_smile:

How to convert this to a nice Home Assistant integration?

#configuration.yaml

#SMA solar inverter Tripower 8.0
modbus:
  type: tcp
  host: 192.168.1.123
  port: 502


# sensor.yaml

#  sensor:
  - platform: modbus
    scan_interval: 2
    registers:
    - name: SMApwrL1
      unit_of_measurement: kW
      slave: 3
      register: 30777
      count: 2
      data_type: uint
      scale: 0.001
      precision: 2
    - name: SMApwrL2
      unit_of_measurement: kW
      slave: 3
      register: 30779
      count: 2
      data_type: uint
      scale: 0.001
      precision: 2
    - name: SMApwrL3
      unit_of_measurement: kW
      slave: 3
      register: 30781
      count: 2
      data_type: uint
      scale: 0.001
      precision: 2
    - name: SMAcurL1
      unit_of_measurement: A
      slave: 3
      register: 30977
      count: 2
      data_type: uint
      scale: 0.001
      precision: 2
    - name: SMAcurL2
      unit_of_measurement: A
      slave: 3
      register: 30979
      count: 2
      data_type: uint
      scale: 0.001
      precision: 2
    - name: SMAcurL3
      unit_of_measurement: A
      slave: 3
      register: 30981
      count: 2
      data_type: uint
      scale: 0.001
      precision: 2
    - name: SMAvoltL1
      unit_of_measurement: V
      slave: 3
      register: 30783
      count: 2
      data_type: uint
      scale: 0.01
      precision: 1
    - name: SMAvoltL2
      unit_of_measurement: V
      slave: 3
      register: 30785
      count: 2
      data_type: uint
      scale: 0.01
      precision: 1
    - name: SMAvoltL3
      unit_of_measurement: V
      slave: 3
      register: 30787
      count: 2
      data_type: uint
      scale: 0.01
      precision: 1
    - name: SMAnetHZ
      unit_of_measurement: Hz
      slave: 3
      register: 30803
      count: 2
      data_type: uint
      scale: 0.01
      precision: 2
    - name: SMApwr
      unit_of_measurement: kW
      slave: 3
      register: 30775
      count: 2
      data_type: int
      scale: 0.001
      precision: 2
    - name: SMAtotal
      unit_of_measurement: MW
      slave: 3
      register: 30529
      count: 2
      data_type: uint
      scale: 0.000001
      precision: 2
    - name: SMAtoday
      unit_of_measurement: kWh
      slave: 3
      register: 30535
      count: 2
      data_type: uint
      scale: 0.001
      precision: 2
    - name: SMAdevtype
      slave: 3
      register: 30053
      count: 2
      data_type: int 
    - name: SMAgridrelay
      slave: 3
      register: 30217
      count: 2
      data_type: int

  - platform: template
    sensors:
      sma_power_l1:
        value_template: "{% if states('sensor.smapwrl1')|float > 10000 %}0{% else %}{{ states('sensor.smapwrl1') | float | round(2)}}{% endif %}"
        friendly_name: "SMA power L1"
        unit_of_measurement: 'kW'
        icon_template: mdi:solar-power
      sma_power_l2:
        value_template: "{% if states('sensor.smapwrl2')|float > 10000 %}0{% else %}{{ states('sensor.smapwrl2') | float | round(2)}}{% endif %}"
        friendly_name: "SMA power L2"
        unit_of_measurement: 'kW'
        icon_template: mdi:solar-power
      sma_power_l3:
        value_template: "{% if states('sensor.smapwrl3')|float > 10000 %}0{% else %}{{ states('sensor.smapwrl3') | float | round(2)}}{% endif %}"
        friendly_name: "SMA power L3"
        unit_of_measurement: 'kW'
        icon_template: mdi:solar-power
      sma_stroom_l1:
        value_template: "{% if states('sensor.smacurl1')|float > 100 %}0{% else %}{{ states('sensor.smacurl1') | float | round(2)}}{% endif %}"
        friendly_name: "SMA stroom L1"
        unit_of_measurement: 'A'
        icon_template: mdi:solar-power
      sma_stroom_l2:
        value_template: "{% if states('sensor.smacurl2')|float > 100 %}0{% else %}{{ states('sensor.smacurl2') | float | round(2)}}{% endif %}"
        friendly_name: "SMA stroom L2"
        unit_of_measurement: 'A'
        icon_template: mdi:solar-power
      sma_stroom_l3:
        value_template: "{% if states('sensor.smacurl3')|float > 100 %}0{% else %}{{ states('sensor.smacurl3') | float | round(2)}}{% endif %}"
        friendly_name: "SMA stroom L3"
        unit_of_measurement: 'A'
        icon_template: mdi:solar-power
      sma_spanning_l1:
        value_template: "{% if states('sensor.smavoltl1')|float > 1000 %}0{% else %}{{ states('sensor.smavoltl1') | float | round(2)}}{% endif %}"
        friendly_name: "SMA spanning L1"
        unit_of_measurement: 'V'
        icon_template: mdi:solar-power
      sma_spanning_l2:
        value_template: "{% if states('sensor.smavoltl2')|float > 1000 %}0{% else %}{{ states('sensor.smavoltl2') | float | round(2)}}{% endif %}"
        friendly_name: "SMA spanning L2"
        unit_of_measurement: 'V'
        icon_template: mdi:solar-power
      sma_spanning_l3:
        value_template: "{% if states('sensor.smavoltl3')|float > 1000 %}0{% else %}{{ states('sensor.smavoltl3') | float | round(2)}}{% endif %}"
        friendly_name: "SMA spanning L3"
        unit_of_measurement: 'V'
        icon_template: mdi:solar-power
      sma_netfrequentie:
        value_template: "{% if states('sensor.smanethz')|float > 1000 %}0{% else %}{{ states('sensor.smanethz') | float | round(2)}}{% endif %}"
        friendly_name: "SMA Netfrequentie"
        unit_of_measurement: 'Hz'
        icon_template: mdi:solar-power
      sma_power:
        value_template: "{% if states('sensor.smapwr')|float < 0 %}0{% else %}{{ states('sensor.smapwr') | float | round(2)}}{% endif %}"
        friendly_name: "SMA Power"
        unit_of_measurement: 'kW'
        icon_template: mdi:solar-power
      sma_total:
        value_template: "{% if states('sensor.smatotal')|float < 0 %}0{% else %}{{ states('sensor.smatotal') | float | round(2)}}{% endif %}"
        friendly_name: "SMA Total"
        unit_of_measurement: 'MWh'
        icon_template: mdi:solar-power
      sma_today:
        value_template: "{% if states('sensor.smatoday')|float > 1000 %}0{% else %}{{ states('sensor.smatoday') | float | round(2)}}{% endif %}"
        friendly_name: "SMA Today"
        unit_of_measurement: 'kWh'
        icon_template: mdi:solar-power
      sma_device_type:
        value_template: "{% if states('sensor.smadevtype')|float == 9347 %}Tripower 8.0{% else %}?{% endif %}"
        friendly_name: "SMA device type"
        icon_template: mdi:solar-power
      sma_grid_relay:
        value_template: "{% if states('sensor.smagridrelay')|float == 51 %}closed{% elif states('sensor.smagridrelay')|float == 311 %}open{% else %}?{% endif %}"
        friendly_name: "SMA grid relay"
        icon_template: mdi:solar-power
1 Like

Hey guys. I’ve got a Sungrow 8K inverter and was pulling in data via MQTT using ‘meltaxa’s solariot script’, which I was able to use grafana for statistic, and HA templating to calculate net load and daily/month costs, and other useful stuff. This was working fine for a few months after I had everything installed, and then it just stopped working.

The python script just started returning ModbusIOException object has no attribute registers “sungrow”, and I’ve tried various other python scripts.

I’ve tried various modbus scanning tools, and I can confirm port 502 is still open. I can physically check on the inverter panel it is using (slave) id ‘1’.

I tried @drmcinnes node-red flow today to see what I might get. The error it returns is Error: Modbus exception 4: Slave device failure (device reports internal error) An unrecoverable error??

Any ideas or suggestions? Data is still being populated in the Sungrow isolarcloud portal. The only thing I can think of is a possible firmware update was push down to it, preventing TCP modbus interrogation.

Thanks in advance.

Mine is still working as of today, no known firmware updates this month.

I’d check that the IP address is still the same and try rebooting the inverter.

Thanks, @drmcinnes. Yeah, tried rebooting it a few times. Left it off for 20mins also. I’ve got a reserved IP for it on a fully open LAN segment, and confirmed the error changes to a timeout when the unit is turned off.

image

I tried ModbusView and the specific error there is EC4 Slave Device Failure. Googling doesn’t come up with any solution for error unfortunately. I can’t think of anything that might have changed…

image

The registers previously working are below.

image

Hi everyone
Totally new in home assistant and programming, dont even ask. I have SMA STP7000 inverter and Home manager 2 which I wanted to get connecetd to HA. Big thanks to Chris for his tutorials, managed to get the Node red done for Home manager and now trying to figure out how to get the solar production out. Will probably follow his another tutorial and see what happens :slight_smile: While I was searching how to get the Home manager and inverter connected to HA I found this: https://www.home-assistant.io/integrations/sma/
Is it working only for USA? I cant get anything out from this and I actually think that it shouldnt be the IP of inverter but home manager. Inverter would not have access to power supplied from grid no?

Edit: Big thx to MBolt for his script, didnt need all the sensors so I have trimmed it a bit and got working. :slight_smile:

1 Like

Anyone have advice on communication hardware to use, from Sunny Island SI6048US, which has rs485 port?

Sounds like most in this thread have a Sunny Webbox or Data Manager box between the inverters and your local network / internet. These feel pricey?

I have a little $2.50 RS485 - TTL board that I’ll try with an ESP32. Anyone do that? Thanks!

I’m getting the same error - I suspect a firmware update, which is a shame because the isolarcloud portal doesnt update as frequently as I would like

Mine died three days ago as well, probably a forced firmware update…

It looks like i’ll have to switch to reading the RS485 directly.

It’s my own fault for not blocking remote access.