SMA Energy Meter in Home Assistant

is this something ? Solved: convert existing python script to HA component for SMA inverters ?

Ya, no worries dude, that’s what the forum is for. @Phuturist was kind enough to share his code on another thread so you may want to try it here: Solved: convert existing python script to HA component for SMA inverters

Thanks to @Phuturist for sharing! @faecon do report back when you’ve tried it.

I cannot change the modbus variable, because I only can login as user and not as an installer

You need to login as installer. Mine had some default password, not sure what it was anymore but Google for it and you will find it.

1 Like

So I’ve been busy rewriting my .yaml to use ModBus instead of WebConnect. Trouble is HA’s ModBus implementation only allows for one ModBus device. With a battery charger and an inverter, I need at least 2 ModBus devices… I decided the easiest route would be to run a separate instance of HA in Docker, have that read the ModBus values from my Sunny Boy, then publish MQTT messages with the values that I need.
Here is the configuration of the HA instance used to do the above:

homeassistant:
  name: Sunny Boy Sensors
  latitude: !secret home_latitude
  longitude: !secret home_longitude
  elevation: !secret home_elevation
  unit_system: metric
  time_zone: !secret home_time_zone
  customize: !include customize.yaml
  customize_glob: !include customize_global.yaml
config:
#frontend:
mqtt:
  broker: !secret mqtt_url
  port: !secret mqtt_port
modbus:
  type: tcp
  host: !secret sma_sb_ip
  port: !secret sma_sb_port
#http:
#conversation:
#history:
#recorder:
#logbook:
sensor:
  - platform: modbus
    scan_interval: 15
    registers:
      - name: Modbus SB Daily Yield
        unit_of_measurement: kWh
        slave: 3
        register: 30517
        count: 4
        data_type: int
        scale: 0.001
        precision: 3
      - name: Modbus SB Total Yield
        unit_of_measurement: kWh
        slave: 3
        register: 30513
        count: 4
        data_type: int
        scale: 0.001
        precision: 3
      - name: Modbus SB PV Power
        unit_of_measurement: W
        slave: 3
        register: 30775
        count: 2
        data_type: uint
      - name: Modbus SB Grid Power
        unit_of_measurement: W
        slave: 3
        register: 30865
        count: 2
        data_type: uint
      - name: Modbus SB Grid Feed
        unit_of_measurement: W
        slave: 3
        register: 30867
        count: 2
        data_type: uint
  - platform: template
    sensors:
      modbus_sb_pv_production:
        friendly_name: 'PV Output'
        value_template: >-
            {% if states('sensor.modbus_sb_pv_power')|float < 10000 %}
              {{ states('sensor.modbus_sb_pv_power') }}
            {% else %}
              0
            {% endif %}
        unit_of_measurement: "W"
      modbus_sb_total_power:
        friendly_name: 'Total consumption'
        unit_of_measurement: "W"
        value_template: '{{ (((states.sensor.modbus_sb_grid_power.state | float) +
          (states.sensor.modbus_sb_pv_production.state | float))) }}'
automation:
  - id: mqtt_publish_sb
    alias: "MQTT Publish Sunny Boy"
    trigger:
    - platform: state
      entity_id: "sensor.modbus_sb_grid_power"
    action:
    - service: mqtt.publish
      data_template:
        topic: "sma/sb"
        payload: '{"grid_feed":{{ states("sensor.modbus_sb_grid_feed") }}, "grid_consumption":{{ states("sensor.modbus_sb_grid_power") }}, "production":{{ states("sensor.modbus_sb_pv_production") }}, "daily_yield":{{ states("sensor.modbus_sb_daily_yield") }}, "total_yield":{{ states("sensor.modbus_sb_total_yield") }}}'

Here is the MQTT message:

{"grid_feed":0, "grid_consumption":1819, "production":0, "daily_yield":15.884, "total_yield":248.245}

Here are the MQTT sensors in my normal HA instance:

- platform: mqtt
  name: "MQTT SB Daily Yield"
  state_topic: "sma/sb"
  value_template: '{{ value_json["daily_yield"] }}'
  force_update: true
  retain: true
  unit_of_measurement: "kWh"
- platform: mqtt
  name: "MQTT SB Total Yield"
  state_topic: "sma/sb"
  value_template: '{{ value_json["total_yield"] }}'
  force_update: true
  retain: true
  unit_of_measurement: "kWh"
- platform: mqtt
  name: "MQTT SB Production"
  state_topic: "sma/sb"
  value_template: '{{ value_json["production"] }}'
  force_update: true
  retain: true
  unit_of_measurement: "W"
- platform: mqtt
  name: "MQTT SB Grid Feed"
  state_topic: "sma/sb"
  value_template: '{{ value_json["grid_feed"] }}'
  force_update: true
  retain: true
  unit_of_measurement: "W"
- platform: mqtt
  name: "MQTT SB Grid Consumption"
  state_topic: "sma/sb"
  value_template: '{{ value_json["grid_consumption"] }}'
  force_update: true
  retain: true
  unit_of_measurement: "W"

This works really well. The Docker container uses 32MB ram and does not use much processing. This may help someone else who is trying to do the same. I may want more sensors in the future and start scraping the Sunny Portal as there is no way to access data from the Home Manager 2.0 on the LAN…

For everyone trying to integrate more than one modbus device, monkey-house found a way how to do it :slight_smile:
So much cleaner than a second instance of HA!

@PtP figured this one out. Hopefully, this gets integrated into a future release of HA…

@TD22057 created a script to read the SMA energy meter and publish it on MQTT, which should then work to ingest in HomeAssistant with MQTT

I also have one of these meters and have thought of including it in pysma (the lib behind the Hass SMA integration) and then exposing it as a sensor, but not gotten so far… I already get consumption from my SB and only need 1 phase

Simple question I hope (not sure it belongs in here though), how can I remove the decimals fro the results?

Cheers in advance.

Create template sensors that look something like this:

patio_fan_load:
      friendly_name: 'Patio Fan Load'
      value_template: '{{ (states.switch.patio_fan.attributes.load_power|float)|round(1) }}'
      unit_of_measurement: 'W'

round(*) is the value you set for the number of decimals.

Hi, I’m also having issues with the SMA sensor.

I have a SunnyBoy connected to a SunnyWebbox which allows me to view my power over a web interface. I can view the stats with a webbrowser OK.

I’ve tried adding this to the sensors part of my config:

  host: 192.168.1.202
  password: !secret sma_password
  group: user
  scan_interval: 120
  sensors:
    current_power:

However once I restart HA I get the following errors on my log file:

2019-01-16 16:44:31 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/sma.py", line 125, in async_sma
    values = await sma.read(used_sensors)
  File "/usr/local/lib/python3.6/site-packages/pysma/__init__.py", line 163, in read
    yield from self.new_session()
  File "/usr/local/lib/python3.6/site-packages/pysma/__init__.py", line 134, in new_session
    body = yield from self._fetch_json(URL_LOGIN, self._new_session_data)
  File "/usr/local/lib/python3.6/site-packages/pysma/__init__.py", line 125, in _fetch_json
    return (yield from res.json()) or {}
  File "/usr/local/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 1001, in json
    headers=self.headers)
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html'

What am I doing wrong?

Possibly you have simply not copied it in here but the sensor config above does not include the platform?
I assume that this may be needed. My SunnyBoy is nicely answering with the following sensor config including platfrom:

sensor:
platform: sma
host: 192.168.2.104
password: “mysecretpassword”
scan_interval: 5
sensors:
current_consumption:
total_consumption:
current_power:
sma_daily_yield:
power_from_grid:
power_to_grid:
custom:
sma_daily_yield:
key: “6400_00262200”
unit: Wh
power_from_grid:
key: “6100_40463700”
unit: W
power_to_grid:
key: “6100_40463600”
unit: W

Hi,

i’ve a sunny boy also, and i never had error.
I didn’t have access to my config right now to show it to you, but i have made exactly what is described in the doc

something like that:

sensor:
  - platform: sma
    host: IP_ADDRESS_OF_DEVICE
    password: YOUR_SMA_PASSWORD
    sensors:
      current_consumption: [total_consumption]
      current_power:
      total_yield:

Hi JensAtGitHub,

interesting, where did-you find the other available options ?
i search something like a the total_year, total_last_year, total_day, and a total_yesterday
Actually i only use the current_power and total_yield

Hi Vincent,

I learned this somewhere here in the forum. (Can’t remember where)
Those values are partly depending on the modell type. One way to reverse engineer those sma identifiers is to log on to the web interface of the inverter and inspect the web site objects with the network-developer tools of the browser. Depending on the browser you would have different options. It is worth playing around with multiple browsers there. I found some identifies with firefox and some others with chrome depending how they are being used.

E.g. You may find them (Firefox) with developer tools enabled as follows: Highlight the value you are interested in, click right mouse and choose investigate element. If this value is using an identifier it will give you a hint in the deveolper tools window:


In this case it is the identifier for current_power 6100_40263F00.

But for my understand this does not work for each and every element since some of them are not refering to identifiers directly but trigger a script to calculate them. The method above will just give no identifier directly.

You may analyse then the different java scripts running on the actual page using the Networkanalyser tab and filter for the parameters being used in the scripts. This will give you a list of identifiers which are being used on this webpage for your specific device.


My experience is that you can use them all as custom sensor. Nevertheless none of them gives you historical data.

I am also trying currently to get historical data with a small automation since total_consumption is available. But I am new to the automation part in home assistant so I am not yet successful. There are already entries which give guidance for this. E.g:
https://community.home-assistant.io/t/how-to-calculate-daily-power-consumption-from-dsmr-sensors/37696
…but this a different topic.
If you find repective direct sma identifiers for total consumption today / yesterday this would be very interesting.I am only aware of ‘dailly_yield’ which is part of the sma component: https://www.home-assistant.io/components/sensor.sma/

1 Like

Anyone successfully integrated SunnyBoy 4000SBL in Combination with a HomeManager1.0 ?
If yes, please give my some hints, maybe relevant part from your yaml.
Many thanks

I have a Sunny Tripower 6.0, and my daily_yield is not captured. total_yield is fine.

Is there any way to capture the status at midnight from the getLogger.json file, as shown in the screenshot? I could also just log it via an automation, but would like to avoid that…
The value of the kWh at midnight for me is equal to the 017A-B2FDACB9 -> 1 -> v (=211036)

Anyone a clue on how to get this into HA?

Thanks
Cadish

I am a newbie. Trying to get SMA into my HA.
I have a Sunny Boy 2.5 1vl-40, with SMA webconnect
This is in my config:

  • platform: sma
    host: 192.168……
    password: …
    group: user
    scan_interval: 120
    sensors:
    current_consumption: [total_consumption]
    pv_power:
    total_yield:

I can see the sensors in HA
sensor.pv_power unknown unit_of_measurement: W friendly_name: pv_power

This is in the log:
Sun Feb 03 2019 13:13:57 GMT+0100 (Central European Standard Time)
Could not start session, Session ID expected [result.sid], got {‘result’: {‘sid’: None}}

Problem is I get no numbers.
Please any help?

Hi Wim.
I assume you can locallly logon to the web interface of your device with the user/pwd/ip@ combination.

I had similar problems. I could not get ‘current_power’ (unknown) but later was able to use ‘pv_power’ instead. My understanding is that not all measurements are available on all SMA devices.

I would recommend to start with only one sensor (‘current_consumption’, pv_power’, …) untl you get first readings. When changed - always precheck your configuration.yaml file until it reports no more error (e.g. on hass.io: hassio ha check). Indent’s are super critical.
Good luck it will work. Jens

Thanks for the answer.
The webinterface is working fine and I have the right ip and pwd in HA.
I will try again tomorrow, when there is sun.