SMA Energy Meter in Home Assistant

Working great. Thanks Chris.

Here is my code:

var count = context.get('count')||0;
count += 1;

/*
IF you do not want to update HA every second you can set the fuction to pass
the data less often - see below
*/


if (count < 1) {         /* process data only every 30 seconds */
    context.set('count',count);
    return null;
}

count = 0;
context.set('count',count);

var msg1 = { payload:"actual consumption" };
var msg2 = { payload:"actual gridfeed" };
var msg3 = { payload:"consumption counter" };
var msg4 = { payload:"gridfeed counter" };
var msg5 = { payload:"voltage" };

/* to get the actual grid consumption in W we need the offset 0.1.4.0 */
offset = msg.payload.indexOf("00010400", 0, "hex")+ 4;
var consumption = parseInt((msg.payload[offset+0]*0x1000000 + 
                            msg.payload[offset+1]*0x10000 + 
                            msg.payload[offset+2]*0x100  +
                            msg.payload[offset+3]) / 10);

/* to get the actual grid consumption counter in Wh we need the offset 0.1.8.0 */
offset = msg.payload.indexOf("00010800", 0, "hex")+ 4;
var consumption_c = parseInt((msg.payload[offset+0]*0x100000000000000 + 
                              msg.payload[offset+1]*0x1000000000000 + 
                              msg.payload[offset+2]*0x10000000000  +
                              msg.payload[offset+3]*0x100000000 +
                              msg.payload[offset+4]*0x1000000 + 
                              msg.payload[offset+5]*0x10000 + 
                              msg.payload[offset+6]*0x100  +
                              msg.payload[offset+7]) / 3600);

/* to get the actual grid feed in W we need the offset 0.2.4.0 */
offset = msg.payload.indexOf("00020400", 0, "hex")+ 4;
var gridfeed = parseInt((msg.payload[offset+0]*0x1000000 + 
                         msg.payload[offset+1]*0x10000 + 
                         msg.payload[offset+2]*0x100  +
                         msg.payload[offset+3]) / 10);

/* to get the actual grid feed counter in Wh we need the offset 0.2.8.0 */
offset = msg.payload.indexOf("00020800", 0, "hex")+ 4;
var gridfeed_c = parseInt((msg.payload[offset+0]*0x100000000000000 + 
                           msg.payload[offset+1]*0x1000000000000 + 
                           msg.payload[offset+2]*0x10000000000  +
                           msg.payload[offset+3]*0x100000000 +
                           msg.payload[offset+4]*0x1000000 + 
                           msg.payload[offset+5]*0x10000 + 
                           msg.payload[offset+6]*0x100  +
                           msg.payload[offset+7]) / 3600);

/* to get the actual Voltage of Phase1 we need the offset 0.32.4.0 note 32 = 0x20 in hex*/
offset = msg.payload.indexOf("00200400", 0, "hex")+ 4;
var voltage = parseInt((msg.payload[offset+0]*0x1000000 + 
                           msg.payload[offset+1]*0x10000 + 
                           msg.payload[offset+2]*0x100  +
                           msg.payload[offset+3]) / 1000);
msg5.payload = voltage.toString();

if (consumption >= 0 && consumption < 100000 ) {
    msg1.payload = consumption.toString();    
} else msg1 = null;

if (consumption_c >= 0 && consumption_c < 50000000 ) {
    msg2.payload = consumption_c.toString();   
} else msg2 = null;

if (gridfeed >= 0 && gridfeed < 100000 ) {
    msg3.payload = gridfeed.toString();    
} else msg3 = null;

if (gridfeed_c >= 0 && gridfeed_c < 5000000 ) {
    msg4.payload = gridfeed_c.toString();   
} else msg4 = null;

if (voltage >= 0 && voltage < 1000 ) {
    msg5.payload = voltage.toString();   
} else msg5 = null;

return [msg1, msg2, msg3, msg4, msg5 ];

Do you remember where you have found these SMA sensors number?

How did you get it work with the Home Manager 2.0 ? I have the same - and looking for a good solution.

Iā€™m doing this as per the description above. Using voltage as well as I log the grid fluctuations to show the utility company that there is a problem.
Iā€™m also using Modbus with both inverter and battery charge. Descriptions can be found on this forumā€¦

Can you send me an example of your config ? i can connect my sonny boy but the home manager 2.0 i did not get it run in HA.
it would be great!

My config is already posted here. Modbus config is only to the inverter and battery charger, not Home Manager. HM2.0 is totally useless. I wish I had not spent money on thisā€¦

when you say useless is that for usage with home-assistant? I thought I need a Home Manager for Battery usage and even without Battery to self consume the power. Or if no HM 2.0 then the SMA energy meter correct?

My understanding is that the home manager is only used, if you have compatible switches and to give you better predictions. It is not needed to use your system, ne it battery or self-consumption. It is useless for Home Assistant, as it disables WebConnect. Users are left with only ModBusa in order to access the devicesā€™ dataā€¦

so you say do it without, but would I need the the sma manager for energy self consumption and battery? I can maybe still change the order.

Not sure what you mean by ā€œenergy self consumption and batteryā€. As previously stated, you can use your self produced electricity and use a battery whitout Home Manager 2. You may want to check with your installer why he recommended this?

cheers will do, he did tell me like this.

SMA HomeManager is a real winner if you have electrical devices where solar power distribution can be controlled using the SMA communication protocol. These are batteries, wallboxes etc. where a substantial percentage of the current solar power can be used.
I use HomeManager 2.0 to control a 22kW Amtron wallbox. When the wallbox is controlled by HomeManager only solar energy is used to charge the car. This works extremely well. SMA and Mennekes (the manufacturer of the Amtron callboxes) developed the communicaton protocol together. To use this to integrate just your dish-washer is in my opinion not worth the effort.

1 Like

Hi Chris, that is what I meant with self consumption. Iā€™m looking for a SMA batterie, plus some devices with EEBus like my washing machine and later on a dryer, plus a wallbox. So then I will go with the Home Manager 2.0 and also use it to control my heatpump. That was helpful. And I like the Node-Red Thing!

well done

Would you mind sharing your code to get the values for ā€œTagesertrag, tagesverbrauchā€ and so on? Thanks :grinning:

Hello. Looking for a little help with the sma stuff. I currently have 2 SMA sunnyboy 6.0 inverters.

i was able to get one of them connected with the basic platform: sma
I can display pv power, voltage1 and voltage 2. i can not seem to get the daily yield to work

I did the inspect element on the webconnect and it doesnt seem to give me a custom code.

if i do the daily_yield. i get unknown and this in the logs

No values for sensors: daily_yield [6400_00262200]. Response from inverter: {ā€˜6100_0046C200ā€™: {ā€˜1ā€™: [{ā€˜valā€™: 1766}]}, ā€˜6100_00464800ā€™: {ā€˜1ā€™: [{ā€˜valā€™: 11856}]}, ā€˜6100_00464900ā€™: {ā€˜1ā€™: [{ā€˜valā€™: 11908}]}}

3:19:33 PM ā€“ /usr/local/lib/python3.8/site-packages/pysma/init.py (WARNING) - message first occurred at 2:59:33 PM and shows up 49 times

2nd question is there a way to have 2 inverters? i tried to do another platform:sma and the ip of the other inverter but HA complains about duplicate everything

thanks for the help

I had the same issue with the sma module as I wanted to connect to multiple SMA inverter. The only way to currently read multiple SMA devices is via the modbus package. The whole list of values you can poll you can find in the modbus or sunspec documentation.
Below as an example my configuration.yaml with four SMA inverter and modbus tcp on port 502 enabled for all of them in the inverter webconfig:

sensor: !include sensor.yaml

modbus:
# STP 10.0 configuration.yaml entry
  - type: tcp
    name: inverter_pv_stp10
    host: 192.168.7.121
    port: 502
# STP 8.0 configuration.yaml entry
  - type: tcp
    name: inverter_pv_stp8
    host: 192.168.7.124
    port: 502
# SBS 5.0 configuration.yaml entry
  - type: tcp
    name: inverter_battery_sbs5
    host: 192.168.7.123
    port: 502
# SB 4.0 configuration.yaml entry
  - type: tcp
    name: inverter_pv_sb4
    host: 192.168.7.125
    port: 502

and than add an extra file sensor.yaml for the sensor readings e.g.:

#### modbus 60 sec polling
- platform: modbus
  scan_interval: 60
  registers:
#### STP 10.0
    - name: AC Leistung STP10
      hub: inverter_pv_stp10
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30775
      register_type: holding
      count: 2

    - name: DC Leistung STP10 A
      hub: inverter_pv_stp10
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30773
      register_type: holding
      count: 2

    - name: DC Leistung STP10 B
      hub: inverter_pv_stp10
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30961
      register_type: holding
      count: 2

    - name: Netzbezug STP10
      hub: inverter_pv_stp10
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30865
      register_type: holding
      count: 2

    - name: Netzeinspeisung STP10
      hub: inverter_pv_stp10
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30867
      register_type: holding
      count: 2

#### STP 8.0
    - name: AC Leistung STP8
      hub: inverter_pv_stp8
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30775
      register_type: holding
      count: 2

    - name: DC Leistung STP8 A
      hub: inverter_pv_stp8
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30773
      register_type: holding
      count: 2

    - name: DC Leistung STP8 B
      hub: inverter_pv_stp8
      device_class: power
      unit_of_measurement: W
      data_type: int
      slave: 3
      register: 30961
      register_type: holding
      count: 2

And so on. I think this should get you the idea. SMA recommends to not poll too many registers simultaneously. Therefore some less critical values can be read less often, e.g.

#### 5min polling
- platform: modbus
  scan_interval: 300
  registers:
    - name: Innentemperatur STP10
      hub: inverter_pv_stp10
      device_class: temperature
      unit_of_measurement: Ā°C
      scale: 0.1
      precision: 1
      data_type: int
      slave: 3
      register: 34113
      register_type: holding
      count: 2
      
    - name: Innentemperatur STP8
      hub: inverter_pv_stp8
      device_class: temperature
      unit_of_measurement: Ā°C
      scale: 0.1
      precision: 1
      data_type: int
      slave: 3
      register: 34113
      register_type: holding
      count: 2

and @azrael783 , tagesertrag for some reason only works on my SB 4.0 inverter with register 30535:

    - name: Tagesertrag SB4
      hub: inverter_pv_sb4
      device_class: power
      unit_of_measurement: kWh
      scale: 0.001
      precision: 2
      data_type: uint
      slave: 3
      register: 30535
      register_type: holding
      count: 2

I installed solar this summer and have three SMA Sunnyboy inverters, this is my solution to reading and combining the individual inverters and sending to data to HA using MQTT:

Simple setup by modifying the file configuration.py, then setting up a cron job to start it once a day before sunrise.

An alternative solution is to use NodeRed and the modbus integration there to access the inverters as I described earlier in this thread

Hello all, I have a Sunny Boy 4.0 and by reading a little bit here and on other forums I managed to get a few readings from my inverter. These are: total_yield, grid_power, pv_power (gets me the same reading as grid_power) and voltage_l1. The rest of the values are ā€œunknownā€. I would like to be able to output the DC readings from the two strings of panels. I obtained the sensor keys but when I try to create the custom sensors in configuration.yaml, it doesnā€™t like the names I give them and the configuration check fails.

These are the values Iā€™m trying to output in HA. I am not sure what sensor names to give them.

You should be able to retireve those values by adding ā€œpv_power_aā€ and ā€œpv_power_bā€ to your sensor configuration (they are predefined in pysma that Home Assistant uses under the hood).