MQTT Trigger - use template in topic


I am trying to make a blueprint to configure my Flic buttons. The buttons send MQTT messages on each click, with topic flic/[flic-name]/action, the payload contains the type of clic (single, double, hold).

The Flic buttons are setup as devices under the MQTT integration. The device is one of my blueprint inputs.
My trigger is designed this way :

  flic_button: !input flic_button
  flic_name: "{{device_attr(flic_button, 'name')}}"
  - platform: mqtt
    topic: "flic/{{ flic_name }}/action"
    payload: click
    id: click

It works if I replace {{ flic_name }} by the actual value. If I print the flic/{{ flic_name }}/action value in a notification I can see it has the correct value, but the automation doesn’t trigger using the variable!
I think it may be due to this sentence in the documentation :

The topic and payload templates are only evaluated when setting up the trigger, they will not be re-evaluated for every incoming MQTT message.

But for what I understand, it should be ok, the flic_name depends on the input device, it can be set only when the automation is created.

So why doesn’t it work ? How could I make it ?

Thanks in advance for your help!

That variable needs to be available before the trigger, so it needs to be a trigger_variable. The other variables are rendered after the trigger, Trigger_variables and !inputs are the only things rendered before triggers.

These are limited templates. I’m not certain this will still work, but worth a try.

I believe this is the correct documentation link for Trigger variables (as applicable for this case). You want the second variant.

Trigger variables

There are two different types of variables available for triggers. Both work like script level variables.

The first variant allows you to define variables that will be set when the trigger fires. The variables will be able to use templates and have access to the trigger variable.

The second variant is setting variables that are available when attaching a trigger when the trigger can contain templated values. These are defined using the trigger_variables key at an automation level. These variables can only contain limited templates. The triggers will not re-apply if the value of the template changes. Trigger variables are a feature meant to support using blueprint inputs in triggers.

    my_event: example_event
    - platform: event
      # Able to use `trigger_variables`
      event_type: "{{ my_event }}"
      # These variables are evaluated and set when this trigger is triggered
        name: "{{ }}"

Thanks a lot, now I understand better !

So I must use trigger_template, but I cannot use device_attr() because it is not supported by “limited templates”.

I’d like to choose the device in the blueprint input, and the trigger depends on the device’s name.
Could the input be directly the device’s name ? Or any other workaround?

Hi @FunkyKwak, have you ever found the answer to your problem?

Do you have a question?
I believe we provided an answer to their question.