Script condition with loop

I want to run a script that has as condition switch.
The tested condition is a loop. If one item returns true, the condition is fulfilled.
In all other cases default condition should be used.

The template holds a variable named ‘player’, this script is called via an automation that passes the exact player entity_id to the script.

When testing the template from development tools, the loop seems to function correct.
It returns ‘true’ when one of the players in the sync group is already playing.
But when I put this inside the script, the default condition is always chosen.

Any help would be appreciated.

squeezebox_play_music:
  alias: Play music
  sequence:
  - service: squeezebox.call_method
    data_template:
      entity_id: '{{ player }}'
      command: power
      parameters: 1
  - service: squeezebox.call_method
    data_template:
      entity_id: '{{ player }}'
      command: mixer
      parameters:
      - volume
      - '8'
  - choose:
    - conditions:
      - condition: template
        value_template: >
          {% for entity_id in state_attr(player, 'sync_group') %}
            {% if states(entity_id) == 'playing' %}
              true
            {% endif %}
          {% endfor %}
      sequence:
      - service: squeezebox.call_method
        data_template:
          entity_id: '{{ player }}'
          command: playlist
          parameters:
          - play
          - ''
          - ''
          - '4'
    default:
    - service: squeezebox.call_method
      data_template:
        entity_id: '{{ player }}'
        command: playlist
        parameters:
        - play
        - MyPlaylist
        - ''
        - '4'

Should have posted this earlier, so I’ve found the answer sooner :slight_smile:

I’ve changed the condition part as follows, and it’s working now.

      - condition: template
        value_template: >
          {% set playing = namespace(found=False) %}
          {% for entity_id in state_attr(player, 'sync_group') %}
            {% if states(entity_id) == 'playing' %}
            {% set playing.found = True %}
            {% endif %}
          {% endfor %}
          {{ playing.found }}
      sequence:
      - service: squeezebox.call_method
        data_template:
          entity_id: '{{ player }}'
          command: play
          parameters:
          - '4'