Jinja2 Conditionals w/Script Variables

Hello,

I am trying to set a variable within a YAML script that will be used in multiple blocks throughout the script.

What I have below works but it is ugly and it doesn’t support more complex conditionals.

office_lighting:
  alias: "SC - Office Lighting"
  variables:
    lightingMode: '{{ "day" if state_attr("sun.sun", "elevation") > 0 else "night" }}'
  sequence:
    - service: scene.turn_on
      data_template:
        entity_id: >
          {# Day Lighting -#}
          {% if lightingMode == 'day' -%} scene.office_lighting_day
          ...

This does not work:

office_lighting:
  alias: "SC - Office Lighting"
  variables:
    lightingMode: >
      {% if state_attr("sun.sun", "elevation") >= 0 -%}
      'day'
      {% else -%}
      'night'
      {% endif -%}
  sequence:
    - service: scene.turn_on
      data_template:
        entity_id: >
          {# Day Lighting -#}
          {% if lightingMode == 'day' -%} scene.office_lighting_day
          ...

I’ve been playing around with trying to get the more complex and cleaner if statement to work. I can’t find much documentation or examples on how to do this - most things are about passing variables from automatons.

Any recommendations on how I can set variables that are scoped to the entire script and can have the value set on a normal if, elif, else statement?

The second example fails only because you wrapped the words day and night in quotation marks. Otherwise, the only difference between the two examples is the first uses as inline if and the second uses the traditional format.

Variables are accessible to all templates within the script.

office_lighting:
  alias: "SC - Office Lighting"
  sequence:
    - variables:
        lightingMode: >
          {% if state_attr("sun.sun", "elevation") >= 0 %} day
          {% else %} night
          {% endif %}
    - service: scene.turn_on
      data_template:
        entity_id: >
          {# Day Lighting -#}
          {% if lightingMode == 'day' -%} scene.office_lighting_day
          ...

Thank you for the help.

I’d like to understand and learn from my mistake.

Since I’m doing a string comparison in the block below I assumed I should set the variable as a string but I wasn’t considering how YAML works :roll_eyes:.

In the past, when I use set within scripts I would always use quotes and everything worked as expected. For example,

{% set foo = 'bar' %}

But that is Jinja…

So I believe the mistake I was making was:
I was outputting a folded block scalar with my original if statement into the variable lightingMode so it would already be outputted as a string.

Does this retrospective seem correct?

For the Jinja2 processor, any text outside of {{ }} or {% %}` is handled as a literal string. Text within braces is processed so you must differentiate between variables and literal strings by wrapping the strings in quotation marks. If you do that outside the braces (like in your “does not work” example) the quotation marks become part of the literal string.