Is this a 'guarded' template

having a full checkup of the templates, I was experimenting with this

        icon_template: >
          {% if states.persistent_notification|count > 0 %} mdi:message-bulleted
          {% else %} mdi:message-bulleted-off
          {% endif %}

is the regular form I use all over, but given the two options, could be shortened:

{{'mdi:message-bulleted-off' if states.persistent_notification|count == 0 else 'mdi:message-bulleted'}}

now, I can even take out the ‘mdi:message-bulleted-’ bit and try something like this

mdi:message-bulleted{{'-off' if states.persistent_notification|count == 0}}

which works for now. Not perfectly sure though this is a guarded enough template.
Thoughts on the matter are appreciated.

What is a guarded template?

1 Like

I remember when I was experimenting with inline if statements, a long time ago, I encountered an error if I left out the else (and the test evaluated to false and needed an else). Based on that experience, I would write the template like this:

mdi:message-bulleted{{'-off' if states.persistent_notification|count == 0 else ''}}

HOWEVER, I tried your example in the Template Editor and it doesn’t seem to care if there’s no else.

I guess your template should be OK without the else. :man_shrugging:

That’s a template that takes care of the unexpected, most of the time with an ‘else’ clause.

Yeah, thanks. That’s what I tried too, the ‘’ (sorry, on mobile right now)

Seems the way I test it now is a guarded template the other way round: always have this value, except when state == 0