Template to combine enum sensors

Hi,

I would like to combine the state of several window sensors to a string (or a set or a list, does not really matter). I tried my best, but with the following code, I get an error: “invalid template (TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘endset’. The innermost block that needs to be closed is ‘set’.)”
Any hint is highly appreciated! My goal is to send a notification “The following windows are open {{openWindows}}” AND to blink an LED if any window is open "openWindows != ‘’ ".

Best regards,
Jonas

{% set open | string %}
{% if states('sensor.fenstersensor_room1') == 'open' %}
  {% set open = open~'Room 1' %}
{% endif %}
//more rooms would follow
{{open}}

Where did that come from?

{% set open = "" %}

There’ll be a neater way to do this. Give us a list of your sensors and the output you want, and you’ll probably get a few answers. Perhaps:

{{ states['sensor']
   |selectattr('entity_id','contains','fenster')
   |selectattr('state','eq','open')
   |map(attribute='attributes.friendly_name')
   |list|join(', ') }}

Great, thank you, that solved the issue!

I would love to find a better way to achieve this, e.g. with a group. What do mean by “give us a list”? The are all called “fenstersensor_XZY”.

Best regards,
Jonas

current ugly way would be like:

{% set open='' %}
{% if states('sensor.fenstersensor_room1') == 'open' %}
  {% set open = open+'Room1 ' %}
{% endif %}
{% if states('sensor.fenstersensor_room2') == 'open' %}
  {% set open = open+'Room2 ' %}
{% endif %}
{{open}}
{{ states['sensor']
   |selectattr('entity_id','contains','fenster')
   |selectattr('state','eq','open')
   |map(attribute='attributes.friendly_name')
   |list|join(', ') }}

Just saw this, that works, thank you so much!

One follow-up Question: The friendly names look like this “Fenstersensor-Schlafzimmer”. Is there any easy way to extend your solution to remote “Fenstersensor-” from every entry?

{{ states['sensor']
   |selectattr('entity_id','contains','fenster')
   |selectattr('state','eq','open')
   |map(attribute='attributes.friendly_name')
   |map('replace','Fenstersensor-','')
   |list|join(', ') }}

THANK YOU!!!

1 Like