Sonnenbatterie with APIv2 / Webhook


I am sharing my way of getting system data from a new (2020) sonnenBatterie 10 system.
Software is at version 1.5.5
I started with Custom Integration: Sonnenbatterie (thanks a lot to weltmeyer!), but soon discovered that there are several other ways of accessing system data in menu entry “Software-Integration”.

And the interesting part is this endpoint:

The json response can easily be parsed with rest-sensor plus a few template-sensors.

Your secrets.yaml needs an entry with the api token:

sonnen_api_token: your-token-here
  # Sonnenbatterie APIV2
  - platform: rest
    name: SonnenAPIV2
    scan_interval: 10
    resource: http://YOUR-IP-HERE:80/api/v2/status
      Auth-Token: !secret sonnen_api_token
      - Apparent_output
      - BackupBuffer
      - BatteryCharging
      - BatteryDischarging
      - Consumption_W
      - Fac
      - FlowConsumptionBattery
      - FlowConsumptionGrid
      - FlowConsumptionProduction
      - FlowGridBattery
      - FlowProductionBattery
      - FlowProductionGrid
      - GridFeedIn_W
      - IsSystemInstalled
      - OperatingMode
      - Pac_total_W
      - Production_W
      - RSOC
      - Sac1
      - Sac2
      - Sac3
      - SystemStatus
      - Timestamp
      - USOC
      - Uac
      - Ubat
      - dischargeNotAllowed
      - generator_autostart
    value_template: '{{ value_json.sonnenapiv2 | default }}'
  - platform: template
        friendly_name: 'Apparent_output'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Apparent_output"] }}'
        friendly_name: 'BackupBuffer'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["BackupBuffer"] }}'
        friendly_name: 'Consumption_W'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Consumption_W"] }}'
        unit_of_measurement: W
        device_class: power
        friendly_name: 'Fac'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Fac"] }}'
        unit_of_measurement: Hz
        friendly_name: 'GridFeedIn_W'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["GridFeedIn_W"] }}'
        unit_of_measurement: W
        device_class: power
        friendly_name: 'IsSystemInstalled'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["IsSystemInstalled"] }}'
        friendly_name: 'OperatingMode'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["OperatingMode"] }}'
        friendly_name: 'Pac_total_W'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Pac_total_W"] }}'
        unit_of_measurement: W
        device_class: power
        friendly_name: 'Production_W'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Production_W"] }}'
        unit_of_measurement: W
        device_class: power
        friendly_name: 'RSOC'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["RSOC"] }}'
        unit_of_measurement: '%'
        friendly_name: 'Sac1'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Sac1"] }}'
        friendly_name: 'Sac2'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Sac2"] }}'
        friendly_name: 'Sac3'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Sac3"] }}'
        friendly_name: 'SystemStatus'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["SystemStatus"] }}'
        friendly_name: 'Timestamp'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Timestamp"] }}'
        friendly_name: 'USOC'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["USOC"] }}'
        unit_of_measurement: '%'
        friendly_name: 'Uac'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Uac"] }}'
        unit_of_measurement: V
        device_class: voltage
        friendly_name: 'Ubat'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["Ubat"] }}'
        unit_of_measurement: V
        device_class: voltage

  - platform: template
        friendly_name: 'FlowConsumptionBattery'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["FlowConsumptionBattery"] }}'
        friendly_name: 'FlowConsumptionGrid'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["FlowConsumptionGrid"] }}'
        friendly_name: 'FlowConsumptionProduction'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["FlowConsumptionProduction"] }}'
        friendly_name: 'FlowGridBattery'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["FlowGridBattery"] }}'
        friendly_name: 'FlowProductionBattery'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["FlowProductionBattery"] }}'
        friendly_name: 'FlowProductionGrid'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["FlowProductionGrid"] }}'
        friendly_name: 'BatteryCharging'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["BatteryCharging"] }}'
        friendly_name: 'BatteryDischarging'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["BatteryDischarging"] }}'
        friendly_name: 'dischargeNotAllowed'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["dischargeNotAllowed"] }}'
        friendly_name: 'generator_autostart'
        value_template: '{{ states.sensor.sonnenapiv2.attributes["generator_autostart"] }}'

You could also setup an integration via Webhooks, for some reasons the Power Meter data is only available through this method.



Copied on github: sonnenBatterie API


My way of getting data from the Webhook API (provides some values that is not included in REST API like Power Meter).

The following action creates the webhook and sets the received value to two input_number helpers:

alias: Sonnen Webhook get power meter
description: ''
  - platform: webhook
    webhook_id: sonnen_power_meter
condition: []
  - service: input_number.set_value
      entity_id: input_number.webhook_consumption
      value: '{{ trigger.json[1][''kwh_imported''] }}'
  - service: input_number.set_value
      entity_id: input_number.webhook_production
      value: '{{ trigger.json[0][''kwh_imported''] }}'
mode: single

The input_number can be used with utility_meter like this:

    source: input_number.webhook_consumption
    cycle: daily

And finally add the webhook in sonnen-Dashboard:

Feel free to suggest a better way of doing this, just starting with Home-Assistant!

1 Like

Thank you for this information, it’s super helpful. Question, where did you find this api information that this screen shot is from?

@loralg : Glad that it helps.
You can find the api documentation on the system itself + I just pushed it to my github-repo:

On the system:
Its location is: http://YOUR-IP-HERE/api/doc.html
Default login data is: User/sonnenUser3552

Is it possible to integrate the extracted data to the energy section? Unfortunately this doesn’t work with Weltmeyers solution.

Sorry that it took a while!
I am using it with the energy management, depending on which values you want to observe you have two options:

    As all the attributes only provide power and not energy you need to add one or several integration sensors like this:
  - platform: integration
    source: sensor.sonnenapiv2_consumption_w
    name: energy_consumed
    unit_prefix: k
  - platform: integration
    source: sensor.sonnenapiv2_production_w
    name: energy_produced
    unit_prefix: k

For getting the flow from or to the battery you can use sth. like this:

  - platform: template
        friendly_name: 'Batteriespeicher Laden'
        unit_of_measurement: W
        device_class: power
        value_template: >-
          {% if states.sensor.sonnenapiv2.attributes["Pac_total_W"] | float < 0 %}
            {{ states.sensor.sonnenapiv2.attributes["Pac_total_W"] | abs }}
          {%- else -%}
          {% endif %}
        friendly_name: 'Batteriespeicher Entladen'
        unit_of_measurement: W
        device_class: power
        value_template: >-
          {% if states.sensor.sonnenapiv2.attributes["Pac_total_W"] | float >= 0 %}
          {{ states.sensor.sonnenapiv2.attributes["Pac_total_W"] }}
          {%- else -%}
          {% endif %}
  - platform: integration
    source: sensor.battery_charge_w
    name: energy_battery_charge
    unit_prefix: k
  - platform: integration
    source: sensor.battery_discharge_w
    name: energy_battery_discharge
    unit_prefix: k
  1. Webhook API
    This provides “meter” readings, so you can use them in the energy section.
    Please see my previous post:
    Sonnenbatterie with APIv2 / Webhook - #3 by julianlu
1 Like

I found this and thanks for sharing this method. Really useful.
But I’m getting this warning from core every time the resource is interrogated (I changed the name from SonnenAPIV2 to sonnen_xxxxxx).
You having the same?

2022-04-26 11:16:42 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'sonnen_xxxxxx' when rendering '{{ value_json.sonnen_xxxxxx }}'

Thank you!
I have this running in a remote location so I’m not monitoring it very closely.
But yeah, examined the logs and I am also seeing:

Template variable warning: 'dict object' has no attribute 'sonnenapiv2' when rendering '{{ value_json.sonnenapiv2 }}'

I can’t remember seeing those initially so I have currently no clue what is going wrong, maybe an update to HA?

I noticed that there was an update to the web interface so I put the recent one on github:

(Page: sonnenBatterie API )

Could you try the following? It seems to do the trick for me, no more messages:

value_template: '{{ value_json.sonnenapiv2 }}'
value_template: '{{ value_json.sonnenapiv2 | default }}'


Hi all and thanks for the hard work and that everyone is sharing there findings and work.
This might be a bit outside the scoop of the thread.

Have anyone used the API to control the battery operation status via home assistant or othere software.

Iam in the process of buying a batterie and looking at Sonnen, However due to the energy tariffs (TOU on houre) that I have.
I need a way to controle the battery when its charging, when charging (grid/solar), when to do nothing and when to try to optimize self consuming.

Any input, experiance, hints or just thoughts of the Sonnen battery/company for the future is welcome. Here or via messages
thanks in advance

I can only collect the information from the web interface, the information there indicates that this might be possible. Before buying I’d check with sonnen to be sure, but at least the endpoints for controlling are there.

It seems you’d need so switch the battery to a different operating mode, I can see the following:


  • 1: Manual charging or discharging via API


Then you have:
The discharging power of a storage system can be controlled by setting a setpoint in watts. The corresponding value of the setpoint is kept until the battery receives a new charging or discharging value.
If VPP is active, the request will be rejected.

The charging power of a storage system can be controlled by setting a setpoint in watts. The corresponding value of the setpoint is kept until the battery receives a new charging or discharging value.
If VPP is active, the request will be rejected.

VPP stands for Virtual Power Plant (Virtual Power Plants | sonnen), but as you wrote you would have a non-sonnen-tariff so the battery shouldn’t operate in that mode.

Many thanks for youre reply Julianlu
as you say and what I can conclude it looks like it should be possible. In some scenarios change the EM_operationgMode and in othere limit the discharge or charge.
Could be that it would be enough to run the battery on TOU and controle the charge/discharge rates to get the functionality needed.

Have been in contact with a reseller and via them some customer responcibele for Sonnen, sadly that did not help mutch.
Will try get in contact with somone more techinical at Sonnen.

If somone ells have tried/using thease via API it would be great if you could share yorue experinace

It seems that with the current Version on my sonnenBatterie I have Power-Meter values with the REST API. I am not sure what I can read out of these values though, since in the documentation the values are not explained.

I used postman to check for the values and it basically returns two brackets one for each direction. since the attributes have the same name how would I import this into HA to use within the Energy integration?

        "a_l1": 5.578000068664551,
        "a_l2": 5.7820000648498535,
        "a_l3": 5.793000221252441,
        "channel": 1,
        "deviceid": 4,
        "direction": "production",
        "error": 0,
        "kwh_exported": 0,
        "kwh_imported": 3157,
        "v_l1_l2": 397.5,
        "v_l1_n": 226.3000030517578,
        "v_l2_l3": 399.29998779296875,
        "v_l2_n": 231.10000610351562,
        "v_l3_l1": 390.79998779296875,
        "v_l3_n": 229.1999969482422,
        "va_total": 3924.800048828125,
        "var_total": -1351.9000244140625,
        "w_l1": 1192.5999755859375,
        "w_l2": 1262.800048828125,
        "w_l3": 1229.300048828125,
        "w_total": 3684.699951171875
        "a_l1": 1.7009999752044678,
        "a_l2": 2.306999921798706,
        "a_l3": 0.8109999895095825,
        "channel": 2,
        "deviceid": 4,
        "direction": "consumption",
        "error": 0,
        "kwh_exported": 0,
        "kwh_imported": 5752.39990234375,
        "v_l1_l2": 397.6000061035156,
        "v_l1_n": 226.3000030517578,
        "v_l2_l3": 399.29998779296875,
        "v_l2_n": 231.10000610351562,
        "v_l3_l1": 391.1000061035156,
        "v_l3_n": 229.1999969482422,
        "va_total": 1086.5999755859375,
        "var_total": -702.7000122070312,
        "w_l1": 229.8000030517578,
        "w_l2": 432.79998779296875,
        "w_l3": 166.3000030517578,
        "w_total": 828.9000244140625

Hi Julianlu

Do you know what the default charge and discharge parameters are?

I want to stop the battery discharging if there is an electric car charging but i want to reset it back to default afterwards.

I don’t know, but there is an endpoint where you can read the current one:
I’ll have a look at the system and observe whether the value is changing.
Right now, with
“FlowProductionGrid”: true
it is set to:
“SetPoint_W”: 0,

I’ll do the same and see what happens

Just for reference, here is the sensor I setup to monitor SetPoint_W:

  - platform: rest
    name: SonnenAPIV2_latest
    scan_interval: 20
    resource: http://YOUR-IP-HERE:80/api/v2/latestdata
      Auth-Token: !secret sonnen_api_token
      - SetPoint_W
    value_template: '{{ value_json.sonnenapiv2_latest | default }}'
  - platform: template
        friendly_name: 'SetPoint_W'
        value_template: '{{ states.sensor.sonnenapiv2_latest.attributes["SetPoint_W"] }}'
        unit_of_measurement: W
        device_class: power
1 Like

And here we go.
In the beginning, the battery is charging and SetPoint_W could be (Production - House consumption).
At USOC 95%, you can observe that the Setpoint is higher than the actual reported charging power (maybe a limit to slow down charge once you reach near 100%).
After that it looks like trickle charge.
How about you just disable charging once you charge your car and enable automatic mode afterwards?