When leaving the house asking whether all windows are closed

Hi,

I am new to this community and started to program with the HA a months ago. The first automations and scripts worked well with my KNX systems (HA is a great addon to my existing home automation) but I now I struggle with the current task.

Task: When I leave the house I would like to check whether all windows (around 40) are closed and if one or more are open I would like to say the name of the window thorugh my SONOS system.

I set up the following script:

versionzwei:
sequence:
- service: media_player.play_media
data:
entity_id: media_player.eingang
media_content_id: ‘http:/XXXX:8123/local/audio/Anmfo.mp3’
media_content_type: ‘music’
- delay:
seconds: 3
- wait_template: “{{ is_state(‘media_player.eingang’, ‘paused’) }}”

      - service_template: >
            {% if is_state('binary_sensor.fk1','on') %}
                script.turn_on
            (% else %}
                script.turn_off
            {% endif %}
        
            entity_id: script.fk1

      - service_template: >
            {% if is_state('binary_sensor.fk3','on') %}
                script.tun_on
            (% else %}
                script.turn_off
            {% endif %}
            
            entity_id: script.fk3
      
fk1:
    sequence:
      - condition: state
        entity_id: binary_sensor.fk1
        state: 'on'
      - service: media_player.play_media
        data:
            entity_id: media_player.eingang
            media_content_id: 'http://XXXXX:8123/local/audio/fk1.mp3'
            media_content_type: 'music'
      - delay:
            seconds: 1
      - wait_template: "{{ is_state('media_player.eingang', 'paused') }}"           

The issue remains that the script does not run stable It misses cerain Windows.

Is there a smarter way to program the Task?
Any help is welcome.

A recent post had an elegant solution for a similar problem that is probably adaptable to your case:

The templates examples on the front end under developer tools has some nifty examples of loop last and whatnot that might allow you to make your notification sound more natural.