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?
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:
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.