However, that will cause a comma to be used even if there are only two items in the list. To respect the ‘three items or more’ rule would require a slightly more elaborate template. It would use an Oxford comma only if there are three or more items.
It’s not difficult to implement it but would complicate the original example (I wanted to keep it simple).
{% set doors = 'Front, Garage, Patio, Yard' %}
{% set x = ', and ' if doors.split(', ') | count > 2 else ' and ' %}
{{x.join(doors.rsplit(', ', 1))}}
{%- set ns = namespace(not_off=[]) %}
{%- for s in expand('group.iungo_switches_actueel')
if s.object_id.split('_actueel')[0] not in
['inductieplaat','patchboard_zolder'] and
states(s.entity_id) != '0' and
states('switch.' + s.object_id.split('_actueel')[0]) == 'off' %}
{%- set ns.not_off = ns.not_off + [s.name+ ': ' + s.state + ' watt'] %}
{%- endfor %}
{% set count_not_off = ns.not_off|count %}
{% set list = ns.not_off %}
{% if count_not_off == 0 %} All fine
{%- elif count_not_off == 1 %}
{{list[0]}} is still using power!
{%- elif count_not_off == 2 %}
{{list|join(' and ')}} are still using power!
{%- else %}
{{list[:-1]|join(', ')}}, and {{list[-1]}} are still using power!
{%- endif %}
I don’t know how I ended up here after all this time but…
How did you know that rsplit was valid?
It isn’t in the Jinja Template Designer Documenattion which is what the HA Docs point to for templating reference.
I’ve been using split for years in my templates, so long in fact that I have no idea now how I knew that was valid as I just checked and it is also not in the docs.
So cutting to the chase…
(Where) is it documented what is available to use in templates?
Am I missing out on loads of other gems?
The added wrinkle is that not everything that is possible in python is supported in Home Assistant’s Jinja2 templates.
On more than one occasion I have found a python example that was impossible to use in a template because it employed something unsupported (the concept of sets comes to mind as well as various list methods). Finding these limitations has been based on trial and error.
FWIW, the technique demonstrated in the first post comes from a python example I found and adapted to what’s permissible in Home Assistant.
I have been running around trying to look for such a document or reference. Thank you very much for explaining that it doesn’t exist and why. But if ever someone comes across some sort of list of methods that we know (so far) works, that’d be nice to put it here as a comment, so we know what is worth trying.