'trigger' is undefined in script called from automation

Hey all,

Goal

I have an automation set up that gets executed on an incoming MQTT message and should then in turn execute a script. That script should then do different things depending on the MQTT topic.
The documentation for scripts says:

When the script is executed within an automation the trigger variable is available. See Available-Trigger-Data.

But when the script gets executed the if condition in the script fails.

Automation

- id: "1668628839844"
  alias: Receive Target Temperature
  description: ""
  trigger:
    - platform: mqtt
      topic: 02/00002F71/+/V/A0
  condition: []
  action:
    service: script.update_target_temperatur_slider
  mode: parallel
  max: 50

Script

update_target_temperatur_slider:
  alias: Update Target Temperatur Slider
  sequence:
    # Vincent
    - if:
        - condition: template
          value_template: '{% if trigger.topic[12:24] == "D43D3914C9DE" %}
            {%endif%}'
      then:
        - service: input_number.set_value
          data:
            value_template: "{{  int(trigger.payload[1:],base=16)/2 }}"
          target:
            entity_id: input_number.temp_vinc_2
#
#
# more conditions for different topics

Problem

When the automation execute the script fails with the following:

Error: In 'template' condition: UndefinedError: 'trigger' is undefined

This should work according to the documentation above, unless Iā€™m misunderstanding this.

Anyone have an idea on how to make this work?
Thanks.

Have you tried Passing the Variables?

- id: "1668628839844"
  alias: Receive Target Temperature
  description: ""
  trigger:
    - platform: mqtt
      topic: 02/00002F71/+/V/A0
  condition: []
  action:
    service: script.update_target_temperatur_slider
    data:
      topic: '{{ trigger.topic }}'
      payload: '{{ trigger.payload }}'
  mode: parallel
  max: 50

Script

update_target_temperatur_slider:
  alias: Update Target Temperatur Slider
  fields:
    topic:
      name: Topic
    payload:
      name: Payload
  sequence:
    # Vincent
    - if:
        - condition: template
          value_template: '{% if topic[12:24] == "D43D3914C9DE" %}
            {%endif%}'
      then:
        - service: input_number.set_value
          data:
            value_template: "{{  int(payload[1:],base=16)/2 }}"
          target:
            entity_id: input_number.temp_vinc_2
#
#
# more conditions for different topics
1 Like

Thank you, that seems to work. Somehow I seemed to miss that in the docs.