Sonnen Battery Binding

Download

Sonnen binding

The binding for sonnen communicates with a sonnen battery. More information about the sonnen battery can be found here: https://sonnen.de/
In general the sonne API can be also read with the standard http binding. However I decided to create this binding to make it easier for normal user to integrate it in their openhab instance.

The following channels are yet supported:

Channel Type Access Description
batteryChargingState Switch read Indicates if the Battery is charging at that moment
batteryCharging Number:Energy read Indicates the actual current charging the Battery. Otherwise 0.
batteryDischargingState Switch read Indicates if the Battery is discharging at that moment
batteryDischarging Number:Energy read Indicates the actual current discharging the Battery. Otherwise 0.
batteryFeedIn Number:Energy read Indicates the actual charging current of the Battery in watt
batteryDischarging Number:Energy read Indicates the actual current discharging the Battery in watt
consumption Number:Energy read Indicates the actual consumption of the consumer in watt
gridFeedIn Number:Energy read Indicates the actual current feeding to the Grid in watt.0 if nothing is feeded
gridConsumption Number:Energy read Indicates the actual current consumption from the Grid in watt.0 if nothing is received
solarProduction Number:Energy read Indicates the actual production of the Solar system in watt
batteryLevel Number read Indicates the actual Battery Level in % from 0 - 100
flowConsumptionBatteryState Switch read Indicates if there is a current flow from Battery towards Consumption
flowConsumptionGridState Switch read Indicates if there is a current flow from Grid towards Consumption
flowConsumptionProductionState Switch read Indicates if there is a current flow from Solar Production towards Consumption
flowGridBatteryState Switch read Indicates if there is a current flow from Grid towards Battery
flowProductionBatteryState Switch read Indicates if there is a current flow from Production towards Battery
energyImportedStateProduction Number:Energy read Indicates the imported kWh Production
energyExportedStateProduction Number:Energy read Indicates the exported kWh Production
energyImportedStateConsumption Number:Energy read Indicates the imported kWh Consumption
energyExportedStateConsumption Number:Energy read Indicates the exported kWh Consumption

Changelog

Version 0.1

  • initial release

Version 0.2

  • Refactoring code
  • Adding new Channels batteryFeedIn, batteryDispense and gridReceive on user request. gridFeedIn shows now only positive values.

Version 1.0

  • Refactoring code
  • Changing Channel descriptions
  • Same Code basis as merged to the next offical release of openhab

Version 1.1 (added 12.03.22) - Update of V1.1 on 07.04.23

  • Added support for sonnen API V2
  • Added two channels for PowerMeter

Resources

Latest Release JAR:
Latest JAR-File V1.1

Source Code is merged here (pull request created):
Github repository

Author

This binding was developed by @Christian_Feininger .

3 Likes

Will integrate it as soon as my Sonnen battery will go live

1 Like

Love this Binding. The first usage went fine.

I used two items for channel js-transformations to get everytime positive numbers of Grid activities (FeedIn / Receive) - cut at 0. You may think to add two additional channels to automate it.

I would like to request a channel for Battery-Activity (FeedIn/Usage) [ -/+ nnnn W]. By then my tracking would be complete.

I published V0.2 which incorporate your requested changes. Have a look

Thnx. Going to investigate. I’ve updated the binding via Client mode.

Not sure, what went wrong, but the old thing reports now HANDLER_MISSING_ERROR
A new created thin is showing correct functionality.
However, I have to recreate all items… based on new channels.

Not sure, by when this is finished.

Furthermore I recognized (beside to your binding), tha I see troubles in the sonnen-API (is also not working in the SonnenAPP (Android) on my end - the live view is not showing the correct values). However the Summary vew (second tab) shows the “correct” graphs based on other values. Do you face a similar issue? Started ~ by 3rd JAN.

This binding is now part of official openHAB distribution, starting at snapshot 2686.

This beta version will have to be unpublished from the marketplace when openHAB 3.3M1 is released.

Entry will be removed after the next milestone from openhab is published

@felso HANDLER_MISSING_ERROR is normal as you have removed the old JAR when you installed the new version. This will happen now again if you migrate now to the latest and final version 1.0.

Besides that, my values of the binding matching with the Live View. However you have to distinguish that the Live View over the webpage from sonnen is always some seconds behind then maybe the binding as the binding is taking the local number directly from your battery and the for the sonnen portal the numbers are getting processed over the cloud and not have a constant refresh.

Thanks for your efforts and keeping me up to date. Today Sonnen solved my problem regarding the live view and from this moment I receice also data from your binding.

However: it was now the second time that the defined thing went into a communication error:
2022-01-07 16:01:35.572 [WARN ] [.MqttBrokerConnectionServiceInstance] - MqttBroker connection configuration faulty: ID segment ‘ddd.ddd.ddd.ddd’ contains invalid characters. Each segment of the ID must match the pattern [\w-]*.
at org.eclipse.jetty.server.HttpConnection.onClose(HttpConnection.java:519) ~[bundleFile:9.4.43.v20210629]
at org.eclipse.jetty.io.ssl.SslConnection.onClose(SslConnection.java:378) ~[bundleFile:9.4.43.v20210629]
at org.eclipse.jetty.io.SelectorManager.connectionClosed(SelectorManager.java:345) ~[bundleFile:9.4.43.v20210629]
at org.eclipse.jetty.server.HttpConnection.onClose(HttpConnection.java:519) ~[bundleFile:9.4.43.v20210629]
at org.eclipse.jetty.io.ssl.SslConnection.onClose(SslConnection.java:378) ~[bundleFile:9.4.43.v20210629]
at org.eclipse.jetty.io.SelectorManager.connectionClosed(SelectorManager.java:345) ~[bundleFile:9.4.43.v20210629]
2022-01-10 23:17:48.746 [WARN ] [b.binding.mqtt.handler.BrokerHandler] - Trying to dispose handler mqtt:broker:c59cc558d5 but connection is already null. Most likely this is a bug.
2022-01-10 23:17:48.747 [WARN ] [g.mqtt.handler.AbstractBrokerHandler] - Trying to dispose handler mqtt:broker:c59cc558d5 but connection is already null. Most likely this is a bug.
at org.openhab.binding.sonnen.communication.SonnenJSONCommunication.refreshBatteryConnection(SonnenJSONCommunication.java:65) ~[?:?]
at org.openhab.binding.sonnen.internal.SonnenHandler.updatebatteryData(SonnenHandler.java:110) ~[?:?]
at org.openhab.binding.sonnen.internal.SonnenHandler.refreshChannels(SonnenHandler.java:165) ~[?:?]
at org.openhab.binding.sonnen.communication.SonnenJSONCommunication.refreshBatteryConnection(SonnenJSONCommunication.java:65) ~[?:?]
at org.openhab.binding.sonnen.internal.SonnenHandler.updatebatteryData(SonnenHandler.java:110) ~[?:?]
at org.openhab.binding.sonnen.internal.SonnenHandler.refreshChannels(SonnenHandler.java:165) ~[?:?]

I replaced my local IP address. After pausing and resuming the binding went to ONLINE again. Not sure if you are able to think about an automatic reconnect feature - if not already given.

3.3M1 has been released.
Time to unpublish this beta version from the marketplace.

Works great for me

Sorry for highjacking this thread a bit. As Sonnen fully swapped to API v2 with version 1.7.7 my powermeter (http://system-ip/api/v2/powermeter) isn’t accessable anymore.

How can i integrate the Auth-Token to my things file?

Currently the “Status” runs like the following, but here the Auth-Token is not necessary.

Thing http:url:sonnen “http SonnenBatterie” [ baseURL=“http://SYSTEM-IP/api/v2/status”, refresh=5] {
Channels:
Type number : Verbrauch “Verbrauch” [ stateTransformation=“JSONPATH:$.Consumption_W” ]

What powermeter do you use? I am not aware of it. However the binding was designed to get the data from the sonnen battery in read only. Therefore no authentication is required and still works as expected. The binding does not have an build in authentication.

Hi Christian,
To clarify, in addition to the binding I was calculating the daily consumption of the building with the http Binding and using the powermeter API.
http://system-ip/api/v2/powermeter

But with API v2 Sonnen started to add a Auth-Token and now I do not know to integrate this Auth-Token to the http Binding Thing.

Ok i see. I will look into it as soon as I have time and add it to the binding. It shouldn’t be to complicated.

Cool, sounds great! Thanks in advance! :wink:

Hello @Heeman2424, I almost implemented then API as well as the PowerMeter. The PowerMeter itself returns a lot of different values for production and consumption. Which one do you use meaning which ones should the binding have?

I get the following returned:

[
    {
        "a_l1": 0.11699999868869781,
        "a_l2": 0,
        "a_l3": 0,
        "channel": 1,
        "deviceid": 4,
        "direction": "production",
        "error": 0,
        "kwh_exported": 0,
        "kwh_imported": 21750.30078125,
        "v_l1_l2": 407.8999938964844,
        "v_l1_n": 233.3000030517578,
        "v_l2_l3": 411.6000061035156,
        "v_l2_n": 236.10000610351562,
        "v_l3_l1": 409.3999938964844,
        "v_l3_n": 235.39999389648438,
        "va_total": 27.100000381469727,
        "var_total": 25.5,
        "w_l1": -9.399999618530273,
        "w_l2": 0,
        "w_l3": 0,
        "w_total": -9.399999618530273
    },
    {
        "a_l1": 4.7170000076293945,
        "a_l2": 4.131999969482422,
        "a_l3": 3.515000104904175,
        "channel": 2,
        "deviceid": 4,
        "direction": "consumption",
        "error": 0,
        "kwh_exported": 0,
        "kwh_imported": 19014,
        "v_l1_l2": 407.8999938964844,
        "v_l1_n": 233.3000030517578,
        "v_l2_l3": 411.6000061035156,
        "v_l2_n": 236.10000610351562,
        "v_l3_l1": 409.3999938964844,
        "v_l3_n": 235.5,
        "va_total": 2889.89990234375,
        "var_total": 1195.300048828125,
        "w_l1": 1049.5,
        "w_l2": 863.7000122070312,
        "w_l3": 718,
        "w_total": 2631.199951171875
    }
]

Furthermore, do you have a description for all the values?

Hi Christian,
Great to hear from you.
Currently I‘m getting the value with the http Binding.

From the powermeter API I‘m using:

Type number : Tagesverbrauch "Tagesverbrauch" [ stateTransformation="JSONPATH:$.[1].kwh_imported" ]

This should be the „second“ kWh imported (19014) to calculate the daily consumption, starting every day at midnight.

From the configuration API I‘m using:

Type string : Software "Software" [ stateTransformation="JSONPATH:$.DE_Software" ]

to get notifications when the Software gets updated.

And from the status API I get the following values:

Type number : Verbrauch "Verbrauch" [ stateTransformation="JSONPATH:$.Consumption_W" ]
	Type number : Ladestand "Batteriestatus" [ stateTransformation="JSONPATH:$.USOC" ]
	Type number : Einspeisung "Einspeisung" [ stateTransformation="JSONPATH:$.GridFeedIn_W" ]
	Type number : Ladeleistung "Ladeleistung" [ stateTransformation="JSONPATH:$.Pac_total_W" ]
	Type number : Erzeugung "Erzeugung" [ stateTransformation="JSONPATH:$.Production_W" ]

Unfortunately I don’t have a description of all the values.

Hi I am trying to extract data from the Sonnen API powermeter.
Below is my setup but its not working, I can view the data from the cml.
Would you please compare my setup to yours so I can see what I have done wrong?
UID: http:url:SonnenPowermeter
label: HTTP URL Thing
thingTypeUID: http:url
configuration:
headers:
- Auth-Token=“6bef9e84-ea7a-40be-a441-dba93cccc779”
ignoreSSLErrors: false
stateMethod: GET
refresh: 30
commandMethod: GET
timeout: 3000
authMode: BASIC
baseURL: http://192.168.1.22:80/api/v2/powermeter
password: xxx
delay: 0
contentType: text/html
bufferSize: 2048
username: john
channels:

  • id: PowerMeterI1
    channelTypeUID: http:number
    label: PM1
    description: “”
    configuration:
    stateTransformation: JSONPATH:$.[1].kwh_imported

Hello,

powermeter is not supported yet. I will update the binding soon to support it.