Manupulating variables in scripts and automations

It is possible to set local variables. However, it seems these variables are static only or there is something special with their scope. See the following example where I introduced the variable currentcycle. The helper is set to 2 in the first loop cycle, but stays there. I expected it to count to 3. Am I a complete noob or what am I missing?

alias: A VARIABLE TEST
description: Using a local script variable and show it in a helper
variables:
  currentcycle: 0
sequence:
  - repeat:
      count: '3'
      sequence:
        - service: variable.set_variable
          data:
            variable: helper_current_cycle
            value: '{% set currentcycle =+ 1 %}{{ currentcycle }}'
mode: single

You called this twice in the loop. The second time with no data:

I am sorry, that was a copy paste mistake for the example, but not the cause of this issue. I removed that with the same result. here is the debug output. Notice that the helper value stays 1

Iteration 1
Executed: 13 mei 2022 16:32:03
Result:

params:
  domain: variable
  service: set_variable
  service_data:
    variable: helper_current_cycle
    value: 1
  target: {}
running_script: false
limit: 10

Iteration 2
Executed: 13 mei 2022 16:32:03
Result:

params:
  domain: variable
  service: set_variable
  service_data:
    variable: helper_current_cycle
    value: 1
  target: {}
running_script: false
limit: 10

Iteration 3
Executed: 13 mei 2022 16:32:03
Result:

params:
  domain: variable
  service: set_variable
  service_data:
    variable: helper_current_cycle
    value: 1
  target: {}
running_script: false
limit: 10

The reason it behaves the way it does is due to the use of the += operator. The variable always gets reset.

Compare the results of using += and + in the Template Editor:

It’s the loop scope on variables inside automations. Which have similiar limitations to jinja. They aren’t the same as you can get around it in jinja with namespace. In automations, you can get around it with a counter entity.

EDIT: Just to clarify further here. The real limitation is that you cannot alter an existing variable in the current namespace of an automation.

What are you trying to do? I’ve yet to come across a need to store the current index in a variable. I believe what you’re doing can be simplified or altered. Can you explain that instead of this question? Why do you need to count the cycle? Are you aware that repeat.index is a variable that exists during looping that contains the index?

FWIW, I wasn’t even aware that the Jinja2 interpreter supported the += operator (the sandboxed python_script integration doesn’t) so I tested it and discovered that it doesn’t quite behave as one would expect; it simply re-initialized the variable each time, even if the variable already exists and contains a value. In contrast, the + operator dutifully incremented the variable.

1 Like

I was experimenting with the scope and behaviour of variables. This loop example came up because I had the need to so something with the count vaule inside the repeat. The documentation doesn’t mention the availability of the count in a repeat count, and I don’t want to create a helper, just because I need the scope in a loop somewhere.

Variable scope is covered here, the example shown covers your exact use case and it shows that it’s not possible.

Repeat loop variables are covered here:

So how can I display a variable in my dashboard? And how can I make an input_number helper to equal a variable in a script? I’m trying to monitor how many times a door opens and display it as a input_number so I can manually change it if I want.

Preferably using a script to change the number.