How to properly set variables using templates in YAML?

I am building a script that, when called, will set a light scene depending on certain conditions. I am using a “choose” action to evaluate different conditions of various complexity, and for each condition I want to set a defined variable to a certain scene name. After the choose, I will call a service to apply that variable as a scene name.
Pseudocode:

define var scene_chosen

if x
scene_chosen = a
else if y 
scene_chosen = b
else
scene_chosen = c

scene.apply(scene_chosen)

My script YAML is as follows:

scene_lights_on:
  alias: Turn on lights
  description: Turn on lights to calculated preset
  variables:
    chosen_preset: n/a
  sequence:
    - choose:
        - conditions: #some template condition shorthand notation here
          sequence: "{% set chosen_preset = 'scene1' %}"
        - conditions: #some template condition shorthand notation here
          sequence: "{% set chosen_preset = 'scene2' %}"
    - service: scene.apply
      #etc...
  mode: single

The issue I am having is whenever I use a sequence like the above to set the variable, my script disappears from the UI, which leads me to believe something is wrong with my notation. If my {% %} Jinja statement is not the right way to do this, can anyone help fix it? Thanks.

How are you defining the scene? Are you creating it on the fly or do you have pre-defined scenes with names “scene.scene1” & “scene.scene2”?

why do you need to create a variable with the scene name in it? Can’t you just call the scene directly in the sequence depending on the conditions?

but generally the sequence of a script or choose statement (which are technically the same thing) needs a series of service calls.

you can’t just put a template under the sequence key like that.

I have predefined scenes already with names, yes. I thought that doing the logic to assign the variable and then have one scene call at the end would be cleaner and shorter. Can you think of another way to structure that approach? There is no reason I can’t do it with a scene call for each condition, other than I thought it would make the YAML longer and more complex to understand.

  1. This is invalid YAML syntax. sequence expects a list of actions, not a solitary string containing a template.
sequence: "{% set chosen_preset = 'scene1' %}"
  1. The template you created attempts to set the value of a Jinja2 variable called chosen_preset which is not the same as the script variable of the same name you defined at the beginning of the script.

  2. The script variable you defined has global scope but it’s value cannot be changed within a choose's sequence because anything defined within a sequence is local to the sequence.

Thanks for this info. Clearly still need to better understand how variables and templates work. Can you think of a workaround for what I am trying to accomplish, besides doing a scene call in each choose?

From the minimal information you have given I can’t tell why you need to use a Choose action at all…

scene_lights_on:
  alias: Turn on lights
  description: Turn on lights to calculated preset
  variables:
    chosen_preset: |
      {% if condition 1 %} scene1 
      {% elif condition 2 %} scene2 
      {% elif condition 3 %} scene3
      {% endif %}
  sequence:
    - service: scene.turn_on
    target:
      entity_id:  scene.{{ chosen_preset }}
  mode: single

Ok this looks promising, thanks! I’ll try using this format.

What would be best is if you could explain why you have designed the script in this manner. For example, why isn’t it an automation that is triggered when the situation requires?

I have a light strip running WLED. I want to be able to call a script whenever I want to turn on the strip, and have the strip choose a preset to turn on to depending on various factors, such as if my hockey team is playing (state condition), or if it is a certain holiday (state condition/helper value), or the time of year (likely a template condition), etc (these would be set hierarchically in the choose in order of precendence). I would want to trigger this from other automations or manually from a dashboard button. Another aspect is that using snapshot_entites does not work well for WLED strips as there are a number of parameters that do not get captured/restored properly, so I’m also setting an input_select helper to keep track of the most recently applied preset, so that if I want to briefly “manually” change the light state (i.e. without this script), I can easily revert it back to previous state. This helper’s value would also be set based on the {{ chosen_scene }} variable as well. Hopefully that provides enough context.