Template variables to use throughout a script

I am passing a string in a variable to a script. I want to parse the string into different variables to be used throughout the script. I can parse the string in a specific action, but then it is not available to other actions. Let’s say command=“play die hard on living room tv”

alias: parse command
sequence:
  - service: notify.phone
    data:
      message: >
        {% set movie = command.split(" on ")[0].split("play ")[1] %} 
        {% set player = command.split(" on ")[1] %}
        movie is {{ movie }}
        player is {{ player }}
mode: single

This parses out movie and player for me, but if I have any other actions they are not available. I only used notify so I can see if it is parsing appropriately. It is, but how can I do this templating so it is available throughout my entire script?

Use variables in your script

I am struggling with getting the syntax correct.

- variables:
  movie: {% set movies = command.split(" on ")[0].split("play ")[1] %}

doesn’t work

- variables:
  movie: {{ command.split(" on ")[0].split("play ")[1] }}

doesn’t work.

Indentation is wrong and yiu don’t need the set command.
Try this:

- variables:
    movie: '{{ command.split(" on ")[0].split("play ")[1] }}'
1 Like

Thank you so much it was my indentation, I didn’t notice!

Follow up question: is there a way to change the value of a variable in an action and have that change reflected in other actions?

No, you can’t change the variable directly. You can however define a new variable based on the other variable. Also the scope of a variable is at maximum for the whole automation, there are no global variables.

Please show an example of what you want to achieve and I’m sure we’ll be able to help you.

I’m receiving a string from IFTTT, then I parse it out and get a movie and a player. Let’s say I get the player variable set to “kitchen tv”. I now need to set another variable, player_entity to “media_player.kitchen_tv”, based on having received “kitchen tv”. I suppose one way to do it would be to call another script and send it data player_entity: media_player.kitchen_tv and separate it out into another script. In the end I want to be able to call an action onto that entity name, for example to turn the device on:

    sequence:
      - service: media_player.turn_on
        entity_id: {{ player_entity }}

You can create an other variable that uses the player variable

- variables:
    movie: '{{ command.split(" on ")[0].split("play ")[1] }}'
    player: '{{ command.split(" on ")[1] }}'
    player_entity: 'media_player.{{ player.replace(" ", "_") }}'

sequence:
  - service: media_player.turn_on
    entity_id: {{ player_entity }}

or use the player variable directly in the service call

sequence:
  - service: media_player.turn_on
    entity_id: 'media_player.{{ player.replace(" ", "_") }}'
1 Like

home assistant really fights me on this one. If I set the entity to {{ player_entity }} in the gui, it forces me to edit yaml. No big deal, but then when I type it in the yaml, as soon as I enter the last } it immediately changes it to ‘[object Object]’: null. I can save it directly in the scripts.yaml, but then if I check config, it says there’s an error on that line.

What does the error say.

Add quotes to the complete the template.

    entity_id: '{{ player_entity }}'
1 Like

Yes!! Thank you!!

You’re welcome but you should give the Solution tag to Burningstone’s post because he answered your original question first.

1 Like

I didn’t realize I couldn’t mark multiple. It unmarked the previous one I had marked.