Template - Create an average on entities

Nico Writing by Nico
  2 min of reading

This page has been visited ... times

Template - Create an average on entities

For our first template tutorial, I suggest you create averages from the state of the values of the entities (sensor). In my cases I need to take an average of two values concerning the off-peak/peak time teleinformation in order to bring up a single common value. In addition it is expressed by default in Wh and I would like to transform it into KWh. You will see that Liquid/Jinja` it’s not very complicated!

Prerequisites

Home Assistant installed

Box Home Assistant Green on sale at domadoo for less than €100

Box HomeAssistant Green

By Nabu Casa

Box Home Assistant Green on sale at domadoo for less than €100

Template and entity

To create a template, nothing could be simpler, go to home assistant

Parameters/inputs/create an entry/template/Model a sensor

get the values

value to be noted

notes the name of the entity

find the name of the entities

Code explanations

The teleinfo module that I use as well as my subscription provide off-peak/peak time information, for personal reasons I pay exactly the same thing whatever the price.

We save the first state under hc

{% set hc = states('sensor.teleinfo_hchc') | float %}

We save the second state under hp

{% set hp = states('sensor.teleinfo_hchp') | float %}

We display the hc + hp value all divided by 2 and then divided by 1000 with rounding of 2 after the decimal point

{{ ((hc + hp) / 2 / 1000) | round(2, default=0) }}

Code result

{% set hc = states('sensor.teleinfo_hchc') | float %}
{% set hp = states('sensor.teleinfo_hchp') | float %}
{{ ((hc + hp) / 2 / 1000) | round(2, default=0) }}

Animated explanation

The device class and the state class will be displayed in red if there is a mismatch with the chosen entity, it’s pretty well done.

Creation of a template on entity averages in home-assistant in animated gif

You will be able to check the data provided directly before validating the template.

Nico

Nico

Founding member of the Haade site, a home automation enthusiast with all the tricks that go well.

Comments