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.


7 Likes

Hi there. I followed your topic but still getting an error:

Message malformed: Entity {{switch_termostato_fancoil}} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]

blueprint:
 name: Clima estancia
 description: Gestiona suelo radiante y fancoil de una estancia de la vivienda
 domain: automation
 input:
   estancia:
     name: Estancia de la vivienda
     description: Estancia de la vivienda cuyo clima se quiere gestionar automáticamente

trigger_variables:
  estancia: !input estancia
  switch_termostato_fancoil: 'switch.termostato_fancoil_{{ estancia }}'
  switch_termostato_suelo_radiante: 'switch.termostato_suelo_radiante_{{ estancia }}'
  switch_modo_fancoil: 'switch.modo_fancoil_{{ estancia }}'
  switch_modo_suelo: 'switch.modo_suelo_{{ estancia }}'


triggers:
  - trigger: state
    entity_id: 
      - '{{switch_termostato_fancoil}}'
      - '{{switch_termostato_suelo_radiante}}'
      - '{{switch_modo_fancoil}}'
      - '{{switch_modo_suelo}}'
    to: null
    from: null

Where is the problem?

The entity_id field for state triggers does not accept templates.

OK, is there any workaround?

Use a entity selector with multiple that filters to the integration instead of using a string to build the entity_id’s. If you plan to share a blueprint, concatenating strings doesn’t really go over well because people rename things.

I don’t follow you. Do you mean create an input for every entity id needed?

No, create an entity selector for the list of switches that the automation uses.

Just ran into this myself. Is there a workaround when you need to trigger on any state change for multiple entities, but you also need to have other information associated with each entity if it is specified? Ex: I would like to trigger on 1, 2, or 3 entities, but if I list an entity, I also need to list a value that will get used in the rest of the automation for that specific entity. I don’t believe the selector would work in this case because order is important to be able to associate the extra values.