I would like to create a simple MATH on the amount of kWh produced versus my kWh used.

In the Netherlands, we can use the (solar) kWh produced (and delivered) to the grid at a later moment
Each kWh delivered to the Grid means I can use 1 kWh later.

I want to create a calculation that gives me the result of kWh delivered versus kWh used
I have a reader on my electricity meter that gives me the present (updated every minute) value of 4 numbers:

tariff 1 used
tariff 2 used
tariff 1 produced
tariff 2 produced

I know the numbers from when my (1 year) contract started.

tariff 1 used (at the start date)
tariff 2 used (at the start date)
tariff 1 produced (at the start date)
tariff 2 produced (at the start date)

I would like to do the following math:

(Tariff 1 used (now, from sensor) - Tariff 1 (at start date) ) + (Tariff 2 used (now, from sensor) - Tariff 2 (at start date) ) = USAGE

(Tariff 1 produced(now, from sensor) - Tariff 1 produced (at start date) ) + (Tariff 2 produced(now, from sensor) - Tariff 2 produced (at start date) ) = DELIVERED

Thanks for the reply…
Of course I have looked at Templates

however… I understand I can get the value of a sensor (no problem)
and I can display that sensor
or I can trigger an event when that sensor reaches a certain value

But… how do I do a MATH
I need to substract the value from the sensor, from a number (my start value of the electricity meter)
That I need to do 4 times

and then I have to get those results and do a Math (plus and minus)
to get the result…

This is not (as far as I can tell) described anywhere
Possibly you could guide me to some actual CODE that sort of does what I need

There’s a link in the Templating documentation that leads to the following page showing all of the mathematical operators available in the Jinja2 templating language:

I have managed to open the documentation on a Virtual Machine to avoid virusses
There is A LOT of information and I am certainly not planning to become a programmer
I just need a bit of Sample code on how to assign a sensor to a variable, add or subtract tjose variables and have a result that i can export as an Entuty to Home Assistant.

I really can’t imagine that this needs to be so much complex…
In Node Red I managed to get the value of the sensor and substract the “start value” in a function
However… it’s unclear how to import multiple values into another function to do the math for not just One entity, but multiple entities.

Templates seems to be another possibility, but I really have no desire to become a “programmer” just to do a simple Math of 4 values
So… I really hope that someone can show me, not just a GENERAL line, but how to get values from HA into a template

{{ states(‘sensor.energy_consumed_tariff_2’) | float(0) }}
is indeed outputting a result in numbers… but I would like to bind that to a new variable so I can use that in another math

gives me the summation of Tariff 1 and Tariff 2 used (after substracting the “start values”
So that also works.

I probabably be able to do the same for “Energy produced” and come with a number…
How can I bind that number to an entity that I can show in LoveLace…?

There’s no need to become a programmer and reading the linked documentation won’t make you into a programmer. It simply lists available mathematical operators which should be familiar to anyone with a basic education in arithmetic.

Templates allow you to do various things, like add/subtract/multiple/divide the values of entities (like your sensors).

I did, in my previous post.

Have you read the Templating documentation? If you did and you still can’t understand the template example I posted, I recommend you consider choosing software that is a better fit for you.

How can I seperate these summations ?
I would need: Tariff2 produced - 8618
and separate Tariff1 produced - 3457
and separate Tariff2 used - 9616
and seperate Tariff1 used - 15268

In this way the math is correct (the result being 2207)

Question 1:
how can I make the math in such a way that it adds and subtract the correct items first before combining the results [ (A - B) + (C-D) - (E-F) - (G-H)

Question 2…
how can I bind the final result to a new Entity
[EDIT]
I think I have understood on how to create a new Entity…
But all help is very much appreciated
[edit]

exactly how you have it written. Just use parenthesis and make things grouped like you have it. each value (A, B, C, etc) will be a state that you grab using what you wrote above. e.g. states('sensor.a') | float(0) for A and so on.

There’s examples in the docs that have been listed many many many times now.

You’re using {{ }} inside {% %}. {% %} means this code doesn’t return a value and it’s applied to the entire line. {{ }} means this code returns a value and it applies to the entire line. So each line will have at most one {% %} or {{ }} and will never have nested {% %} or {{ }}.

I strongly urge you to take the time to read some of the documentation. It’s clear you’re just clicking on links and skimming. You’ll need to get the basics downs that are covered in the link you didn’t want to click.