A simple Math?

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 %}
...

Thanks for all the very kind assistance :slight_smile:

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

afbeelding

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 :slight_smile:

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.

Just some ideas on how HA could contribute.

Hi Edwin,

Thanks for sharing that information and of course I am aware of that functionality :slight_smile:
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

template: !include templates.yaml

Thanks

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 :slight_smile: (that’s also very easy, I think even easier than creating a button)