# Engergy "left" calculator

I would like to build a calculation to how many kWh I have “left”

In the Netherlands we can use as much power from the grid as we have fed back to the grid in kWh.
If during the summer I produce 4000 kWh that flows back to the grid, I can use 4000 kWh at other times.

I know the meter reading for the 4 sensors when my contract with the energy supplier started.
Power consumption tariff 1
Power consumption tariff 2
Power production tariff 1
Power production tariff 2

I have a DSMR meter that show me the actual readings (every second)

Sample:
my consumption tariff 2 now is: 8.400
the reading when I started the contract was: 6.500
That means I have used 8.400 - 6.500 = 1900 kWh

My power production tariff 2 is now 4.400
my power production tariff 2 when I started the contract was 2.200
That means I have produced: 4.400 - 2.200 = 2.200 kWh

Because I have produced: 2.200
and I have used: 1.900
I still have: 2.200 - 1.900 = 300 kWh in my “wallet” that I can use from the grid without paying.

I would like to make that calculation for both Tariff 1 and Tariff 2 (as I have single balancing) and see how many kWh i still “have” or how many kWh I have used more then produced.

How can I make such a calculation in HA.
I do have the sensor names:

sensor.energy_consumed_tariff_1
sensor.energy_consumed_tariff_2
sensor.energy_produced_tariff_1
sensor.energy_produced_tariff_2

I know the values when the contract started.
But I guess I should give those an entity manually, so I can change them each year when the new contract starts?