Defining variables inside if-then clause

This must be simple syntax thing, but I can’t define a variable inside if-then clause.

For example the following automation should return if_true for the test_variable but it returns the default value. What I’m doing wrong?

alias: New Automation
description: ""
trigger: []
condition: []
action:
  - if:
      - condition: template
        value_template: "{{ 1 == 1 }}"
    then:
      - variables:
          test_variable: if_true
    else:
      - variables:
          test_variable: if_false
mode: single
variables:
  test_variable: default

1 Like

You overlooked the fact that variables have scope.

The scope of the variable named test_variable defined inside the if-then-else is limited to the if-then-else.

2 Likes

Is there a way to access automation-global scopes variables? Perhaps global.variables.whatever?

Can’t access what doesn’t exist.

How sad. Would there be a way to attach/change variables to the scope of the automation itself? Like automation.variables.bla ?

1 Like

Variables

1 Like

I had a similar problem. I found the solution puting the “if statement” in the variable definitions. Check it out here:

1 Like