How do I omit the comma only on the last item in the returned list, here is my code below.
{%- for entity_id in states.group.all_updater_entities.attributes.entity_id -%}
{%- if states(entity_id) == 'on' -%}
{%- if not loop.last -%}
{{ state_attr(entity_id, 'friendly_name') | replace ('Updater', '') }}{{', '}}
{%- else -%}
{{ state_attr(entity_id, 'friendly_name') | replace ('Updater', '') }}
{%- endif -%}
{%- endif -%}
{%- endfor -%} has updates available.
That if not loop.last
condition doesn't work because the last item of the returned list might not be the list item of the group in the for loop. Thanks in advance for you help.
CodePudding user response:
you modify your j2 like this (add condition if to the loop for):
{%- for entity_id in states.group.all_updater_entities.attributes.entity_id if states(entity_id) == 'on' -%}
{%- if not loop.last -%}
{{ state_attr(entity_id, 'friendly_name') | replace ('Updater', '') }}{{', '}}
{%- else -%}
{{ state_attr(entity_id, 'friendly_name') | replace ('Updater', '') }}
{%- endif -%}
{%- endfor -%} has updates available.
The advantage is that the special loop variable will count correctly; thus not counting the entities not iterated over.