All Doors closed TTS YAML script help

I setup a script that checks my doors to see their states. As it currently is, it either reports “All doors are secure” if all states of the entities are off. If one of the entities is On it goes through the list. Door is Off, Porch is Off, Garage is OPEN, Slider is Off…

I would like it to ONLY report back which entities are On, instead of Alexa rambling all of the states. But also report back with a custom ____ is Open instead of ____ is On.

i.e. " Door is Open" not “Door is On”

door_check:
alias: Door Check all Doors TTS Living
sequence:

  • service: media_player.alexa_tts
    data_template:
    entity_id: media_player.living_room
    message: ’
    {%- if is_state(“binary_sensor.door_window_sensor_158d000208e248”, “off”) and
    is_state(“binary_sensor.door_window_sensor_158d0002228713”, “off”)and
    is_state(“binary_sensor.door_window_sensor_158d000245b2c7”, “off”)and
    is_state(“binary_sensor.door_window_sensor_158d000245b38a”, “off”) -%}
    All doors are secure
    {%- else -%}
    The Front door is {{ states(“binary_sensor.door_window_sensor_158d000208e248”) }}
    The Front Porch door is {{ states(“binary_sensor.door_window_sensor_158d0002228713”) }}
    The Slider Door is {{ states(“binary_sensor.door_window_sensor_158d000245b2c7”) }}
    The Garage Door is {{ states(“binary_sensor.door_window_sensor_158d000245b38a”) }}
    {% endif %} ’

Hi ElSlabo.

First of all you should try to use the “preformatted text” feature. Paste your code, select it and click on it:

Than it will look like this

You should look on this site

To make things easier, try to create a group of all your sensors.

You are lucky. I’m working on this too, so i will post my results here.

I’m testing around with the Template editor. Searching this forum etc.

{% for state in states.binary_sensor -%}
{% if state.binary_sensor == off %}
{{ state.name }} is closed
{% endif %}
{%- endfor %}

This gives me:
Frontdoor is closed

Livingroom Street is closed

Livingroom right is closed

Livingroom left is closed

Motion Sensor Car is closed

Button is closed

Now i try to get rid of the Motion Sensor and the Button (both also binary_sensor)

Forget about this post, its not working. If there is just one window/door open it will show:

, Livingroom left open

or

and Livingroom right open

without any other text around it.


Old Post:

This is what I need. It shows me all the open sensors:

{%- for entity_id in states.group.doorsensors.attributes.entity_id -%}
  {% set parts = entity_id.split('.') -%}
  {%- if states(entity_id) == 'on' %}
  {%- if loop.first %}Warning! The following sensors are OPEN: {% elif loop.last %} and {% else %}, {% endif -%}{{ states[parts[0]][parts[1]].name }}{% endif -%}
{%- endfor %}