How can i Set variable in a loop repeat until , inside an automation for find increase temperature in a room

i have a problem with set the variable a (i don’t write anything in configuration.yaml i have write all code inside only in automation.yaml) with this my automation to find increase temperature

can you help me ? i don’ t know how i can resolve …

- alias: Controllo_incremento_temperatura301
  trigger:
  - platform: state
    entity_id: switch.sonoff_XXXX
    from: "off"
    to: "on"
  action:
  - variables:
      start_temperature301: "{{ states('sensor.sonoff_XXXX_temperature') }}"
      #counter: 0
  - alias: repeat until 
    repeat:
      sequence:
        - delay: 00:00:03
        - variables:
            new_temperature301: "{{ states('sensor.sonoff_XXXX_temperature') }}"
        - choose:
          - conditions:
              - condition: template
                value_template: "{{ (float(start_temperature301) + 0.1) < float(new_temperature301) }}"
            sequence:
              - variables:
                  a: 2   # this don't work
              - service: notify.mail
                data:
                  title: "Controllo Istantaneo temperatura 301"
                  message: "Incremento di 0.1 gradi rilevato"
          default:
            - variables: 
                a: 1  # this don't work
      until:
        condition:
          - or:
            - condition: template
              value_template: "{{ a != 2 }}"
            - condition: template
              value_template: "{{ repeat.index > 20 }}"
  - service: notify.mail
    data:
      title: "STATO CALDAIA ENTRO 15 MIN DA ACCENSIONE CALDO 301"
      message: >
        {% if a == 1 %}
          ALERT_CALDAIA NON FUNZIONANTE ENTRO 15 MIN_DOPO 301 CALDO ON
        {% elif a == 2 %}
          OK_CALDAIA FUNZIONANTE ENTRO 15 MIN_DOPO 301 CALDO ON
        {% else %}
          PROBLEMA NEL CODICE
        {% endif %}

how can i set variable a to 1 or 2
a=1 or a=2 ???
is the only tings that don’t work in this automation …

Reference: Scope of variables

1 Like

I fixed it without using variables, but it still doesn’t work

- alias: Controllo_incremento_temperatura301
  trigger:
  - platform: state
    entity_id: switch.sonoff_XXXX
    from: "off"
    to: "on"
  action:
  - service: input_number.set_value
    target:
      entity_id: input_number.start_temperature301
    data:
      value: "{{ states('sensor.sonoff_XXXXX_temperature') }}"
  - service: switch.turn_off
    target:
      entity_id: input_boolean.avar301

  - repeat:
      sequence:
        - delay: 00:00:30
        - service: input_number.set_value
          target:
            entity_id: input_number.new_temperature301
          data:
            value: "{{ states('sensor.sonoff_XXXXX_temperature') }}"
            
        - service: input_number.set_value
          target:
            entity_id: input_number.diff_temperature301
          data:
            value: "{{ states('input_number.new_temperature301')- states('input_number.start_temperature301')  }}"
        
        - choose:
          - conditions:
              - condition: numeric_state
                entity_id: input_number.diff_temperature301
                above: 0.1
              
            sequence:
              - service: switch.turn_on
                target:
                  entity_id: input_boolean.avar301
              - service: notify.mail
                data:
                  title: "Controllo Istantaneo temperatura 301"
                  message: "Incremento di 0.2 gradi rilevato"
          default:
            - service: input_boolean.turn_off
              entity_id: input_boolean.avar301
            - service: notify.mail
              data:
                title: new_temperature301
                message: new_temperature301 = '{{states("input_number.new_temperature301")}}' dopo '{{repeat.index*30}}' secondi = '{{(repeat.index*30)/60}}' minuti, l'incremento è stato di "{{ float(states('input_number.new_temperature301'))- float(states('input_number.start_temperature301'))  }}" gradi
      
      until:
        condition:
          - or:
            - condition: state
              entity_id: input_boolean.avar301
              state: 'on'
            - condition: template
              value_template: "{{ repeat.index > 15 }}"
  - service: notify.mail
    data:
      title: "STATO CALDAIA ENTRO 15 MIN DA ACCENSIONE CALDO 301"
      message: >
        {% if is_state('input_boolean.avar301','on') %}
          OK_CALDAIA FUNZIONANTE ENTRO 15 MIN_DOPO 301 CALDO ON
        {% elif is_state('input_boolean.avar301','off') %}
          ALERT_CALDAIA NON FUNZIONANTE ENTRO 15 MIN_DOPO 301 CALDO ON
        {% else %}
          PROBLEMA NEL CODICE
        {% endif %}