Sonnenbatterie with APIv2 / Webhook

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

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:

  1. REST API
    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
    sensors:
      battery_charge_w:
        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 -%}
            0
          {% endif %}
      battery_discharge_w:
        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 -%}
            0
          {% 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:

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

Reference:

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:
image

/api/v2/status
Has:
OperatingMode

  • 1: Manual charging or discharging via API

/api/v2/configurations
Has
EM_OperatingMode

Then you have:
/api/v2/setpoint/discharge/{watt}
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.

/api/v2/setpoint/charge/{watt}
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:
https://jlunz.github.io/homeassistant/#/api/getApiV2Latestdata
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
    headers:
      Auth-Token: !secret sonnen_api_token
    json_attributes:
      - SetPoint_W
    value_template: '{{ value_json.sonnenapiv2_latest | default }}'
  - platform: template
    sensors:
      sonnenapiv2_latest_setpoint_w:
        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?

Hi Julianlu

Yea that is the plan. No rush on this as i don’t have the car yet :slight_smile: . Just want to built the ability and system set up. I am still waiting to hear back from Sonnen.

David

You need to use the RESTful Sensor - Home Assistant to select either the production or consumption subset.
Then pair it with a template sensor and you should be able to select it in the energy dashboard.

The following config should get you started:

  # Sonnenbatterie APIV2 - Powermeter production
  - platform: rest
    name: SonnenAPIV2_Powermeter_Production
    scan_interval: 20
    resource: http://YOUR-IP-HERE:80/api/v2/powermeter
    headers:
      Auth-Token: !secret sonnen_api_token
    json_attributes_path: "$.[0]"
    json_attributes:
      - direction
      - kwh_imported
    value_template: '{{ value_json.sonnenapiv2_powermeter_production | default }}'
    
  # Sonnenbatterie APIV2 - Powermeter consumption
  - platform: rest
    name: SonnenAPIV2_Powermeter_Consumption
    scan_interval: 20
    resource: http://YOUR-IP-HERE:80/api/v2/powermeter
    headers:
      Auth-Token: !secret sonnen_api_token
    json_attributes_path: "$.[1]"
    json_attributes:
      - direction
      - kwh_imported
    value_template: '{{ value_json.sonnenapiv2_powermeter_consumption | default }}'

template:
  - sensor:
      - name: 'Consumption kWh'
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >-
          {{ states.sensor.sonnenapiv2_powermeter_consumption.attributes["kwh_imported"] }}
  - sensor:
      - name: 'Production kWh'
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >-
          {{ states.sensor.sonnenapiv2_powermeter_production.attributes["kwh_imported"] }}
2 Likes

Thanks for sharing all the information. I installed the repo but I don’t know, if I had to add further configurations, or if the webhooks are already integreted in this version. I add the Riemann-Sum for the charging and discharging to add it into the energy dashboard. What can and need I do more?

I’m using Node Red to change the operating mode from Automatic to Time of use to charge the battery in off peak hours and take advantage of a free hour of power that the electric company I use gives us everyday.