Help with template calculation for virtual power battery

Hi, I need some help with calculationg my virtual power battery. For some back story: in Poland I have a PV installation and my contract says that for 15 years I have a virtual battery which means that I can import 80% of the energy generated and exported to the grid for free. We also have net billing here, for which I use this integration: https://github.com/MiguelAngelLV/balance_neto

I’v made a template sensor to calculate my virtual power battery:

        device_class: energy
        friendly_name: "Energia zmagazynowana w PGE dół"
        value_template: >-
          {{ [0, ((((((states('sensor.energia_dol_eksport')|float(0))*80))/100))-(states('sensor.energia_dol_import')|float(0)) )|round (1)] | max }}
        unit_of_measurement: "kWh"

This works great and I get a reading which I can use in a gauge card:

Przechwytywanie

But when my virtual power battery is empty the gauge will show 0, but the actuall calculations will be negative, and when I get some new export to the grid it will not show unless 80% of my production in bigger than the negative values generated days before.

I want to have a calculation to reach 0 and not use any negative values, and when export starts again to add new values from 0. Like in a normal bettery system. I do not want to use the bettery info from energy card, since I will in a near future install local batteries but I do want to know the values from my virtual battery.

Can anybody pleas help with this, I am stuck and have no idea where to go from here.

You can see when the battery reaches zero on my other system, and the actual value from calculations:

Przechwytywanie

If I understand this correctly you can import 80% of the surplus energy for free, so 80% of the difference between the totally exported and the totally imported energy, when the total amount of exported energy is larger than the total amount of imported energy, right?
If so, this means that you have to take 80% of the difference between the exported and imported energy:
( ( exported – imported) * 0.8 )
, and not the difference between 80% of the exported energy and the imported energy:
( ( exported * 0.8) – imported ).
So, assuming that the two sensors are giving the total amount of exported and imported energy, I think your template sensor should be something like this (in legacy format):

sensor:
  - platform: template
    sensors:
      virtual_energy_battery:
        device_class: energy
        friendly_name: "Energia zmagazynowana w PGE dół"
        value_template: >-
          {{ [0, ( ( ( states('sensor.energia_dol_eksport') | float(0) ) - ( states('sensor.energia_dol_import') | float(0) ) ) * 0.8 ) | round(1)] | max }}
        unit_of_measurement: "kWh"

Does that make sense?

Well no, the official government documents say it has to be (exported * 0.8) - (imported), that is the way the power companies have to count.

Edit:
I’ve found an custom integration Battery sim and made a template helper for my export to grid to show only 80% of the original value. Then made a custom battery, now I will test if this works the way I would like to (it should) but there is no sun today so maybe tomorrow.

{{((((states('sensor.energia_dol_eksport')|float(0))*80))/100)|round (1)}}

hi @DominikW what solution you ended up with?

The answer is in the post above, I use battery sim integration, works great.

1 Like