Templates and script problem

I have a little problem whit some scripts and templates.

I have made a dashboard to join my multiroom speakers, but can’t get it to work correctly.

one speaker works but when I want to join more than 2 my entity list doesn’t work

- name: heos gruppera
    state: >
      {% set output = namespace(heos=[]) %}
      {% for state in states.input_boolean | selectattr('entity_id','search','heos') %}
        {% if state.state == 'on'  %}
          {% set output.heos = output.heos + [state.entity_id | replace("input_boolean","media_player") ~ ('\n') ] %}
        {% endif %}
      {% endfor %}
      {{ output.heos | join  }}
heos_gruppera:
  alias: heos gruppera
  sequence:
  - service: media_player.join
    data:
      group_members: 
        - "{{ states('sensor.heos_gruppera') }}"
    target:
      entity_id: "{{master}}"

gruppera_badrum:
  alias: gruppera badrum
  sequence:
    - service: script.heos_gruppera
      data:
        master: media_player.heos_badrum
  mode: single

gruppera_kok:
  alias: gruppera kök
  sequence:
    - service: script.heos_gruppera
      data:
        master: media_player.heos_kok
  mode: single

one works
step details

Executed: May 24, 2022 at 21:52:16
Result:
params:
domain: media_player
service: join
service_data:
group_members:
- media_player.heos_badrum
entity_id:
- media_player.heos_kok
target:
entity_id:
- media_player.heos_kok
running_script: false
limit: 10

and when I want more than one I get “|-” group_members:

Executed: May 24, 2022 at 21:52:18
Result:
params:
domain: media_player
service: join
service_data:
group_members:
- |-
media_player.heos_badrum
media_player.heos_flyttbar
entity_id:
- media_player.heos_kok
target:
entity_id:
- media_player.heos_kok
running_script: false
limit: 10

Change your Template Sensor to this:

  - name: heos gruppera
    state: >
      {{ states.input_boolean | selectattr('object_id', 'search', 'heos')
        | selectattr('state', 'eq', 'on') | map(attribute='entity_id')
        | map('replace', 'input_boolean', 'media_player')
        | list | join(' ') }}

Then make a small change to the heos_gruppera script:

heos_gruppera:
  alias: heos gruppera
  sequence:
  - service: media_player.join
    data:
      group_members: "{{ states('sensor.heos_gruppera').split() }}"
    target:
      entity_id: "{{master}}"
1 Like

Thanks for the much nicer template

But I still have errors
The group members need to be listed

group_members: 
  - media_player.heos_badrum
  - media_player.heos_flyttbar

To make it work and with this code I get

 group_members: media_player.heos_badrum media_player.heos_flyttbar

But thanks for the help so far

The value for group_members must be a list (for more than one media_player).

In YAML, this is a list:

group_members: 
  - media_player.heos_badrum
  - media_player.heos_flyttbar

This is also a list:

group_members: ['media_player.heos_badrum', 'media_player.heos_flyttbar']

The value contained by sensor.heos_gruppera looks like this:

media_player.heos_badrum media_player.heos_flyttbar

However, the split() function in this template:

{{ states('sensor.heos_gruppera').split() }}

converts it to a list that looks like this which is what group_members requires:

['media_player.heos_badrum', 'media_player.heos_flyttbar']

Ensure the script’s template contains the split() function.

You where right I have missed the split()

It works much better now

Thanks for the help

1 Like

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.