How initialize "trigger variable" using a variable, to be used in action

Hello,
I searched in the forum but I was not able to find an answer to my question/problem.
I have an automation with a template trigger, that does some calculations and then evaluate a condition to fire the automation.
I would like to use the result of the calculation (stored in a variable) in the action.
If I manually set the value of the trigger variable, this is passed and made available in the action (I get a Telegram notification with that value).

trigger:
  - platform: template
    value_template: >-
      [...]
    variables:
      threshold: 5
action:
  - service: notify.xxx
    data:
      title: "xxx"
      message: >-
        Value is {{threshold}}

Value is 5

If I try to set it using the variable containing it I don’t get what I expect:

trigger:
  - platform: template
    value_template: >-
      [...]
      {% set test = 5 %}
    variables:
      threshold: {{test}}
action:
  - service: notify.xxx
    data:
      title: "xxx"
      message: >-
        Value is {{threshold}}

and the Telegram message is:

Value is {‘object Object’: None}

What am I missing/doing wrong? Maybe I misunderstood how the trigger variable can be used?
Thanks for any feedback.

Issue 1: {‘object Object’: None} Usually indicates that you didn’t properly enclose a template in quotes or use a multi-line quote designator. In this case it was the template for threshold:

Issue 2: Variables defined in Jinja have local scope, they are not available outside the block they are defined in.

trigger:
  - platform: template
    value_template: >-
      [...]
      {% set test = 5 %}  {# test has value here #}
    variables:
      threshold: "{{test}} {# test is undefined/none here #}"
action:
  - service: notify.xxx
    data:
      title: "xxx"
      message: >-
        Value is {{threshold}} {# Since test is none, threshold is none #}

If you give a more detailed explanation of what you are trying to do we can suggest options for you.

I see… I learned variables have local validity (and also some limitations when loops are involved - I found the workaround of “namespace” to extend the scope to inner loops) but I thought/hoped that a variable defined in the trigger block was available through it all, so also for the “variables”.

What I developed is an automation to get a Telegram notification when the external temperature (from a sensor) approaches (within a threshold, that I define inside a variable) the average temperature of rooms with the window open (which I calculate and store in another variable to be able to perform the comparison with the external temp, considering the threshold, and fire the automation).
The calculations, the trigger and the action work.

What I’d now like to do is to enrich the notification message with the external temperature (not a problem - easy to do) but also the average internal temperature (which I calculate) and the threshold (I statically set) values, both stored inside variables.
Does this make sense?

I thought the function of this version of “trigger variables” was to make values available outside the trigger block, but if they can’t be dynamically set I can’t see its utility.

Any advice?
Thanks

The values of the trigger variables are available outside the trigger block, they have the same scope as any other script variables. They support Limited templates, as such they can have somewhat dynamic value based on their own template. They can even use specific data points from the template used in the trigger. But, you don’t set their value inside the value_template of a template trigger because a Jinja variable is different from a script variable.

Post the actual automation…

alias: Telegram finestre aperte caldo fuori
description: >-
  Questa automazione, quando qualcuno è in casa, ti avvisa se la temperatura
  esterna sale oltre la temperatura media (tolleranza impostabile nello script)
  delle stanze con la finestra aperta e ti suggerisce di chiuderle per restare
  al fresco più a lungo.
trigger:
  - platform: template
    value_template: >-
      {% set climate_entities = [ states.climate.soggiorno,
      states.climate.bagno, states.climate.studio, states.climate.camera ] %}

      {% set binary_sensor_entities =  [ states.binary_sensor.portafinestra,
      states.binary_sensor.finestra_bagno, states.binary_sensor.finestra_studio,
      states.binary_sensor.finestra_camera ] %}

      {% set ait = namespace(average_int_temp = 0.0) %}

      {% set i = namespace(i = 0) %}

      {% set j = namespace(j = 0) %}

      {% set threshold = -1.0 %}

      {% for climate_entity in climate_entities %}
        {% if (binary_sensor_entities[i.i].state == 'on') %}
          {% set ait.average_int_temp = ait.average_int_temp + climate_entity.attributes.current_temperature %}
          {% set j.j = j.j + 1 %}
        {% endif %}
        {% set i.i = i.i + 1 %}
      {% endfor %}

      {% if (j.j > 0) %}
        {% set ait.average_int_temp = ait.average_int_temp / j.j %}
      {% else %}
        {% set ait.average_int_temp = 100.0 %}
      {% endif %} {{
        states('sensor.openweathermap_temperature')|float >= (ait.average_int_temp + threshold)
      }}
    variables:
      computed_int_temp: ait.average_int_temp
      soglia: threshold
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition:
  - condition: state
    entity_id: group.family
    state: home
  - condition: or
    conditions:
      - type: is_open
        condition: device
        device_id: 27ff91e34d6077fd1d9cccf9d7ce6709
        entity_id: binary_sensor.finestra_bagno
        domain: binary_sensor
      - type: is_open
        condition: device
        device_id: 3d85a3388934eea91d2c9c54fc328ea9
        entity_id: binary_sensor.finestra_camera
        domain: binary_sensor
      - type: is_open
        condition: device
        device_id: e8d3b63f5f7b5beb039b5a4777c82384
        entity_id: binary_sensor.finestra_studio
        domain: binary_sensor
      - type: is_open
        condition: device
        device_id: aa8278dc30f71d53c6a1f4b55ee47b71
        entity_id: binary_sensor.portafinestra
        domain: binary_sensor
action:
  - service: notify.gggggg
    data:
      title: "*Mi*"
      message: >-
        La temperatura esterna
        {{'('+(states('sensor.openweathermap_temperature')|round(1))|string+'°C)'}}
        è prossima {{'('+soglia|string+'°C)'}} a quella interna
        {{computed_int_temp|string}}. Valuta di chiudere le finestre per
        rimanere al fresco!
mode: single

You’d be better off moving the assignment of the threshold value to a helper and the average_int_temp to a Template sensor.

That’s not a bad idea. A bit more dispersive as not everything is in a single place but probably the easiest and only (for now) way to get this working the way I’d like.

Thanks for the suggestion but especially for explaining this can’t work in the way I expected.