Setting script variable doesn't work?

Hi,

I’m trying to dynamically set a script variable based on choose conditions, I’ve tried it in two different ways but neither will actually set the script variable. Anyone any pointers? I really don’t want to copy and past the same bit of logic all over my choose branches. The script is a condensed version of the real one, it might not even work but hopefully does demonstrate what I’m trying to achieve:

variables:

    # Method 1

    test_variable: 100

  sequence:

    - variables:

        # Method 2

        test_variable: 100

  

    - choose:

        - conditions: "{{ true }}"

          sequence:

            variables:

                test_variable: 50

    - choose:

        - conditions: "{{ test_variable < 75 }}"

          sequence:

            - service: cover.set_cover_position

              target:

                entity_id: cover.test_cover

              data:

                position: "{{ test_variable }}"

You’re running into a variable scope issue. Both of those chooses pull from the variable defined above and do not impact eachother. Put your condition in the variable in your main sequence, not a separate choose.

Hi!

And thanks for the answer, I suspected it was a scoping issue but just to be clear, I seperated the two scripts. Because if I understand you correctly method 2 (below) should work?

Method 1:

variables:
    test_variable: 100

  sequence:
    - choose:
        - conditions: "{{ true }}"
          sequence:
            variables:
                test_variable: 50

    - choose:
        - conditions: "{{ test_variable < 75 }}"
          sequence:
            - service: cover.set_cover_position
              target:
                entity_id: cover.test_cover
              data:
                position: "{{ test_variable }}"

Method 2:

  sequence:
    - variables:
        test_variable: 100  

    - choose:
        - conditions: "{{ true }}"
          sequence:
            variables:
                test_variable: 50

    - choose:
        - conditions: "{{ test_variable < 75 }}"
          sequence:
            - service: cover.set_cover_position
              target:
                entity_id: cover.test_cover
              data:
                position: "{{ test_variable }}"

@petro sorry, forgot to tag you! Hope you have some insights for me :slight_smile: