Error while processing template (as trigger)

Hey folks,

I have this blueprint (excerpt)

blueprint:
  name: Stop heating with AC (boost)
  description: Shuts of the AC when temperature has been reached (while general heating is on)
  domain: automation
  input:
    fbh_sensor:
      name: FBH Knob
      description: The knob that does the trigger and reads the temperature.
      selector:
        entity:
          domain: climate
[...]
variables:
  template_fbh_sensor: !input fbh_sensor

and I have a trigger:

trigger:
[...]
  - platform: template
    value_template: >-
      {{ (state_attr(template_fbh_sensor, 'current_temperature') | int(0)) >=
         (state_attr(template_fbh_sensor, 'temperature') | int(0))
      }}
[...]

But HA throws me this error:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 660, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 541, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'template_fbh_sensor' is undefined
2023-04-11 12:00:19.298 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'template_fbh_sensor' is undefined when rendering '{{ (state_attr(template_fbh_sensor, 'current_temperature') | int(0)) >=
   (state_attr(template_fbh_sensor, 'temperature') | int(0))
}}'

So I am setting fbh_sensor with a selector, containing a room thermostat.
I then set template_fbh_sensor with the contents of fbh_sensor.

That same variable is then not available when I use it as trigger?
What am I doing wrong here?

-Chris.

variables are resolved after triggers, not before. If you want to set something that a trigger will use, you have to use trigger_variables. Keep in mind trigger variables are limited and do not have access to HA or the state machine. Meaning they only use basic jinja. No states(), or any function/filter like that.

trigger_variables:
  template_fbh_sensor: !input fbh_sensor
1 Like