Hi, I have been looking here, on the docs, on jinja docs, debugging on the developers tools and on the log but cannot get it right…
I am trying to create a generic script to test and act on a number of switches so I need to test it against a template condition on a choose statement and then use it on a formula for a variable to another script. But I cannot evaluate the expressions:
value_template: "{{ ( position | int ) > (states('sensor.{{ shade }}')|int + 9)|int}}"
and
variables:
deltaporcento: "{{ (position | int) - (states('sensor.{{ shade }}') | int ) | int }}"
Is there a way I can do it? I assumed so as I can use the entity formed for other tests…
Can someone please please help me as this string/code mix is making me crazy?
I can also publish the complete script section if there should be another way to accomplish that…
in order to use blanket variable names like ‘position’, or ‘shade’, you need to declare them ahead of time unless the docs say they are ‘in the environment’ already.
{% set shade = 'my_shade' %}
{% set postion = 4 %}
Then after you can perform tests in the template editor, however your tests are incorrect. When accessing variables, you don’t need extra {{ }} or {% %}. {{ }} returns a whole line of code as a string. {% %} executes a line of code without returning anything.
Also, when you’re trying to combine values, you need to combine them properly. Combining strings can be done using + or ~. Typically ~ is the safest bet.
Lastly, you only need to convert strings to other types. Using |int will convert whatever you’re using to an integer. Typically predefined variables in templates are already declared as a specific type, so you don’t need to convert it. Using states(), you do because it will always return a string.
{{ position - states('sensor.' ~ shade) | int }}
I believe your biggest hindrance right now is understanding types in code. I recommend doing a short interactive online python tutorial. That should familiarize yourself with typing. Then use the jinja documents once you understand that. If you can find a jinja tutorial, great, do that. But those are harder to find.
30+ years ago I did some programming and I did it on strongly typed languages. What I am very new to are undeclared variables and objects. I will try to find a tutorial for python types handling, thanks.
I know I am “filtering” too much to int, but that is exactly because I dont know what type they are and I am afraid! Position, which seems to be a pre-defined value for the cover entity, is float, but I could not find this info anywhere…
Anyway, your example saved my day! Pointed me the boundaries!
The ~ did the trick there and everything is working now.
I will leave my code beneath as this explanation might help someone as others have helped me.
Hi @petro!
I was trying something similar, but couldn’t figure it out. I wanted to go from this {% set lc = as_timestamp('states.sensor.xy_linkquality.last_changed') | int %}
to this:
{% set entity = 'xy_linkquality' %}
{% set lc = as_timestamp(states.sensor. ~ entity.last_changed) | int %}
but unfortunately a string doesn’t work in this case with the state object. Is there a way to integrate the variable?
Thank you very much!
May I ask, where is something like using these brackets "[ ]"for variables documented? Couldn’t find anything.
P.S.: the int was useful for my code