Setting variable and reuse result

I try to write a script to mimic the behaviour of a shutter control button to save reimplement it for all my buttons.
Press once down the shutters will close. press again stop the shutters. same for up and open.
The problem I have is, that the variables scope arn’t global.

So following script can’t work.
Is there a way around it by either reading the value of the variable with the name in “var_direction_state” or by some kind of variable with a global scope?

alias: Rollo open-close-stop
description: Toggle between a direction and stop
fields:
  cover_id:
    name: Cover
    required: true
    selector:
      entity:
        domain: cover
  direction:
    name: Direction
    required: true
    selector:
      select:
        options:
          - label: Open
            value: open
          - label: Close
            value: close
variables:
  open_direction_state: "opening"
  open_direction_service: "cover.open_cover"
  close_direction_state: "closing"
  close_direction_service: "cover.close_cover"
  var_direction_state: "{{ direction }}_direction_state"
  var_direction_service: "{{ direction }}_direction_service"
  direction_state: "opening"
  direction_service: "cover.open_cover"
sequence:
  - choose:
      - conditions: "{{ direction == 'close' }}"
        sequence:
          - variables:
              direction_state: "closing"
              direction_service: "cover.close_cover"
  - choose:
    - conditions: "{{ states(cover_id) == direction_state }}"
      sequence:
        - service: cover.stop_cover
          data: {}
          target:
            entity_id: "{{ cover_id }}"
    default:
      - service: "{{ direction_service }}"
        data: {}
        target:
          entity_id: "{{ cover_id }}"

Remove this:

  - choose:
      - conditions: "{{ direction == 'close' }}"
        sequence:
          - variables:
              direction_state: "closing"
              direction_service: "cover.close_cover"

Use a template in variables to set the correct value.

  direction_state: "{{ iif(direction == 'close', 'closing', 'opening') }}"
  direction_service:  "cover.{{ direction }}_cover"

Thank you, now the script is elegant and short.

alias: Rollo open-close-stop
description: Toggle between a direction and stop
fields:
  cover_id:
    name: Cover
    required: true
    selector:
      entity:
        domain: cover
  direction:
    name: Direction
    required: true
    selector:
      select:
        options:
          - label: Open
            value: open
          - label: Close
            value: close
variables:
  direction_state: "{{ iif(direction == 'close', 'closing', 'opening') }}"
  direction_service: cover.{{ direction }}_cover
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states(cover_id) == direction_state }}"
        sequence:
          - service: cover.stop_cover
            data: {}
            target:
              entity_id: "{{ cover_id }}"
    default:
      - service: "{{ direction_service }}"
        data: {}
        target:
          entity_id: "{{ cover_id }}"
mode: parallel
max: 999
icon: mdi:window-shutter-auto