Need a conditional entity_id list, please help

Trying to conditionally set an entity_id list, the second one here is creating an issue of not being a valid entity_id:

    - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.intercom_language')|lower}}
          entity_id:
            - >
              {{states('sensor.intercom')}}
            - >
              {% if states('media_player.theboss') not in ['unknown','unavailable'] %}
                media_player.theboss
              {% else %} None
              {% endif %}

the {{states('sensor.intercom')}} passes, because I set a condition in the beginning of the script this is a part of, using:

      - condition: template
        value_template: >
          {{states(states('sensor.intercom')) not in ['unknown','unavailable']}}

which effectively checks if the selected medial_player exists.

however, I cant also check for the second player, which is always the media_player.theboss (a mobile device with browser_mod). If this isnā€™t touched it doesnā€™t exist, so returns None. Which is not a valid entity_id :wink:

How, I only need the tts.google_say part to see the second entity_id if it passes the test

 {% if states('media_player.theboss') not in ['unknown','unavailable'] %}

and otherwise only play the first in the list.

Simply using:

              {% if states('media_player.theboss') not in ['unknown','unavailable'] %}
                media_player.theboss
              {% endif %}

without else clause doesnā€™t guard it either of course.

Ive thought about a ā€˜hackā€™, using the identical media_player twice, but that doesnt feel very adequate :wink:

          entity_id:
            - >
              {{states('sensor.intercom')}}
            - >
              {% if states('media_player.theboss') not in ['unknown','unavailable'] %}
                media_player.theboss
              {% else %} {{states('sensor.intercom')}}
              {% endif %}

this seems to work (probably playing the 2 entity_ids simultaneously, as I hoped) but it really should be playing only the {{states(ā€˜sensor.intercomā€™)}} is that is available, and the media_player.theboss isnā€™t availableā€¦

Havenā€™t tested it, but doesnā€™t this work?

    - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.intercom_language')|lower}}
          entity_id: >
            - {{states('sensor.intercom')}}
            {% if states('media_player.theboss') not in ['unknown','unavailable'] %}
            - media_player.theboss
            {% endif %}

HI, and thanks,

but no this wont work, since the output of the template is no correct list (it is always a string), though the template editor would make you believe :wink:

made a more correct ā€˜hackā€™ for my quest: Ive added it as a separate script, passing the variable:

#added this in the calling script:
      - service: script.text_message_the_boss
        data_template:
          text_message: >
            {{states('input_select.intercom_message')}}

#new called script, with the variable it gets from the caller above
  text_message_the_boss:
    alias: Text message The Boss
    mode: queue
    icon: mdi:text-to-speech
    sequence:
      - condition: template
        value_template: >
          {{states('media_player.theboss') not in ['unknown','unavailable']}}
      - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.intercom_language')|lower}}
          entity_id: media_player.theboss
          message: >
            {{text_message}}

note: the mode: queue isnt yet in the current core scripting, but will be merged from HA 113.

1 Like

I always forget about script variables, good call! :+1:

How about:

    - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.intercom_language')|lower}}
          entity_id: >
            {{ states('sensor.intercom') }}
            {% if states('media_player.theboss') not in ['unknown','unavailable'] %}
              , media_player.theboss
            {% endif %}

Most (if not all) services with an entity_id option accept a comma separated string of entity IDs.

1 Like

ok, forgot about the comma separated list.
will test asap. thanks.
Doesnt this need a guard though? an else clause? Or would this simply continue if the media_player would be unknown/unavailable.

If the ā€œifā€-condition isnā€™t met, it will be skipped, this might work

Of course it all depends on what youā€™re ultimately trying to do. I didnā€™t try to figure that out. :laughing: I just suggested an implementation that included states('sensor.intercom') unconditionally and media_player.theboss only if its state wasnā€™t unknown or unavailable. That seemed to be what you were trying to do with the templates.

1 Like

Guard against what exactly?

Against it (the boss) being unknown or unavailable.

It should not leave the service with an None entity

Do you understand what my suggestion does? It either sets entity_id to

"{{ states('sensor.intercom') }}"

or

"{{ states('sensor.intercom') }}, media_player.theboss"

depending on the state of media_player.theboss. Where do you see it including "None"?

1 Like

of courseā€¦

and, I am happy to report this works fine. so thank you, and I would have marked it ā€˜solvedā€™ if the option would have been there. Has the community software changed that??

is just that I was well conditioned (by you and Petroā€¦) to always have an else clause in templates guarding unexpected outcomes and errors.

Yes, when appropriate. I.e., when an else is needed. It depends on the use. But in this case thereā€™s nothing useful in an else clause. If the expression evaluates to false, the overall template still renders a valid result. But if you want to always have an else, then:

    - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.intercom_language')|lower}}
          entity_id: >
            {% if states('media_player.theboss') not in ['unknown','unavailable'] %}
              {{ states('sensor.intercom') }}, media_player.theboss
            {% else %}
              {{ states('sensor.intercom') }}
            {% endif %}
1 Like

of course!
on both accounts. beautiful!
made my day once again, thank yvm.