Calculate kWh

I am a newbie to Node Red so the solution might be very simple.
I would like to calculate the amount of kWh I produced, versus what I have used.

In the Netherlands we can use the same amount of kWh we have produced during the summer (with solar panels), in the winter.

I have a sensor that reads the amount kWh (every second) used (tariff 1 and tariff 2)
I have a sensor that reads the amount of kWh produced (also tariff 1 and tariff 2)
I know the numbers of Tariff 1 and 2, produced and used, when I started the contract with my Energy supplier.

I would like to do the “math” (single balancing) of how many kWh I still have “over produced” or how many kWh I have used (compared to what I have produced)

Sample:

Tariff1 used now: 8.300
Tariff1 produced now: 6.200
Tariff1 used at the start of my contract: 6.700
Tariff1 produced at the start of my contract: 3.900

I have used: 8.300 - 6.700 = 1.600 kWh
I have produced: 6.700 - 3.900 = 2.800 kWh

I can still use: 2.800 - 1.600 = 1.200 kWh before I have to pay

How can I create a “template” / Automation that can do this and show it in Lovelace

Thanks