Jinja2 templating built-in filters

This page list the built-in filters for Jinja2 but it does not include “split()” and “rsplit()”. I’ve seen several codes posted in thiis forum that used both these features, here is one examples that works in template editor.

{% set  windows_sensors=['binary_sensor.window_d1','binary_sensor.window_d2','binary_sensor.window_d6','binary_sensor.window_d5'] %}
{% set windows = ( windows_sensors | select('is_state', 'off') | list | join(' ,' ) ) %}
{% set x = ', and ' if windows.split(', ') | count > 2 else ' and ' %}
{{x.join(windows.rsplit(' ,', 1))}} {{'are' if windows | count > 1 else 'is' }}

I’m just curious if there is more to template filters than the documentation page.

These are Python methods which are mentioned briefly in the Jinja Docs. I’ve never seen an exhaustive list of which methods work in HA’s implementation of Jinja.

1 Like

Thanks and that makes sense. The above code in the template editor produces following output for me:

binary_sensor.window_d1 ,binary_sensor.window_d6 and binary_sensor.window_d5 are open right now.

And noticed the “binary_sensor.window_d1 ,binary_sensor.window_d6” part and wanted to change the spacing order around the “,” such as “binary_sensor.window_d1, binary_sensor.window_d6”.

I tried all tweaks in the code (last two lines) but did not get the desired result so not sure if I’m doing it wrong or if this is the limitation of this code.

{%- set  windows_sensors = ['binary_sensor.window_d1', 'binary_sensor.window_d2', 'binary_sensor.window_d6', 'binary_sensor.window_d5'] %}
{%- set windows = windows_sensors | select('is_state', 'on') | list %}
{%- set w_count = windows | count %}
{%- set x = ', and ' if w_count > 2 else ' and ' %}
{{- x.join((windows|join(', ')).rsplit(', ', 1)) -}}
{{- ' are ' if w_count > 1 else ' is ' -}} open right now.
1 Like