Changing values of variables in automations before using it

Hello,

I have the following automation that does not work correctly:

alias: Heating & Cooing
description: ""
trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
  - platform: state
    entity_id: climate.living_room_thermostat
  - platform: state
    entity_id: input_boolean.heating_cooling_auto
    to: "on"
  - platform: state
    entity_id:
      - schedule.heating_active_time
      - schedule.cooling_active_time
  - platform: state
    entity_id:
      - timer.heating_cooling_boost_timer
condition:
  - condition: state
    entity_id: input_boolean.heating_cooling_auto
    state: "on"
  - condition: or
    conditions:
      - condition: state
        entity_id: climate.living_room_thermostat
        state: heat
      - condition: state
        entity_id: climate.living_room_thermostat
        state: cool
action:
  - variables:
      temperature: 21
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: alarm_control_panel.alarmo
                state: armed_away
              - condition: state
                entity_id: alarm_control_panel.alarmo
                state: armed_vacation
              - condition: and
                conditions:
                  - condition: state
                    entity_id: climate.living_room_thermostat
                    state: heat
                  - condition: state
                    entity_id: schedule.heating_active_time
                    state: "off"
              - condition: and
                conditions:
                  - condition: state
                    entity_id: climate.living_room_thermostat
                    state: cool
                  - condition: state
                    entity_id: schedule.cooling_active_time
                    state: "off"
        sequence:
          - variables:
              temperature: >-
                {{ states("input_number.heating_cooling_eco_temperature") |
                float }}
      - conditions:
          - condition: state
            entity_id: climate.living_room_thermostat
            state: heat
          - condition: template
            value_template: "{{ states('sensor.home_hours_of_sun_today') | float > 5 }}"
        sequence:
          - variables:
              temperature: >-
                {{ states("input_number.heating_cooling_active_temperature") |
                float - 1 }}
    default:
      - variables:
          temperature: >-
            {{ states("input_number.heating_cooling_active_temperature") | float
            }}
  - if:
      - condition: state
        entity_id: timer.heating_cooling_boost_timer
        state: active
    then:
      - choose:
          - conditions:
              - condition: state
                entity_id: climate.living_room_thermostat
                state: heat
            sequence:
              - variables:
                  temperature: "{{ temperature | float + 1.5 }}"
          - conditions:
              - condition: state
                entity_id: climate.living_room_thermostat
                state: cool
            sequence:
              - variables:
                  temperature: "{{ temperature | float - 1.5 }}"
  - service: climate.set_temperature
    data:
      temperature: "{{  temperature | float }}"
    target:
      entity_id: climate.living_room_thermostat
mode: single

The Problem is that the variable “temperature” is changed throughout the automation but at the end it sets the Thermostat to the value that is defined in the beginning and not the value that has been set in the different choose blocks.

Can someone point me in the right direction?

It’s due to the variable’s scope.

The temperature variable you define within choose is independent of the temperature variable defined outside of choose. They may have the same name but they’re independent; the one defined inside choose cannot affect the value of the one defined outside of choose.

Scoping rules apply to variables defined within choose, if, repeat, etc.

Reference

Scope of variables

You need to compute the temperature value using a Jinja template, directly in the temperature option of the climate.set_temperature service call. In other words, you need to convert all the decision-making logic of your choose statement into a Jinja template.

Is it not possible to change the existing variable?

No.

Use a template.

OK, Thank you!