If elsif code questions

I’m trying to use a template to count how many input_numbers are not zero:

{% set i = 0 %}
{% if states('input_number.sunday') | int != 0 %}
i++
{% elif states('input_number.monday') | int != 0  %}
i++
{% elif states('input_number.tuesday') | int != 0 %}
i++
...
{% endif %}
{{i}}

i is always zero. Even if I could get it to work, it seems cumbersome. Alternatively, I tried adding the days to a group & iterating through the group, but I couldn’t find anyway to get that to work either. For my programming knowledge, I’d love help with both approaches if there are such…

Copy-paste the following template into the Template Editor and experiment with it to understand how it works.

{% set days = [ 'input_number.sunday', 'input_number.monday',
  'input_number.tuesday', 'input_number.wednesday', 'input_number.thursday', 
  'input_number.friday', 'input_number.saturday' ] %}

{{ expand(days) | map(attribute='state')
  | map('float') | select('>', 0) | list | count }}

For future reference, this is Jinja2 templating language (Templating).


EDIT

I used > 0 as the test so it checks if the value is positive. However, if you want to count positive and negative values then the test should be != 0 to simply exclude zero values.

Thanks! It works perfectly. Is there a way of doing something similar to my first try? (I’m an old Perl & PHP programmer, have very little experience with python & python-based languages.)

1 Like
{% set i = 0 %}
{% if states('input_number.sunday') | int(0) != 0 %}
  {% set i = i + 1 %}
{% endif %}
{% if states('input_number.monday') | int(0) != 0 %}
  {% set i = i + 1 %}
{% endif %}
% if states('input_number.tuesday') | int(0) != 0 %}
  {% set i = i + 1 %}
{% endif %}
...

{{i}}

Jinja2 does support elif but I don’t think that’s the right choice for the way you want it to work here; execution of the chain of elif statements would end at the first elif that evaluates to true.

To avoid the issues Taras pointed out, you could use a loop:

{% set ns = namespace( i = 0 ) %}
{% set days = ['input_number.sunday', 'input_number.monday', 'input_number.tuesday', 'input_number.wednesday', 'input_number.thursday', 'input_number.friday', 'input_number.saturday'] %}
{% for x in days %}
{% if  states(x) | int(0) != 0 %}
{% set ns.i = ns.i + 1 %}
{% endif %}{% endfor %}
{{ ns.i }}

FWIW, loops are useful if the desired result can’t be easily achieved with a chain of filters (which leverages Jinja2’s strengths). Otherwise, a loop becomes just a longer way to get the job done (although shorter than the brute-force method of a long chain of if-else).

Thanks, Drew! Thanks, Taras. A lot of learning here.

You’re welcome!

If you chose to use the example I posted to solve your problem, please consider marking my post above with the Solution tag. Only one post in the entire thread can bear the tag.

It will automatically place a check-mark next to the topic’s title and a link below your first post that leads to the Solution post. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.