A simple Math?

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

USAGE - DELIVERED = BALANCE

that “BALANCE” i want to show in LoveLace

Thanks

I think you are looking for a template sensor:

Templates can be tested in the developer area

Hi,

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

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 :frowning:
Possibly you could guide me to some actual CODE that sort of does what I need :slight_smile:

Thanks

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:

https://jinja.palletsprojects.com/en/latest/templates/#math

Here’s a simple example of a template that adds the values of sensor.first and sensor.second.

{{ states('sensor.first') | float(0) + states('sensor.second') | float(0) }}

If the values of the two sensors are 10.5 and 12.75 then the result reported by the template will be 23.25.

1 Like

Thanks for pointing me in that direction. :slight_smile:
i do get a Warning:

so I am not very eager to visit that specific website :frowning: any other resources?

Thanks again

According to the GitHub repository for the Jinja project, that’s the official web site for its documentation.

Strange that I get this warning in 2 different types of browsers :frowning:

I will try to sort it out and possibly just ignore when running in a Virtual system

Thanks

I have managed to open the documentation on a Virtual Machine to avoid virusses :slight_smile:
There is A LOT of information and I am certainly not planning to become a programmer :slight_smile:
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 :slight_smile:
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 :slight_smile:

Thanks

We would need to see whatever errors you are getting when the template is evaluated, so we can help you.

Try your template in the templates section of the developer tools within Home Assistant, paste a screenshot here.

123 wrote

states('sensor.first') | float(0)

You wrote

states(sensor.energy_consumed_tariff_2) | float(0)

Can you spot the differences?

1 Like

Hi…

Yes and Thanks… I have found the difference
I do get a value :slight_smile:

{{ states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616 }}

this gives the value: 1268.7970000000005

That represents the difference between the value of Tariff2 consumed Now and the Start value
So… that is (and was) running :slight_smile:

{{ states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616 + states(‘sensor.energy_consumed_tariff_1’) | float(0) - 15268 }}

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…?

Thanks

1 Like

By making a template sensor.

@der-optimist linked the documents for that in his last post to you.

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.

I am testing in the Developer Tools :slight_smile:

I have tested:

{{ states(‘sensor.energy_produced_tariff_2’) | float(0) - 8618 + states(‘sensor.energy_produced_tariff_1’) | float(0) - 3457 - states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616 - states(‘sensor.energy_consumed_tariff_1’) | float(0) - 15268 }}

which of course gives the wrong value…

In EXCEL I would put () around each part
beacuse I would need to seperate the different parts to get the correct result:

{{ states(‘sensor.energy_produced_tariff_2’) | float(0) - 8618 +

states(‘sensor.energy_produced_tariff_1’) | float(0) - 3457 -

states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616 -

states(‘sensor.energy_consumed_tariff_1’) | float(0) - 15268 }}

It can’t be executed as one summation :slight_smile:

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 :slight_smile:
[EDIT]
I think I have understood on how to create a new Entity…
But all help is very much appreciated :slight_smile:
[edit]

Sorry I am a total newbie

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.

THANKS…

{{ (states(‘sensor.energy_produced_tariff_2’) | float(0) - 8618) + (states(‘sensor.energy_produced_tariff_1’) | float(0) - 3457) - (states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616) - (states(‘sensor.energy_consumed_tariff_1’) | float(0) - 15268) }}

works perfectly :slight_smile:

Thanks for the help.
It’s complex for someone who has no idea and might seem very stupid for someone that is working with templates every day…

I am happy I have found (a part of) the solution, thanks to you guys :slight_smile:
Thanks… will continue with the Entity integration

template integration which uses yaml, not entity integration (which doesn’t exist). You’re confusing jinja code with the template integration.

read this:

I have tried to follow the sample and also tried diffrent variations but probably I am just stupid :frowning:

2022-11-10 19:37:45.180 ERROR (MainThread) [homeassistant.config] Invalid config for [template]: invalid template (TemplateSyntaxError: expected token ‘:’, got ‘}’) for dictionary value @ data[‘sensor’][0][‘state’]. Got “{% set sum = {{(states(‘sensor.energy_produced_tariff_2’) | float(0) - 8618) + (states(‘sensor.energy_produced_tariff_1’) | float(0) - 3457) - (states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616) - (states(‘sensor.energy_consumed_tariff_1’) | float(0) - 15268)}} | float %} {{ (sum | round(1, default=0) }}\n”. (See /config/configuration.yaml, line 22).

is the error I am getting

Have tried line 27 With and without {{ }} around the calculation that proved to work in Templates.

{% set sum = {{(states(‘sensor.energy_produced_tariff_2’) | float(0) - 8618) + (states(‘sensor.energy_produced_tariff_1’) | float(0) - 3457) - (states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616) - (states(‘sensor.energy_consumed_tariff_1’) | float(0) - 15268)}} | float %}

and:

{% set sum = (states(‘sensor.energy_produced_tariff_2’) | float(0) - 8618) + (states(‘sensor.energy_produced_tariff_1’) | float(0) - 3457) - (states(‘sensor.energy_consumed_tariff_2’) | float(0) - 9616) - (states(‘sensor.energy_consumed_tariff_1’) | float(0) - 15268) | float %}

both give an error
Sorry to ask for more help…

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.

The one with the {{ … }} inside the {% … %} can not work - the {{ do not belong there.

In the second line, there’s a

(

too much, the one before sum. Change it to

{{ sum | round(1, default=0) }}

I think then it should run