Custom Integration: Sonnenbatterie

Sorry you lost me. Not sure what you mean. Can you give some example of what you are trying to do? Your trying to manage a solaredge battery but it’s managed by sonnen?

You can manipulate entities and creat new entities by writing jinja templates and storing them in the configuration.yaml file.

A simple example below calculates the battery percentage from the available voltage in a home made weather station.

This is out of my configuration.yaml file and is an entry under the sensor: section.

# calculate battery percentage for weather stations from voltage level reading
  - platform: template
    sensors:
      w_station_bat_percent:
        value_template: >-
          {% set  volts = states.sensor.w_station_battery.state|float(0) %}
          {{ ((volts - 2.95) * 100 / 1.16)|float(0) }}

You can experiment with these templates in the Developer Tools section of Home Assistant.

Ive been trying to make this sensor stuff work but im not sure what ive done wronng

This is my code im trying to get to configure with all my 3 batteries together
#sensor Battery total
sensors:

  • platform: template
    sensors:
    Total Battery Percent
    value_template:"{{ (states(‘sensor.sonnenbatterie_56495_state_charge_user’) | float(0) + states(‘sensor.solaredge_b1_state_of_energy’) | float(0) + states(‘sensor.solaredge_b2_state_of_energy’) | float(0) - states(‘V’) | float(0)) / 3 }}"
    unit_of_measurement: “%”
    icon_template: mid:battery

It keeps throwing this error message. i have all green ticks and followed some guides but ive hit a wall.

Can i have help?

Is this what you are trying to do?

(Sonnen + Solaredgeb1 + Solaredgeb2) / 3

If so something like this:

sensors:
# Calculate total battery percentage 
  - platform: template
    sensors:
      total_battery_percent:
        unique_id: total_battery_percent
        friendly_name: "Total Battery Percentage"
        unit_of_measurement: "%"
        icon_template: mdi:battery
        value_template:>-
          {% set sonnencharge = states.sensor.sonnenbatterie_56495_state_charge_user|float(0) %}
          {% set solaredgeb1  = states.sensor.solaredge_b1_state_of_energy|float(0) %}
          {% set solaredgeb2  = states.sensor.solaredge_b2_state_of_energy|float(0) %}
          {{ ((sonnencharge + solaredgeb1 + solaredgeb2) / 3)|float(0)) }}

Note the first sensor: positioned in column one should be the only sensor heading positioned in column one. All new sensor templates you create come under this heading.

Also note the indentation.

This didnt work for me even when i tried in the template editor

sensors:

Calculate total battery percentage

  • platform: template
    sensors:
    total_battery_percent:
    unique_id: total_battery_percent
    friendly_name: “Total Battery Percentage”
    unit_of_measurement: “%”
    icon_template: mdi:battery
    value_template:>-
    {% set sonnencharge = states(‘sensor.sonnenbatterie_56495_state_charge_user’) | float(0) %}
    {% set solaredgeb1 = states(‘sensor.solaredge_b1_state_of_energy’)|float(0) %}
    {% set solaredgeb2 = states(‘sensor.solaredge_b2_state_of_energy’)|float(0) %}
    {{ ((sonnencharge + solaredgeb1 + solaredgeb2) / 3)|float(0) }}

i found this works in the template tester but fails everytime when i deploy it in sesors.yaml file.

Take the template section and past that into the template editor and play with it. I can’t test it here as I don’t have your sensors. You probably need to add .state at the end of the statement so something like.

{% set sonnencharge = states.sensor.sonnenbatterie_56495_state_charge_user.state | float(0) %}
{% set solaredgeb1 = states.sensor.solaredge_b1_state_of_energy.state|float(0) %}
{% set solaredgeb2 = states.sensor.solaredge_b2_state_of_energy.state|float(0) %}
{{ ((sonnencharge + solaredgeb1 + solaredgeb2) / 3)|float(0) }}

If I print the sensor I have like this:

{{ states.sensor.sonnenbatterie_84324_state_charge_user }}

You get this:

<template TemplateState(<state sensor.sonnenbatterie_84324_state_charge_user=10; state_class=measurement, unit_of_measurement=%, device_class=battery, friendly_name=Charge percentage (user) @ 2023-08-07T21:41:28.511544+10:00>)>

If you print it like this:

{{ states.sensor.sonnenbatterie_84324_state_charge_user.state }}

with .state at the end, you get this:

10

Just the state of the sensor which is currently 10%

Oh the issue with this particulare template is an extra bracket


But it also needs .state at the end of the statements so try this:

sensors:
# Calculate total battery percentage 
  - platform: template
    sensors:
      total_battery_percent:
        unique_id: total_battery_percent
        friendly_name: "Total Battery Percentage"
        unit_of_measurement: "%"
        icon_template: mdi:battery
        value_template:>-
          {% set sonnencharge = states.sensor.sonnenbatterie_56495_state_charge_user.state|float(0) %}
          {% set solaredgeb1  = states.sensor.solaredge_b1_state_of_energy.state|float(0) %}
          {% set solaredgeb2  = states.sensor.solaredge_b2_state_of_energy.state|float(0) %}
          {{ ((sonnencharge + solaredgeb1 + solaredgeb2) / 3)|float(0) }}

or

sensors:
# Calculate total battery percentage 
  - platform: template
    sensors:
      total_battery_percent:
        unique_id: total_battery_percent
        friendly_name: "Total Battery Percentage"
        unit_of_measurement: "%"
        icon_template: mdi:battery
        value_template:>-
          {% set sonnencharge = states('sensor.sonnenbatterie_56495_state_charge_user')|float(0) %}
          {% set solaredgeb1  = states('sensor.solaredge_b1_state_of_energy')|float(0) %}
          {% set solaredgeb2  = states('sensor.solaredge_b2_state_of_energy')|float(0) %}
          {{ ((sonnencharge + solaredgeb1 + solaredgeb2) / 3)|float(0) }}

This all works in the template, but when i deploy it, there is errors constantly

it works fine in the template tester. this is the odd thing.

Pass the code through this site to validate. https://jsonformatter.org/yaml-formatter

Actually I think what the problem is you’ve included two sensors: headings. The first is in configuration.yaml where you have:

sensors: !include sensor.yaml

and then you’ve started sensor.yaml with another sensors: heading. You can only have one sensors: heading in column one.

Thanks a lot! That is really helpful. But i have one question. Which sensor is intended for witch positon in the enery dashboard? Sorry for that question. Battery is clear, but the rest?

@TomOene if you are trying to set up weltmeyer’s custom integration to feed data into the HA energy dashboard you have to calculate the energy in kWh from these sensor which are in kW.

The difference is that the battery is reporting on instantanious power in kW comming and going through the system while the energy dashboard is looking for energy not power and energy is the sum of these power flows over time (h or hours) giving you kWh.

So you have to use a feature of Home Assistant that can keep track of a sensor over time and store the chages over time (AKA area under the curve). Just to confuse things this platform is called an integration and can be set up ether in your configuration.yaml file or in the ‘Devices & Services’ ‘helper’ screen.

I have this set up in the yaml file but its probably best to do it in the helper system as thats the new way to set these things up. But I’ll show you what I have in YAML anyway just for clarity.

This is what I have in yaml (note the XXXXX should be your battery serial numer):

  # Convert power to energy for energy dashboard using integration function with method left
  - platform: integration
    source: sensor.sonnenbatterie_XXXXX_meter_consumption_4_2_w_total
    name: PowerConsumption
    round: 3
    unit_prefix: k
    unit_time: h
    method: left

  - platform: integration
    source: sensor.sonnenbatterie_XXXXX_meter_production_4_1_w_total
    name: PowerProduction
    round: 3
    unit_prefix: k
    unit_time: h
    method: left

  - platform: integration
    source: sensor.sonnenbatterie_XXXXX_state_grid_input
    name: GridInput
    round: 3
    unit_prefix: k
    unit_time: h
    method: left

  - platform: integration
    source: sensor.sonnenbatterie_XXXXX_state_grid_output
    name: GridOutput
    round: 3
    unit_prefix: k
    unit_time: h
    method: left

  - platform: integration
    source: sensor.sonnenbatterie_XXXXX_state_battery_input
    name: BatteryInput
    round: 3
    unit_prefix: k
    unit_time: h
    method: left

  - platform: integration
    source: sensor.sonnenbatterie_XXXXX_state_battery_output
    name: BatteryOutput
    round: 3
    unit_prefix: k
    unit_time: h
    method: left

This is just to show you how it’s done manually.

But best set it up in helper.

Go to Settings then Devices & Services then select Helpers from the top menu.
Then create each of the 6 integrations I’ve listed above by selecting “+ CREATE HELPER”
For each of the 6 select “Integration - Riemann sum integral” something like this:

Then use these integrations in setting up the energy dashboard:

Has anyone experianced the entities freezing?

Mine get stuck and i require to do a full reset on the system.

Other intergrations you can just reset that individual intergration but this one doesnt have that. Unless u can be shown a template to reboot it?

Yes, I have also started experiencing this the latest week/weeks. Maybe we need to open an issue?

Opened an issue, maybe you can also provide som information in the issue:

Entities intermittently freeze and get unavailable · Issue #39 · weltmeyer/ha_sonnenbatterie (github.com)

Yer sent it. hopefully we get a result

In case this helps anyone my ‘User’ password was on the side of my Sonnen Core system

Thank you for a fantastically useful integration!