Trigger.id always "unknown" in automation action

Hey all,

I’ve searched the forum and the docs and can’t find what it is I’m doing wrong, so I’m hoping someone here can tell me the obvious thing I’ve missed :flushed:

My setup is HA 2024.9.0 running as a full install on a raspberry pi 4.

What I’m trying to do is include the name of the trigger which started the automation in a notification I send to my phone, but no matter what I try it always reports as “unknown”. I know to expect this if I manually trigger it, but it’s also reporting “unknown” when one of the triggers starts it.

This is what I’ve got at the moment - no errors are reported in the logs.
Sidenote: I know this is overly complex, my next job is to simplify it :slightly_smiling_face:

alias: Detect arriving home v2
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ states('binary_sensor.arriving_on_foot') and
      states('sensor.distance')|float < 1.5 }}
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: Walking
  - platform: template
    value_template: >-
      {{ states('binary_sensor.arriving_by_bike') and
      states('sensor.distance')|float < 6 }}
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: Riding
  - platform: template
    value_template: >-
      {{ states('binary_sensor.arriving_by_car') and
      states('sensor.distance')|float < 35 }}
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: Driving
condition:
  - condition: not
    conditions:
      - condition: zone
        entity_id: person.myname
        zone: zone.home
action:
  - sequence:
      - action: notify.mobile_app_my_s_s22
        metadata: {}
        data_template:
          title: V2 Coming home triggered
          message: >-
            Trigger was {{states('trigger.id')}}. Speed was
            {{states('sensor.speed_towards_home')}} mph and distance was
            {{states('sensor.distance')}} miles.
      - action: input_boolean.turn_on
        target:
          entity_id:
            - input_boolean.coming_home_v2
        data: {}
      - wait_for_trigger:
          - platform: zone
            entity_id: person.myname
            zone: zone.home
            event: enter
        continue_on_timeout: true
        timeout:
          hours: 0
          minutes: 35
          seconds: 0
          milliseconds: 0
      - metadata: {}
        data: {}
        action: input_boolean.turn_off
        target:
          entity_id: input_boolean.coming_home_v2
mode: single
trace:
  stored_traces: 20

Hello returnofthem,

“trigger.id” is a variable, not something in the state machine.

Try:

Trigger was {{ trigger.id }}. Speed was

Thanks so much for the quick suggestion.

If I try that, I get:

Error rendering data template: UndefinedError: ‘trigger’ is undefined

Here’s the updated YAML I tried:

action: notify.mobile_app_my_s_s22
metadata: {}
data_template:
  title: V2 Coming home triggered
  message: >-
    Trigger was {{ trigger.id }}. Speed was
    {{states('sensor.speed_towards_home')}} mph and distance was
    {{states('sensor.distance')}} miles.

In case it’s relevant, I’m using the front-end editor for this so haven’t written all the YAML myself.

Try changing data_template to data:

action: notify.mobile_app_my_s_s22
metadata: {}
data_template:
  title: V2 Coming home triggered
  message: >-
    Trigger was {{ trigger.id }}. Speed was
    {{states('sensor.speed_towards_home')}} mph and distance was
    {{states('sensor.distance')}} miles.

to

action: notify.mobile_app_my_s_s22
metadata: {}
data:
  title: V2 Coming home triggered
  message: >-
    Trigger was {{ trigger.id }}. Speed was
    {{states('sensor.speed_towards_home')}} mph and distance was
    {{states('sensor.distance')}} miles.

Thanks for the suggestion! Unfortunately I still get the same error.

Updated YAML:

action: notify.mobile_app_my_s_s22
metadata: {}
data:
  title: V2 Coming home triggered
  message: >-
    Trigger was {{ trigger.id }}. Speed was
    {{states('sensor.speed_towards_home')}} mph and distance was
    {{states('sensor.distance')}} miles.

And the error:

OK, got it. You are hitting run in the UI and when you do that there is no trigger.
You need to make one of the actual triggers work, or temporarily add a trigger that’s easier to make happen, like a light switch or something and give it a trigger id as well.

1 Like

Haha I can’t believe it was that obvious! I even mentioned that in the OP, then forgot about it when retesting :man_facepalming:

I added a trigger for when I press a smart button and it worked perfectly - thank you!

2 Likes