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.
What just came into my mind: For making such a template better readable in future (imagine you don’t touch it for 3 years and then you have to change something), I like to use variables in my templates. For example like this:
{% set tariff_1_used_startvalue = 8618 %}
{% set tariff_2_used_startvalue = 3457 %}
{% set tariff_1_used_currentvalue = states('sensor.energy_produced_tariff_1') | float(0)%}
{% set tariff_2_used_currentvalue = states('sensor.energy_produced_tariff_2') | float(0)%}
{% set usage_tariff_1 = tariff_1_used_currentvalue - tariff_1_used_startvalue %}
{% set usage_tariff_2 = tariff_2_used_currentvalue - tariff_2_used_startvalue %}
{% set usage_total = usage_tariff_2 + usage_tariff_1 %}
...
It seems to work now with exactly those changes “der-optimist” suggested.
Will check the math with excel, the next couple of days and make alterations if needed, but the code itself is working and I have an extra sensor entity that I can display as a Gauge
This calculation monitors my energy usage versus the energy returned to the grid.
As we are only using electricity (no Gas etc) it’s nice to be able to monitor how many kWh we have left in our account before we start paying for kWh’s that are used from the grid.
With the above (and with the monitoring of the daily energy usage of my heat-pumps for warm water and for central heating) I can make predictions (based on outside temperatures and number of people in my home) on the energy usage till the end of my contract and decide to charge my car using the kWh I got left, or charge my car at a public charging station.
At the moment, electricity from the grid at home is way more expensive than at a public charging station…
Public charging is about 31 cents per kWh
Electricity from the grid (at home) is (depending on the contract) between 40 and 70 cents per kWh
In 2023, the government will intervene and guarantees 40 cents per kWh till a max usage of 2900 kWh.
As before, even when electricity was only 21,5 cents per kWh, we are aiming to return as many kWh to the grid as we use from the grid having 22 solar panels since 2018.
Thanks you all for your kind help and making me understand a bit more on how templates work,
I am happy I no longer have to do the weekly math in Excel
This code looks easier and I am definitely going to test this.
I agree that (being a novice but capable of doing some Python coding) that variables are much easier to read and modify if needed.
I need to change these numbers once a year (our contracts and specially now) are one year contracts)
Although I have been using HA for almost 2 years now, for me it’s more a “monitoring” system then anything else. I use the Tuya app to control the lights (lights on at sunset and off at a certain time)
A “Nest” thermostat to control the temperature
Envoy and Growatt (Cloud) to monitor the Solar energy produced.
I have not yet found the need to use HA to “automate” anything, also because my wife and (out_living) children don’t want to use HA. They are happy with a tablet on the wall to switch on the lights (Tuya)
and the automated settings for the termostat etc.
With Floor Heating, the temperature remains (almost) the same 7 days a week.
With a well insulated home, the temerature drop overnight is about 1-1,5 degrees in 8 hours (depending on the outside temperature and the amount of wind)
Again… my thanks to those who were willing to help me
Have a wonderful day
Of course you must do with HA what suits you best, but “using” HA is explicitly not what it is for i.m.o. when it comes to normal living in a house. For the casual observer, my home is like any other home. For me, the power of HA is to have one place where all comes together. Tuya can only do lights, the heating system only knows about itself…
Things I use HA for is to make every subsystem a bit smarter by exchanging information. Examples: my heating is set for normal home use in my thermostat. But HA intervenes when needed. When everyone leaves - heating off. When my kids leave, the heating in their room lowers. When the humidity in the bathroom rises (shower): crank up the ventilation and the bathroom thermostat, but leave it lower the rest of the time. When a window opens, shut of the radiators in that room. A high temperature will start a fan in a dead zone where hot air tends to accumulate.
I also have “modes” that are controlled by simple buttons: Home, away, sleep, vacation. The house automations respond to these modes, e.g. in the vacation the lights go on and off as usual, rollerblinds go up and down to get the feeling some-one might still be home. Sleep turns off all the lights, and other devices like the tv. Garden lights behave a little different, some motion sensors too.
A lot of sunlight outside will lower the sunscreens, but not if the heating system has heated that day, and not in a vacation. The robot vacuum will start when nobody is home, unless it’s been a while then it will start anyways… The possibilities are endless to improve the smartness of the individual subsystems.
Thanks for sharing that information and of course I am aware of that functionality
When on Holiday, I would like the lights to go on and off just the same
In fact I do have an “automation” that switches off the quooker and other appliances while on holiday.
and sets the Panasonic Heat pump to “Holiday”
I modified the code as per the suggestion of “Der - Optimist”
template:
sensor:
name: “kWh_left”
unit_of_measurement: “kWh”
state: >
{% set tariff_1_used_startvalue = 15268 %}
{% set tariff_2_used_startvalue = 9616 %}
{% set tariff_1_produced_startvalue = 8618 %}
{% set tariff_2_produced_startvalue = 3457 %}
{% set tariff_1_used_currentvalue = states(‘sensor.energy_consumed_tariff_1’) | float(0)%}
{% set tariff_2_used_currentvalue = states(‘sensor.energy_consumed_tariff_2’) | float(0)%}
{% set usage_tariff_1 = tariff_1_used_currentvalue - tariff_1_used_startvalue %}
{% set usage_tariff_2 = tariff_2_used_currentvalue - tariff_2_used_startvalue %}
{% set usage_total = usage_tariff_1 + usage_tariff_2 %}
{% set tariff_1_produced_currentvalue = states(‘sensor.energy_produced_tariff_1’) | float(0)%}
{% set tariff_2_produced_currentvalue = states(‘sensor.energy_produced_tariff_2’) | float(0)%}
{% set produced_tariff_1 = tariff_1_produced_currentvalue - tariff_1_produced_startvalue %}
{% set produced_tariff_2 = tariff_2_produced_currentvalue - tariff_2_produced_startvalue %}
{% set produced_total = produced_tariff_1 + produced_tariff_2 %}
{% set sum = produced_total - usage_total %}
{{ sum | round(1, default=0) }}
I wish I had started of with this method.
I will add some varables and see if I can create a button that I can click on the day the new contract starts and take the 4 values of that moment, as the “startvalue”
In that way I don’t need to change the configuration yaml.
I was wondering if I can take the template out and put it in a separate file while putting the following in the configuration.yaml
You could use input_numbers for your start values and just change them there in the dashboard, rather than having to edit your sensors every time they change
That’s true. And you can create a button that stores the current values in these input_numbers and by that have the mentioned functionality of “one button to click on new contract day”.
But the “more HA” way would be to set a trigger for contract day and do that automaticly (that’s also very easy, I think even easier than creating a button)