Advanced Jinja2 Templating for fun and profit

thank you for the answer. Im try to understand because CCOSTAN have a very nice speech_engine and want us that as well. But first need to understand the way how macro work.

1 Like

you could add a dash at the end of {{ device.name }} for remove spacing in output:

# Loop over devices with a state of not_home
{% for device in dict(states.device_tracker|groupby('state'))['not_home'] %}
  # If this not the last device add a comma. Otherwise, use the word "and". Don't do either if there's only 1 device
  {% if loop.last and loop.length > 1 %} and {% elif not loop.first and loop.length > 1 %}, {% endif %}
  # Print the device name
  {{ device.name -}}
{% endfor %} {% if dict(states.device_tracker|groupby('state'))['not_home']|length == 1 %}is{% else %}are{% endif %} away.
# The line above will output 'is' or 'are' depending on how many devices are in the list.
1 Like