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?
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”
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.