Need help with "else" template

i’m using a great speech engine that @ccoston created. I want to modify it to check / speak if a zone is violated. If not violated to then say “everything is closed.” I’m using the ‘else’ but for the life of me i can’t get it to work. sorry but i’m still new to python. It never gets to the ‘else’ when all zones are not violated.

here is my code
indent preformatted text by 4 spaces
{%- for entity_id in states.group.elkzones.attributes.entity_id -%}
{% set parts = entity_id.split(’.’) -%}
{%- if states(entity_id) != ‘Normal’ %}
{%- if loop.first %} {% elif loop.last %} and the {% else %},The {% endif -%}{{ states[parts[0]][parts[1]].name }} needs to be closed{% endif -%}
{%- else -%}
{%- if states(entity_id) == ‘Normal’ %}
Everything is closed
{%- endfor %}
indent preformatted text by 4 spaces

you have an endif before the else. It’s hard to tell if this is exactly what you wanted, but try this:

{%- for entity_id in states.group.elkzones.attributes.entity_id  -%}
  {% set parts = entity_id.split(’.’) -%}
{%- if states(entity_id) != ‘Normal’ %}
   {%- if loop.first %} 
   {% elif loop.last %} and the 
   {% else %},The 
   {% endif -%}

   {{ states[parts[0]][parts[1]].name }} needs to    be closed
{%- elif states(entity_id) == ‘Normal’ %}
  Everything is closed
{% endif -%}
{%- endfor %}

@treno thank you for trying. the ‘endif’ actually works where it’s at. If a zone is violated it will voice out all the zones or the one zone that is violated. My problem is when there is no zone violated that i want it to go to the ‘else’ and voice “Everything is closed”

I tried your code into dev-template and i got the following error:

Error rendering template: TemplateSyntaxError: unexpected char ‘’’ at 102

Again, the code i displayed doesn’t throw any errors, it is just when there isn’t any zones violated it doesn’t voice “everything is closed”. Only when there are zones open it will loop and let me know which zones are open.

Thank you very much for trying. I’m pulling my hairs out over here.

edit: @treno I found the issue with your code…nothing at all :slight_smile: it was when i copied and pasted the " ’ " was formatted differently due to the webpage.

Yes your code works, I should have said I just want one return " Everything is closed". I get however many zones i have. Currently 14 returns :frowning:

Here is an intent script that I use that does much the same thing as yours.
The trick here is to check the loop index. If the loop index is greater than zero, then one of the entities in the loop matched. This is pretty tricky, but I think it does what you want.:

 {%-for state in states 
    if (<my_condition_list> -%}        
        {%- if loop.first -%}The {% elif loop.last %} and the {% else %} the {% endif -%} 
           {{state.name}} is  {{state.state}},
       {%- if loop.last -%}
         {%- if loop.index >0  %}
           Everything else is off. .
         {%- endif%} 
        {%- endif%} 
    {%- else%}
       Everything is off. .
    {%- endfor -%}

@treno thank you a million times. you are a genius! this worked perfectly. here is the final code i used.

Blockquote
{%- for entity_id in states.group.elkzones.attributes.entity_id -%}
{% set parts = entity_id.split(‘.’) -%}
{%- if states(entity_id) != ‘Normal’ %}
{%- if loop.first %} {% elif loop.last %} and the {% else %},The {% endif -%}{{ states[parts[0]][parts[1]].name }} needs to be closed{% endif -%}
{%- if loop.last -%}
{%- if loop.index >0 %}
Everything else is off. I will activate the alarm
{%- endif%}
{%- endif%}
{%- endfor %}