How to set a variable inside automation loop?

As previous responses have noted, you are running into a scoping issue.

In automations, YAML variables can be defined multiple places, their scope depends on where they are defined.

trigger_variables:
  var_1: true #script-wide (only Limited Templates and static values allowed)
trigger:
  - platform: template
    value_template: "{{ states('binary_sensor.x') | bool == var_1 }}"
    variables:
      var_2: "{{ false }}" #script-wide but, if you have multiple triggers, this value applies only when this specific trigger initiates the automation
variables:
  var_3: "{{ true }}" #script-wide
condition:
  - variables:
      var_4: "{{ true }}" #local to condition block
action:
  - variables:
      var_5: "{{ true }}" #local to action block
  - choose:
      - conditions: 
          - condition: template
            value_template: "{{ var_2 }}" 
        sequence:
          - variables:
              var_6: "{{ false }}" #local to this Option of this Choose action    

* Variables will be populated with values roughly in the order I have numbered above.

One workaround for variables inside repeat actions is to set the value to an appropriate Helper or Trigger-based template sensor through an action.

Example using OP's sample automation
alias: Benachrichtigung - Fenster geöffnet
description: ""
triggers:
  - type: opened
    device_id: xxx
    entity_id: xxx
    domain: binary_sensor
    trigger: device
conditions: []
actions:
  - action: input_boolean.turn_off
    target:
      entity_id: input_boolean.fenster_notification_running
  - repeat:
      sequence:
        - variables:
            test1: "{{ states('input_boolean.fenster_notification_running')|bool }}"
        - if:
            - condition: or
              conditions:
                - condition: numeric_state
                  entity_id: weather.home
                  attribute: temperature
                  enabled: false
                  above: input_number.contact_sensor_notification_disable_temperature
                - condition: state
                  entity_id: input_boolean.contact_sensor_notification_disable_by_user
                  state: "on"
                  enabled: true
          then: []  #You can't leave this blank and expect the automation to work reliably
          else:
            - device_id: xxx
              domain: mobile_app
              type: notify
              title: "Fenster "
              message: Wurde geöffnet
              enabled: false
            - action: notify.alexa_media_alexa_arbeitszimmer
              data:
                data: {}
                message: >-
                  Wert 1 {# The variable that follows is undefined #}{{contact_sensor_notification_disable_temperature_hint}},
                  Wert 2 {{ test1 }} 
        - action: input_boolean.turn_on
          target:
            entity_id: input_boolean.fenster_notification_running
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
      while:
        - type: is_open
          condition: device
          device_id: xxx
          entity_id: xxx
          domain: binary_sensor
          enabled: true
mode: single
3 Likes