Can a helper variable be used in an action alias

Hi,

I’m rather new to HA and are making my first automation. I have 5 Sonos devices and for each device a custom button card that sets a boolean helper (on/off). Depending on the state of those 5 boolean helpers, I want to group the selected devices. I was wondering if I could use the boolean helpers to make my group_members conditional? Maybe it’s not possible, maybe I’m struggling with the syntax.

type: button
tap_action:
  action: call-service
  service: media_player.join
  target:
    entity_id: media_player.sonoskeuken
  data:
    group_members:
      - '{{input_boolean.bsonosdraagbaar, ''media_player.draagbaar''}}'
      - '{{input_boolean.bsonosbadkamer, ''media_player.badkamer''}}'
icon: mdi:multiplication-box
icon_height: 30px
name: Group
  1. Home Assistant’s standard set of cards don’t support templates (except for the Markdown card). Move the service call to a script and make the Button card call the script.

  2. If you want to use the entity’s value then you have to retrieve the entity’s value using the states() function.

  3. If you want to check if an entity’s value is equal to a specific value then you have to explicitly perform the comparison. For example, using is_state().

  4. Your example fails to consider the possibility that both input_booleans might be off. In that case, there would be no media_players specified and the media_player.join service is likely to fail with an error.

You need to build the list of media_players in a variable and then, if the quantity of media_players is greater than zero, proceed to join them with the service call, otherwise don’t execute the service call.

Let me know if you need more help to implement my suggestion.

Hi,

Thank you for this information, but as I told I have just started with home assistant. I get remark 1, I created a script and called it from the button card. In this script, I’m trying to use the is_state (remark 2 and 3), but I think I’m doing something wrong over there. I just tried a little test and wasn’t even thinking about remark 4 and the general remark. This is the piece of code I’m using without success. This code throws an error about an invalid entity_id for the group_members. Probably my is_state is wrongly used…

testcombine:
  alias: testCombine
  sequence:
  - service: media_player.join
    data:
      group_members:
      - "{% if is_state('input_boolean.bsonosbadkamer', 'on') %} media_player.badkamer {% else %} {% endif %}"  
      - "{% if is_state('input_boolean.bsonosstudeerkamer', 'on') %} media_player.studeerkamer {% else %} {% endif %}"  
    target:
      entity_id: media_player.sonoskeuken
  mode: single

Try this version:

testcombine:
  alias: testCombine
  sequence:
  - variables:
      players: >
        {{ [] + (['media_player.badkamer'] if is_state('input_boolean.bsonosbadkamer', 'on') else []) +
          (['media_player.studeerkamer'] if is_state('input_boolean.bsonosstudeerkamer', 'on') else []) }}
  - condition: "{{ players | count > 0 }}"
  - service: media_player.join
    data:
      group_members: "{{ players }}"
    target:
      entity_id: media_player.sonoskeuken
  mode: single

Many thanks for this example, it is working fine and I learned a lot. One last question. Is it possible to have the first speaker from the list as the enity_id for the target?

1 Like

The value of the variable named players is a list. Lists are indexed starting with zero. So if we want the first item in the list we would use players[0] in a template.

testcombine:
  alias: testCombine
  sequence:
  - variables:
      players: >
        {{ [] + (['media_player.badkamer'] if is_state('input_boolean.bsonosbadkamer', 'on') else []) +
          (['media_player.studeerkamer'] if is_state('input_boolean.bsonosstudeerkamer', 'on') else []) }}
  - condition: "{{ players | count > 0 }}"
  - service: media_player.join
    data:
      group_members: "{{ players }}"
    target:
      entity_id: "{{ players[0] }}"
  mode: single