ok Thank You
I´m sorry to pull out a 2 year old thread but as I´m currently optmizing my dashboard i found this gem of a template
Now I´m still a noob in templating and need help. Where do I put the “if/else” if I want basically that the template is showing me the amount of opened windows and if all are closed just a “all closed”?
Thanks for your help
{% set number_open = (expand('group.windows') | selectattr('state', 'eq', 'on') | list | count) %}
{% if number_open > 0 %}
{{number_open}}
{% else %}
"all closed"
{% endif %}
You Sir are a legend! Thanks a lot!
This is working perfectly fine for me although I need to do some adjustments and I can’t figure out how to do it.
All my room sensors are called for example “Aqara Sensor Kitchen Contact”, “Aqara Sensor Bathroom Contact”, and so on.
How can I remove the first and last part of the string so that the correct output will be “The following window’s are open: Kitchen, Bathroom”
Use python’s ability to slice a string in order to slice off the ‘Aqara Sensor’ and ‘Contact’ portions.
Or use the split
function to convert the string to a list (using a space character to serve as the delimiter) and then display the list’s third item (list index is 2).
Thanks for pointing me into the right direction.
I played around with split
but couldn’t make it work in this specific code.
replace
did the trick for me. Here is my adjusted code for anyone who is struggling like me.
{% if is_state('binary_sensor.alle_fenster', 'on') %}
{% set openWindows = expand('binary_sensor.alle_fenster')
| selectattr('state','eq','on')
| map(attribute='name')
| list %}
{% set qty = openWindows | length %}
{% set p1 = 'Das' if qty == 1 else 'Die' %}
{% set p2 = 'ist' if qty == 1 else 'sind' %}
{{ '' if qty == 0 else '{} folgende Fenster {} geöffnet:{}.'.format(p1, p2, openWindows | join(',') | replace('Aqara Fenstersensor', '') | replace(' contact', '') | replace('ue', 'ü')) }}
{% else %}
{% endif %}
Another way to do the same thing:
{% if is_state('binary_sensor.alle_fenster', 'on') %}
{% set openWindows = expand('binary_sensor.alle_fenster')
| selectattr('state','eq','on')
| map(attribute='name')
| map('regex_replace', '(Aqara Fenstersensor | contact)', '')
| map('replace', 'ue', 'ü') | list %}
{% set qty = openWindows | length %}
{{ '{} folgende Fenster {} geöffnet:{}.'.format(iif(qty == 1, 'Das', 'Die'), iif(qty == 1, 'ist', 'sind'), openWindows | join(',')) }}
{% else %} {{ '' }}
{% endif %}