Entity Templating

Hi all,

I’m working on some scripts and I’m trying to use templates with the entity_id. Something like this:

data:
    media_content_type: "video"
    entity_id:  >
      {%- if a == "Player2" -%} 
        media_player.player1
      {%- elif a == "Player2" -%}
        media_player.player2

so I can action something to happen in the player i want.

is this possible?

I’m getting error as is the entity_id doesn’t support templates.

Ideas?

Did you try data_template?

Cheers

Hmmm… No. I think I have not.

Do you have an example handy that I can review?? Not in my PC right now but I can start thinking about it … :slight_smile:

What is “a” in this scenario?

You already have the template down, but since I don’t know what “a” is, I replaced it with checking the state of a sensor, which can be done with any entity using is_state:

{% if is_state("sensor.my_sensor", "Player 2") %}

or as a direct comparison like you were doing with “a”:

{% if states.sensor.my_sensor.state == "Player 2" %}

So in your script, you just have to make data into data_template

script:
  my_script:
    sequence:
      service: media_player.play_media
      data_template:
        media_content_type: "video"
        entity_id: >
          {%- if is_state("sensor.media_player", "Player1") -%} 
            media_player.player1
          {%- elif is_state("sensor.media_player", "Player2") -%} 
            media_player.player2

An input_selec component …

ok, then that would look like this:

script:
  my_script:
    sequence:
      service: media_player.play_media
      data_template:
        media_content_type: "video"
        entity_id: >
          {%- if is_state("input_select.media_player", "Player1") -%} 
            media_player.player1
          {%- elif is_state("input_select.media_player", "Player2") -%} 
            media_player.player2
1 Like

I’m testing this right now …

Quick question: What’s the difference between
is_state("input_select.media_player", "Player1")
and
states.input_select.input_select.media_player.state == "LivingRoom"

???

Working like a charm … thank you sir!!!

Sorry for the late response. Jbardi seems to have you helped out here. There is no real difference between the two statements mentioned by you except for the first one being shorter and imho easier to read.

Cheers