Understanding of the Use of Script Variables in If Statement

This is commonly called a “switch case”.

Yes, you can use a either an if/elif construct or a dictionary. In both methods you need to take care with the order of options based on the comparison type you are using. The cases are rendered from top to bottom and the first one to render true will be returned. You can avoid some problems by using chained comparisons and <= instead of < where appropriate:

tempo: |
  {% if soc <= 20 %}
    {{ soc_calculo * 10 }}
  {% elif 20 < soc <= 40 %}
    {{ soc_calculo * 50 }}
  {% elif 40 < soc <= 60 %}
    {{ soc_calculo * 70 }}
  {% else %}
    {{ soc_calculo * 100 }}
  {% endif %}

In a dictionary, you use the same type of pattern but different methods to find the correct value.

{% set dict = { soc <= 20: 10, 20 < soc <= 40: 50, 40 < soc <= 60: 70 } %}
{{soc_calculo * (  100 if true not in dict.keys() else dict.get(true)) }}
1 Like