Sonnenbatterie with APIv2 / Webhook

The “path” leads to complex JSON data when all you want is one element. So you have to use jinja template to extract the data of interets.

Hi!

now I’m trying to set the Em_OperatingMode to manual. I can do it with this curl command:

curl -X PUT -d EM_OperatingMode=1 --header 'Auth-Token: TOKEN' http://sonnen:80/api/v2/configurations

And I get the appropriate response:

{"EM_OperatingMode":"1"}

I would like to have it as a switch though:

switch:
  - name: Sonnen battery manual mode
    platform: rest
    resource: http://sonnen:80/api/v2/configurations
    method: put
    headers:
      Auth-Token: !secret sonnen_api_token
    body_on: "EM_OperatingMode=1"
    body_off: "EM_OperatingMode=2"
    is_on_template: '{{ value_json["EM_OperatingMode"] == "1" }}'

But it doesn’t work. In the logs I only get:

ERROR (MainThread) [homeassistant.components.rest.switch] Can't turn on http://sonnen:80/api/v2/configurations. Is resource/endpoint offline?

Any clues?

I think the problem lies in how you’re specifying the body_on and body_off parameters.

In your curl command, you’re using -d EM_OperatingMode=1, which sends a JSON payload to the API. However, in your Home Assistant configuration, you’ve specified body_on: "EM_OperatingMode=1" and body_off: "EM_OperatingMode=2", without any indication that these should be treated as JSON payloads.

When you send a request with these values, it’s likely being interpreted as a query string (e.g. http://sonnen:80/api/v2/configurations?EM_OperatingMode=1) instead of a JSON payload. This is why the API is not receiving the expected data and returning an error.

To fix this, you should use the following configuration:

switch:
  - name: Sonnen battery manual mode
    platform: rest
    resource: http://sonnen:80/api/v2/configurations
    method: put
    headers:
      Auth-Token: !secret sonnen_api_token
    body_on: '{"EM_OperatingMode": "1"}'
    body_off: '{"EM_OperatingMode": "2"}'
    is_on_template: '{{ value_json["EM_OperatingMode"] == "1" }}'

By surrounding the values with double quotes and curly braces, you’re telling Home Assistant to treat them as JSON payloads.

Hi!

thanks for the answer, but unfortunately it didn’t work. I get the same log output and nothing happens in the battery. I also got this working in HTTPie (GUI) and I had to specify that the payload is Form data:

PUT /api/v2/configurations HTTP/1.1
Auth-Token: TOKEN
Content-Length: 18
Content-Type: application/x-www-form-urlencoded
Host: sonnen
User-Agent: HTTPie

EM_OperatingMode=2

So I guess we need to tell HA somehow to send the payload as form data, which, turns out you can do with a header:

switch:
  - name: Sonnen battery manual mode
    platform: rest
    resource: http://sonnen:80/api/v2/configurations
    method: put
    headers:
      Auth-Token: !secret sonnen_api_token
      Content-Type: application/x-www-form-urlencoded
    body_on: "EM_OperatingMode=1"
    body_off: "EM_OperatingMode=2"
    is_on_template: '{{ value_json["EM_OperatingMode"] == "1" }}'

This worked for me.

Thanks for the hint!!

1 Like