Trigger_Variables - variables that are available when attaching a trigger

First, this is not meant to be a replacement for the section in the HA Docs about this.
Please read and refer to that as the official word on this subject.
Automation Trigger - Home Assistant.

When I mention Triggers here I am referring to the main key trigger: used to activate an automation, and not necessarily the triggers found in key action: statements.

What the Docs don’t say is the reason that there are 2 kinds of variables. Regular variables used in Automations and the ‘trigger’ variables that are the result of a trigger happening do not exist before there is an actual trigger. If you try to plug them into a template and attempt to use them as part of your trigger logic, they are actually ‘undefined’ and not changing any time soon.

This WILL NOT work:

blueprint:
  input:
    input_alarm_trigger:

variables:
  alarm_trigger: !input input_alarm_trigger
  printer_topic: !input base_topic
  p_cmd_t: '{{ printer_topic }}BluePrint/presentation'

trigger:
  - platform: template
    value_template: "{{ states(alarm_trigger) == true }}"
  - platform: mqtt
    topic: '{{ p_cmd_t }}'
    payload: "PRESS"

Errors such as this will be seen:

Template variable warning: 'p_cmd_t' is undefined when rendering '{{ p_cmd_t }}'
Template variable warning: 'alarm_trigger' is undefined when rendering '{{ states(alarm_trigger) }}'

Trigger Variables created using the key trigger_variables: are different. They actually do exist before the trigger.

This WILL work:

blueprint:
  input:
    input_alarm_trigger:

trigger_variables:
  alarm_trigger: !input input_alarm_trigger
  printer_topic: !input base_topic
  p_cmd_t: '{{ printer_topic }}BluePrint/presentation'

trigger:
  - platform: template
    value_template: "{{ states(alarm_trigger) == true }}"
  - platform: mqtt
    topic: '{{ p_cmd_t }}'
    payload: "PRESS"

Because these exist before the trigger happens, that means that all the templates and values you use to generate trigger_variables have to exist before the trigger as well. So hard coded values, !input’s, and math and string type functions that are are in the limited_templates availability list CAN be used. You CANNOT use regular variables or template functions that are not available on that list.

Example of using a function not on the limited_templates availability list:

Errors such as these will be seen

Another thing I see that seems to confuse people ie where to place the key variables: and the key template_variables: in the automation. It doesn’t matter. Before the trigger, after the trigger, just doesn’t matter. Putting one of these in the YAML before the trigger does not mean that it will change the order of execution. The YAML is read into the interpreter and the code is executed from there. Place them in any order you like.

In the end if you cannot template your information to get the trigger you need because something is not available before the template, I suggest triggering more generally, meaning trigger on more cases and then use condition statements to narrow that down to get your result. During conditions all variables, template_variables, and template functions are available to help screen properly for your actions.


The Home Assistant Cookbook - Index.

4 Likes