Trigger_variables used in state trigger

I am working on a blueprint where I create variables using tempates. Since I cannot use these variables in a trigger, I have created “template_variables” so that I can use them in the trigger. When I now use the variable for “entity_id” it is not working, as it says that “{{ … }}” is not an enity_id.

trigger_variables:
  devices: !input smoke_detectors
  smoke_sensors: >
    {% set data = namespace(alarm_entity=[]) %}
    {% set temp = [] %}
    {% for device in devices %}
      {% set temp = device_entities(device) | select('match', 'binary_sensor.*_smoke') | list | join  %}
      {%- set data.alarm_entity = data.alarm_entity + [temp] -%}
    {% endfor %}
    {{ data.alarm_entity }} 
trigger:
  - platform: state
    entity_id: "{{smoke_sensors}}"
    to: "on"

I could use a template trigger, but then I lose the information of which sensor did acutally trigger the alarm - or is there a possibility to save that in another variable for later use?

1 Like
trigger_variables:
  devices: !input smoke_detectors
  smoke_sensors: >
    {% set data = namespace(alarm_entity=[]) %}
    {% set temp = [] %}
    {% for device in devices %}
      {% set temp = device_entities(device) | select('match', 'binary_sensor.*_smoke') | list | join  %}
      {%- set data.alarm_entity = data.alarm_entity + [temp] -%}
    {% endfor %}
    {{ data.alarm_entity }} 
trigger:
  - platform: template
    value_template: "{{ smoke_sensors == 'on' }}"

you need to use

  - platform: template
    value_template: 

but I haven’t got this to work E.g. same problem

The entity_id option of a State Trigger doesn’t support templates.

trigger_variables only support limited templates.

1 Like

This is marked as the solution, but what is the solution?

You’re just saying that the state trigger doesn’t support templating for entity_id, but how do I get this to work?

Or is the “solution” the knowledge that this just can’t work?

There are 2 problems
123 mentions both problems on a high level. Def goes specific on the trigger one.

If your question is how do you make this kind of scenario work, this might help.

Since trigger_variables have to generate before any other code is run in the blueprint, it does not have access to a lot of things. That is why it is limited. Trigger_variables were supposedly created to allow !inputs to be used as triggers in blueprints, but have expanded slightly since to about their limit. However it is limited to mostly constant type things that do not change, like !inputs.

Your solution is to look at your problem differently. A common way to solve this is to trigger on something broader/more general, and use the full template set available in conditions to only run your code if your specific thing is true.

Yeah, the knowledge that it does not work.