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: []
  - if:
      - condition: template
        value_template: "{{ 1 == 1 }}"
      - variables:
          test_variable: if_true
      - variables:
          test_variable: if_false
mode: single
  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.


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


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