Get list of sensors that are open

hello, i’ve an automation made with the web ui that will send a notification via telegram if the group.door_sensors is on, that mean if at least one door is open it will send a generic notification, i would like to update this with the list of the sensors that are on, do you have any suggestion on how to change this automation to loop throu all the sensors in the group door_sensors and save the list of sensors with state open and append it to a telegram message?

thanks a lot

I have something like this as an action, you’ll need to doctor it a bit but should give an idea:

service: notify.mike
data:
  message: |
    The following devices are unavailable: 
    {%- for item in expand('group.monitored_devices') -%}
    {%- if item.state == 'unavailable' -%} 
     {{ item.name }}
    {%- endif -%}
    {%- endfor -%}  

thank you so much, i’ve edited your code like this:

 - data:
        message: >
    hai lasciato aperto: {% for item in expand('group.door_sensors') %}{% if
    item.state == 'on' %}{{ item.name}}, {% endif %}{% endfor %}
    service: notify.telegram

and it seems pretty ok, i have added > in the mssage to be able to have spaces, unfortunatelly i can not format the code better otherwise i have new line or other spaces in it.

Try adding {{ '\n' }} for a new line:

hai lasciato aperto: {{ '\n' }}
{% for item in expand('group.door_sensors') %}
  {% if item.state == 'on' %}
    {{ item.name + '\n'}}
  {% endif %}
{% endfor %}
- service: notify.your_name
  data:
    message: >
      hai lasciato aperto: 
      {{ expand('group.door_sensors') | selectattr('state', 'eq', 'unavailable')
         | map(attribute='name') | list | join('\n') }}
2 Likes
message: "hai lasciato aperto: \n{{ expand('group.door_sensors') | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join('\n') }}"

i think with this i pretty good, for future editing and as the message i get!!!
thanks all

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.