Simple help with an basic YAML script

Hello all,
Can you please help me with a simple YAML script? I am beginner with YAML and I need a reference on this:

sequence:
  - variables:
      output_text: this is a text
  - if:
      - condition: state
        entity_id: binary_sensor.boolean
        state: "off"
    then:
       output_text = output_text + added text <===This is the part that I am struggling

Thank you for your help!

sequence:
  - variables:
      output_text: this is a text
  - if:
      - condition: state
        entity_id: binary_sensor.boolean
        state: "off"
    then:
      - variables:
          output_text: "{{ output_text }} added text"

Be advised that due to scoping rules, the second output_text variable is not the first output_text variable. It has the same name but it’s a different variable whose scope is limited to within the if then statement.

Here’s what I mean:

sequence:
  - variables:
      output_text: this is a text
# Value of output_text outside of the `if then` is "this is a text"
  - if:
      - condition: state
        entity_id: binary_sensor.boolean
        state: "off"
    then:
      - variables:
          output_text: "{{ output_text }} added text"
      # Value of output_text inside the `if then` is "this is a text added text"
# Value of output_text outside of the `if then` is "this is a text"

I would need to know more about your intended application in order to make alternative suggestions. For example, here’s another way to make decisions about a variable’s value:

sequence:
  - variables:
      output_text: >
        {% set x = 'this is a text' %}
        {{ iif(is_state('binary_sensor.boolean', 'off'), x, x ~ 'added text') }}

EDIT

Correction. Added missing closing parenthesis.

2 Likes

Thank you for your response! I am trying to make a “Home health check”.
I wanna start by a notification “Health check starting” and then the rest of the readings.
I know that I can just make partial notifications instead of adding them all up and reading it.
I just wanted to make it for sake of being able to make it. To learn some basics more.
So from what I gathered on the document which you showed me, I need to keep reminding myself that YAML is actually a mark up language, not an full blown programming language.
thank you very much

  1. Home Assistant uses YAML to create its own scripting language.

  2. The text shown between {{ }} and {% %} in the example I posted above is Jinja2 (a templating language).

That’s achieved via templating.

1 Like