Sonnenbatterie with APIv2 / Webhook

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.

I have checked with Sonnen directly and setting the Charge/Discharge manually can be reverted by setting it to Automatic.
@chrism751 can you share the node red as that sounds really good. I want to do something similar in winter when there is a low PV prediction to charge at night time rates.

@chrism751 Any chance you can paste it in preformatted text </> button?

[{"id":"65840aa926d9c567","type":"tab","label":"Battery Mode Change 3:30pm-4:30pm","disabled":false,"info":"","env":[]},{"id":"08e9b6e2e3f53a7b","type":"http request","z":"65840aa926d9c567","name":"Set TOU","method":"PUT","ret":"obj","paytoqs":"ignore","url":"http://192.168.1.**/api/v2/configurations","tls":"","persist":false,"proxy":"","authType":"bearer","senderr":false,"x":540,"y":100,"wires":[[]]},{"id":"fd585e199cd561bf","type":"function","z":"65840aa926d9c567","name":"API- TOU 10","func":"msg.payload = \"EM_OperatingMode=10\"\nmsg.headers = {}\nmsg.headers ['Auth-Token'] = ''\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":100,"wires":[["08e9b6e2e3f53a7b"]]},{"id":"1111237c7934964c","type":"debug","z":"65840aa926d9c567","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1310,"y":100,"wires":[]},{"id":"687817d1f310a567","type":"inject","z":"65840aa926d9c567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"30 15 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":100,"wires":[["fd585e199cd561bf"]]},{"id":"ad6e77e19a4338f0","type":"function","z":"65840aa926d9c567","name":"API- AUTO 2","func":"msg.payload = \"EM_OperatingMode=2\"\nmsg.headers = {}\nmsg.headers ['Auth-Token'] = ''\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":100,"wires":[["7681aa91eab215e1"]]},{"id":"7681aa91eab215e1","type":"http request","z":"65840aa926d9c567","name":"Set AUTO","method":"PUT","ret":"obj","paytoqs":"ignore","url":"http://192.168.1.**/api/v2/configurations","tls":"","persist":false,"proxy":"","authType":"bearer","senderr":false,"x":1160,"y":100,"wires":[["1111237c7934964c"]]},{"id":"ec480c16aeed8942","type":"inject","z":"65840aa926d9c567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"30 16 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":770,"y":100,"wires":[["ad6e77e19a4338f0"]]}]
1 Like

After having more of a play around, I’m now changing modes to Manual and telling it to charge for the hour period at its max rate. Cuts out having to set times in Time of Use and then ToU varying the charge rate. Also Time of Use is limited to input and output of 3.3kw, yet in Self Consumption/Automatic, it can discharge at 3.3kw while using PV power for a combined total of 4.6kw. So I would avoid using Time of Use mode if possible.

1 Like

@chrism751 did you manage to get the Curl command working, mine below does not seem to work

curl -X POST --header 'Auth-Token: xxxxxxxxx' http://192.168.86.../api/v2/setpoint/charge/5000

I got it working via node-red.

Im a complete noob when it comes to HA and I understand very little. I managed to get the following to work in Node-red to set a manual charging event.

[{"id":"e109ac634646d68d","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"9f6f12d3acbccb8b","type":"debug","z":"e109ac634646d68d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1350,"y":400,"wires":[]},{"id":"82be1d2cf87a3f4f","type":"function","z":"e109ac634646d68d","name":"API- Op Mode - Manual","func":"msg.payload = \"EM_OperatingMode=1\"\nmsg.headers = {}\nmsg.headers ['Auth-Token'] = 'xxxx'\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":400,"wires":[["25908fa01fce9869"]]},{"id":"e7c00ef16ffa20bd","type":"inject","z":"e109ac634646d68d","name":"Start 15:30","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"30 15 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":400,"wires":[["82be1d2cf87a3f4f"]]},{"id":"25908fa01fce9869","type":"http request","z":"e109ac634646d68d","name":"Set Man","method":"PUT","ret":"obj","paytoqs":"ignore","url":"http://192.168.1.48/api/v2/configurations","tls":"","persist":false,"proxy":"","authType":"bearer","senderr":false,"headers":[],"x":760,"y":400,"wires":[["d0dcbf1b5b0a057c"]]},{"id":"cab48567606ca882","type":"http request","z":"e109ac634646d68d","name":"Set Charge Value","method":"POST","ret":"obj","paytoqs":"ignore","url":"http://192.168.1.48/api/v2/setpoint/charge/4000","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"bearer","senderr":false,"headers":[],"x":1170,"y":400,"wires":[["9f6f12d3acbccb8b"]]},{"id":"d0dcbf1b5b0a057c","type":"function","z":"e109ac634646d68d","name":"API- Charge Value","func":"msg.payload = \"4000\"\nmsg.headers = {}\nmsg.headers ['Auth-Token'] = 'xxxx'\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":950,"y":400,"wires":[["cab48567606ca882"]]}]

I finally got this working via a HA button and slider for the charge/discharge rate.

[{"id":"65840aa926d9c567","type":"tab","label":"Sonnen","disabled":false,"info":"","env":[]},{"id":"f8c8624c7c9dca94","type":"http request","z":"65840aa926d9c567","name":"Set Charge","method":"POST","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"bearer","senderr":false,"headers":[],"x":670,"y":260,"wires":[["0e3f0373d7e81e09"]]},{"id":"858d53f8ca9abe53","type":"function","z":"65840aa926d9c567","name":"Charge Setup","func":"msg.headers = {}\nmsg.headers['Auth-Token'] = 'xxxxxxxxxxxxx'\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nmsg.url = \"http://192.168.1.1:80/api/v2/setpoint/charge/\" + msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":260,"wires":[["f8c8624c7c9dca94"]]},{"id":"a376688f9b317ab6","type":"api-current-state","z":"65840aa926d9c567","name":"Charge Rate","server":"250b02e2.c35e4e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.battery_charge_rate","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":290,"y":260,"wires":[["858d53f8ca9abe53"]]},{"id":"7723490d3cc48276","type":"inject","z":"65840aa926d9c567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":220,"wires":[["a376688f9b317ab6"]]},{"id":"880d464ecf976a69","type":"api-call-service","z":"65840aa926d9c567","name":"","server":"250b02e2.c35e4e","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.sonnen_charge"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":300,"y":340,"wires":[[]]},{"id":"0e3f0373d7e81e09","type":"debug","z":"65840aa926d9c567","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":260,"wires":[]},{"id":"05d69d4319a0f5d6","type":"http request","z":"65840aa926d9c567","name":"Set Discharge","method":"POST","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"bearer","senderr":false,"headers":[],"x":680,"y":460,"wires":[["0e4ea3476743d2c3"]]},{"id":"6151ac699f7381c1","type":"function","z":"65840aa926d9c567","name":"Discharge Setup","func":"msg.headers = {}\nmsg.headers['Auth-Token'] = 'xxxxxxxxxxxxx'\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nmsg.url = \"http://192.168.1.1:80/api/v2/setpoint/discharge/\" + msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":460,"wires":[["05d69d4319a0f5d6"]]},{"id":"970fcd98e7325a0f","type":"api-current-state","z":"65840aa926d9c567","name":"Discharge Rate","server":"250b02e2.c35e4e","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.battery_discharge_rate","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":300,"y":460,"wires":[["6151ac699f7381c1"]]},{"id":"fba52a65c4cd6de8","type":"inject","z":"65840aa926d9c567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":420,"wires":[["970fcd98e7325a0f"]]},{"id":"fbdf424727a2aa57","type":"api-call-service","z":"65840aa926d9c567","name":"","server":"250b02e2.c35e4e","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.sonnen_discharge"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":300,"y":540,"wires":[[]]},{"id":"0e4ea3476743d2c3","type":"debug","z":"65840aa926d9c567","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":460,"wires":[]},{"id":"2d18a92a5ddd4fd8","type":"ha-entity","z":"65840aa926d9c567","name":"Sonnen Charge","server":"250b02e2.c35e4e","version":2,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"Sonnen Charge"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:battery-plus-outline"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"","outputPayloadType":"str","x":100,"y":300,"wires":[["a376688f9b317ab6","880d464ecf976a69"],[]]},{"id":"ee18bd169efda9d5","type":"ha-entity","z":"65840aa926d9c567","name":"Sonnen Discharge","server":"250b02e2.c35e4e","version":2,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"Sonnen Discharge"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:battery-minus-outline"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"","outputPayloadType":"str","x":110,"y":500,"wires":[["970fcd98e7325a0f","fbdf424727a2aa57"],[]]},{"id":"402679171d35e0d2","type":"http request","z":"65840aa926d9c567","name":"Set State","method":"PUT","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"bearer","senderr":false,"headers":[],"x":660,"y":640,"wires":[["5bd08bcd972153bc"]]},{"id":"9efde64dbc00c864","type":"function","z":"65840aa926d9c567","name":"Manual Mode","func":"msg.payload = \"EM_OperatingMode=1\"\nmsg.headers = {}\nmsg.headers['Auth-Token'] = 'xxxxxxxxxxxxx'\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nmsg.url = \"http://192.168.1.1:80/api/v2/configurations\" \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":640,"wires":[["402679171d35e0d2"]]},{"id":"5bd08bcd972153bc","type":"debug","z":"65840aa926d9c567","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":640,"wires":[]},{"id":"5d17e76b69d0b277","type":"inject","z":"65840aa926d9c567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":600,"wires":[["9efde64dbc00c864"]]},{"id":"15891e5320025a4a","type":"ha-entity","z":"65840aa926d9c567","name":"Sonnen Manual Mode","server":"250b02e2.c35e4e","version":2,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"Sonnen Manual Mode"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:car-shift-pattern"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"","outputPayloadType":"str","x":120,"y":680,"wires":[["9efde64dbc00c864","10920422f5fa573e"],[]]},{"id":"10920422f5fa573e","type":"api-call-service","z":"65840aa926d9c567","name":"","server":"250b02e2.c35e4e","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.sonnen_manual_mode_2"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":480,"y":700,"wires":[[]]},{"id":"b66c43b2d71ebcfc","type":"http request","z":"65840aa926d9c567","name":"Set State","method":"PUT","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"bearer","senderr":false,"headers":[],"x":660,"y":800,"wires":[["1736681453a9e3de"]]},{"id":"49c07dcad21bb88c","type":"function","z":"65840aa926d9c567","name":"Automatic Mode","func":"msg.payload = \"EM_OperatingMode=2\"\nmsg.headers = {}\nmsg.headers['Auth-Token'] = 'xxxxxxxxxxxxx'\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nmsg.url = \"http://192.168.1.1:80/api/v2/configurations\" \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":800,"wires":[["b66c43b2d71ebcfc"]]},{"id":"1736681453a9e3de","type":"debug","z":"65840aa926d9c567","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":800,"wires":[]},{"id":"9875e1ef482e0d6a","type":"inject","z":"65840aa926d9c567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":780,"wires":[["49c07dcad21bb88c"]]},{"id":"d6e30b460a9ee319","type":"api-call-service","z":"65840aa926d9c567","name":"","server":"250b02e2.c35e4e","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.sonnen_automatic_mode_2"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":480,"y":880,"wires":[[]]},{"id":"d173cac1c20527d9","type":"ha-entity","z":"65840aa926d9c567","name":"Sonnen Automatic Mode","server":"250b02e2.c35e4e","version":2,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"Sonnen Automatic Mode"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:refresh-auto"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"","outputPayloadType":"str","x":130,"y":860,"wires":[["49c07dcad21bb88c","d6e30b460a9ee319"],[]]},{"id":"9ec5e985bd82391f","type":"http request","z":"65840aa926d9c567","name":"Set State","method":"PUT","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"bearer","senderr":false,"headers":[],"x":660,"y":960,"wires":[["d9e9186f881a3455"]]},{"id":"c2b655c6084fc8c4","type":"function","z":"65840aa926d9c567","name":"TOU Mode","func":"msg.payload = \"EM_OperatingMode=10\"\nmsg.headers = {}\nmsg.headers['Auth-Token'] = 'xxxxxxxxxxxxx'\nmsg.headers [\"Content-Type\"] = \"application/x-www-form-urlencoded\"\nmsg.url = \"http://192.168.1.1:80/api/v2/configurations\" \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":960,"wires":[["9ec5e985bd82391f"]]},{"id":"d9e9186f881a3455","type":"debug","z":"65840aa926d9c567","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":960,"wires":[]},{"id":"21c89c7e42792a67","type":"inject","z":"65840aa926d9c567","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":940,"wires":[["c2b655c6084fc8c4"]]},{"id":"d4d8f582e64181bc","type":"api-call-service","z":"65840aa926d9c567","name":"","server":"250b02e2.c35e4e","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.sonnen_tou_mode_2"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":480,"y":1040,"wires":[[]]},{"id":"c7d80a827356ea97","type":"ha-entity","z":"65840aa926d9c567","name":"Sonnen TOU Mode","server":"250b02e2.c35e4e","version":2,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"Sonnen TOU Mode"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:timeline-clock-outline"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"","outputPayloadType":"str","x":110,"y":1020,"wires":[["c2b655c6084fc8c4","d4d8f582e64181bc"],[]]},{"id":"250b02e2.c35e4e","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

This fixed it.
Thanks

Nice. Looks really tidy.

Hi there, sorry for the newbie question but is there some kind of tutorial available on how to set this up ?

@kevincroughs what part do you need help with? HA, Node-red or Sonnen?

I’ve been trying to fumble my way through creating a sensor so I can output the Consumption value the whole house is using. It isn’t one of the ones in the weltmeyer integration but I can see the correct output in the basic status data.
I tried entering this into my config file, but get errors in the log so something is wrong.

Or alternatively, is there an easy way to do this in node red?

sensor:
  # Sonnenbatterie APIV2
  - platform: rest
    name: SonnenAPIV2_latest
    scan_interval: 5
    resource: http://192.168.1.40:80/api/v2/latestdata
    headers:
      Auth-Token: !secret sonnen_api_token
    json_attributes:
      - Consumption_W
    value_template: '{{ value_json.sonnenapiv2_latest | default }}'
  - platform: template
    sensors:
      sonnenapiv2_latest_consumption_w:
        friendly_name: 'Consumption_W'
        value_template: '{{ states.sensor.sonnenapiv2_latest.attributes["Consumption_W"] }}'
        unit_of_measurement: W
        device_class: power

This is the error in the log file.

Logger: homeassistant.helpers.event
Source: helpers/template.py:421
First occurred: 8:23:49 AM (1 occurrences)
Last logged: 8:23:49 AM

Error while processing template: Template("{{ states.sensor.sonnenapiv2_latest.attributes[“Consumption_W”] }}")
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 419, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 1904, in _render_with_context
return template.render(**kwargs)
File “/usr/local/lib/python3.10/site-packages/jinja2/environment.py”, line 1301, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.10/site-packages/jinja2/environment.py”, line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File “”, line 1, in top-level template code
File “/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py”, line 303, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: ‘None’ has no attribute ‘attributes’

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 535, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 421, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: ‘None’ has no attribute ‘attributes’

Hi
Tried your yaml in my installation but I get the following when validating it:

Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->sonnen_powermeter_consumption->state_class. (See ?, line ?).

I changed the template section to:

  - platform: template
    sensors:
      sonnen_powermeter_consumption:
        friendly_name: "Consumption kWh"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: "total_increasing"
        state: >-
          {{ states.sensor.sonnenapiv2_powermeter_consumption.attributes["kwh_imported"] }}
        

Any inputs on how to make it work? If your example is going to work, where should I put the template section if I copy-paste it? Not in the sensors section at least :stuck_out_tongue:

Any input is appreciated! Thanks!

Hi again
Yet another observation that somebody might be able to help me with :slight_smile:
I’m connecting to the endpoint /powermeter, but I only get the first portion for consumption, not the second for production.
Output via Postman:

[
    {
        "a_l1": XX,
        "a_l2": XX,
        "a_l3": XX,
        "channel": 2,
        "deviceid": 4,
        "direction": "consumption",
        "error": 0,
        "kwh_exported": 0,
        "kwh_imported": XX,
        "v_l1_l2": XX,
        "v_l1_n": XX,
        "v_l2_l3": XX,
        "v_l2_n": XX,
        "v_l3_l1": XX,
        "v_l3_n": XX,
        "va_total": XX,
        "var_total": -XX,
        "w_l1": XX,
        "w_l2": XX,
        "w_l3": XX,
        "w_total": XX
    }
]

Might this be because my Sonnen is not producing at the time of the GET request?
Any input appreciated. Thanks!

Hi - this is how it wirks for me.

But in this I see the total production/consuption for all years. I did not figured out how to do it to see to current production/consuption for the energy dashboard

This is what i did to split the sensor and give it a value of zero.

- platform: template
  sensors:
    gridimport:
      icon_template: mdi:transmission-tower-import
      friendly_name: "Grid Import"
      device_class: power
      unit_of_measurement: "W"
      value_template: "{% if states('sensor.sonnenapiv2_gridfeedin_w')|int(default = 0) < 0 %}{{states('sensor.sonnenapiv2_gridfeedin_w')|int(default=0)|abs|default(0)}}{%else%} {{0|int}} {%endif %}"
    gridexport:
      icon_template: mdi:transmission-tower-export
      friendly_name: "Grid Export"
      device_class: power
      unit_of_measurement: "W"
      value_template: "{% if states('sensor.sonnenapiv2_gridfeedin_w')|int(default = 0) > 0 %}{{states('sensor.sonnenapiv2_gridfeedin_w')|int(default=0)|default(0)}}{%else%} {{0|int}}{%endif %}"
    batterycharge:
      icon_template: mdi:battery-charging-outline
      friendly_name: "Battery Charging"
      device_class: power
      unit_of_measurement: "W"
      value_template: "{% if states('sensor.sonnenapiv2_pac_total_w')|int(default = 0) < 0 %}{{states('sensor.sonnenapiv2_pac_total_w')|int(default=0)|abs|default(0)}}{%else%} {{0|int}}{%endif %}"
    batterydischarge:
      icon_template: mdi:battery-arrow-down
      friendly_name: "Battery Discharging"
      device_class: power
      unit_of_measurement: "W"
      value_template: "{% if states('sensor.sonnenapiv2_pac_total_w')|int(default = 0) > 0 %}{{states('sensor.sonnenapiv2_pac_total_w')|int(default=0)|default(0)}}{%else%} {{0|int}}{%endif %}"
1 Like