Is it possible to use local variables in definition of a template sensor?

Hello all.

To accurately calculate the level of a cistern, I would like to convert the value of an ultrasonic sensor (unit: meter) into the actual usable volume (unit: liter)

Since the calculation for this is a bit more complex, I would like to use variables to keep the overview myself for one thing. On the other hand, it would also simplify the calculation.

Is there a possibility to use local variables in the definition of a template sensor? If so, how does that work?

I am grateful for any help.

Kind regards

Hannah

Not sure what you mean by “local variables” in this context. If you mean variables created in automations then no you can’t. You also can’t use jinja variables outside of the template that created them.

What you can do is create more template entities. Like have one which is the value converted from meters to liters. And then another which uses that sensor to do other calculations. Both sensors will appear as entities so they aren’t really local variables but they work for this.

You can also technically stuff intermediary calculations in the attributes of the same entity but I wouldn’t really recommend it. It adds a ton of complexity and most likely has worse performance, I put together something showing how here

Hello and thank you for your answer.
What I mean is actually much less complex, but I do not know if it is so implementable… What I want to do is to convert the value of an ultrasonic sensor into a liter value - using a simple template sensor:

To transform the value A into the value B, I have to apply a mathematical formula with several variables, for example for height and width of the cistern, the distance of the sensor to the water surface, the water height calculated from this, etc.
For this it would be helpful if I could create local variables to store values with defined value. I need the values only INSIDE the template, not outside of it.

A simplified and not valid example for what I mean would be something like this… with “local variables”

template:
  - sensor:
      - name: "Water level cistern"
        unit_of_measurement: "Liter"
        height_of_cistern: 245
        radius_of_cistern: 122
        sensor_height_above_ground: 188.5
        PI = 3.14159
        state: "{{  ( radius_of_cistern * radius_of_cistern * PI * (  sensor_height_above_ground - states('sensor.ultrasonic_cistern'))  / 1000 }}"

I am not an expert, so I am likely wrong. But I use helper entities as “local” variables.

Settings → Devices and Services:

settings-helpers

Ah ok… nice idea… I have not thought of it.
Thought it would be easier to just quickly allocate a few variables that you only need once. But i will try it if there is no real “local” solution

You can declare variables within the template as follows…
FWIW, pi’s definition is built in so you don’t need to declare it, and you can use ** for exponential notation.

template:
  - sensor:
      - name: "Water level cistern"
        unit_of_measurement: "Liter"
        state: |-
          {% set height_of_cistern = 245 %}
          {% set radius_of_cistern = 122 %}
          {% set sensor_height_above_ground = 188.5 %}
          {{  ( (radius_of_cistern ** 2) * pi 
          * (  sensor_height_above_ground 
          - states('sensor.ultrasonic_cistern'))  / 1000 }}

or you can set them as attributes:

template:
  - sensor:
      - name: "Water level cistern"
        unit_of_measurement: "Liter"
        state: |-
          {{  ( (this.attributes.radius_of_cistern ** 2) * pi 
          * ( this.attributes.sensor_height_above_ground 
          - states('sensor.ultrasonic_cistern'))  / 1000 }}
        attributes:
          height_of_cistern: 245
          radius_of_cistern: 122
          sensor_height_above_ground: 188.5

EDIT: Fixed errors… hit submit button too fast…

1 Like

oh perfect… this is quiet cool and exactly what I was looking for… thanks a lot!