Solar generated kWh vs Grid usage calculator

In the Netherlands, till the end of 2024, it’s allowed to use “the grid” as a battery.
kWh’s delivered to the grid during the summer / daytime, can be used during winter / nighttime
For each kWh delivered, you can use 1 kWh at another moment from the grid.

The calculations are done by the Energy supplier and typically are measured per 1 year.

What do I want to create:
A “calculation” of the amount of kWh delivered, minus, the amount of kWh used.
I have a sensor that gives me the actual numbers of my electricty meter.
I know the numbers on which I started mt contract.
As I have “single balancing” it makes no difference if I use /generate Tariff 1 or Tariff2 power.

Energy meter tariff 1 (used) at start of contract: 6.200 (kWh)
Energy meter tariff 2 (used) at start of contract: 10.500 (kWh)
Energy meter tariff 1 (produced) at start of contract: 3.100 (kWh)
Energy meter tariff 2 (produced) at start of contract: 5.300 (kWh)

From the sensor (that is updated every 1 minute) I get:

tariff 1 used: 8.100
tariff 2 used: 11.800
tariff 1 produced: 4.700
tariff 2 produced: 8.100

In effect I have used (till now, from the start of my contract):
Tariff1 (used): 8.100 - 6.200 = 1.900 (kWh)
Tariff 2 (used): 11.800 - 10.500 = 1.300 (kWh)
In effect I have Produced (till now since the start of my contract):
Tariff 1 (produced): 4.700 - 3.100 = 1.600 (kWh)
Tariff 2 (produced): 8.100 - 5.300 = 2.800 (kWh)

Total (tariff 1 + tariff 2) used: 1.900 + 1.300 = 3.200 (kWh)
Total (tariff 1 + tariff 2) produced: 1.600 + 2.800 = 4.400 (kWh)

my "balance"at this moment is:
Total (used) - Total (produced) 3.200 - 4.400 = -/- 1.200 (kWh)

At THIS moment I have 1.200 kWh that I can use till the end of my contract that I can Balance without paying
I have produced (and delivered to the grid) 1.200 kWh MORE than I used (till today)

I would like to make a calculation (in Node Red / Python / Script) that does this math and I want to show this entity (kWh balance) in Lovelace.

Can someone help me to realise this script?
Maybe it’s something Very obvious, but at the moment I seem to be unable to get it done

What Have I tried:
I am able to get the actual (today) values from Tariff 1 / Tariff 2 Used / Produced in Node Red
I simply don’t know (understand) how to insert the “start of contract” number and how to make the correct calculation (with a “function”??) and output that is an Entity that I can show in Lovelace (as a Gauge for example)

Thanks for your time and help. H.


Hi! I’m also very much interested in this exact solution, did you maybe in the meantime received or worked out something?
Thank you!

Hi, I have, in the meantime, solved it!
I took the two sensors from my meter (P1 port, slimmelezer) that count the current import and export for all tariffs cumulated, and then went and created a new sensor.

# Calculate Remaining Power
- platform: template
        value_template: "{{ (( states('sensor.hatasos_export_energia_a') | float) - ( states('sensor.hatasos_import_energia_a') | float) + XXXXXX) |  round(3) }}"
        unit_of_measurement: 'kWh'
        device_class: energy
        friendly_name: Net Electricity

XXXX is the balance value that you must calculate beforehand if you start running the script later than the measurement cycle starts. Addition for me, because we ended the cycle in energy “debt” which I did not want to carry forward but modify however necessary.

Put this in your config.yaml file, or if you have a separate one for sensors, there. Once you have the new sensor up and running, it’s easy to make a Lovelace card out of it.