Problem with data_template

I have the following automation set up:

- id: auto5
  alias: Tracker (Left Vegas)
  trigger:
    platform: zone
    entity_id: device_tracker.my_iphone
    zone: zone.Vegas
    event: 'enter'
  action:
    - service: notify.ios_my_iphone
      data_template:
        message: "{{trigger.entity_id}}"

When i trigger this from the UI (click on the automation, the ‘trigger’), I get the dreaded “UndefinedError: ‘trigger’ is undefined” error.

If I change it so that I have static text for the message:

        message: "This is a test"

It works as expected, so I know that the trigger itself is working. Its only the data_temaplate piece.

Anyone see anything obvious that i may have wrong?

I think you just have a formatting error and missing a “>”.

Here’s an example from one of my automations:

action:
    service: notify.pushbullet
    data_template:
      message: >
        Robert, you left one of the entry doors open. Front Door is {{ states.binary_sensor.front_entry_opened.state }}.  Side Door is {{ states.binary_sensor.side_entry_opened.state }}.

Ok, I tried adding the >, and a next line. Same issue. Then I tried:

        message: "{{states.binary_sensor.garagedoor_sensor}}"

With one of my sensors. THIS WORKS. Its not referencing ‘trigger’ at all though… I really need access to the trigger stuff, so I can consolidate a dozen or so automations into a single one.

What is the output you want to have? The entity_id, the zone entered/left?

Both actually. My original attempt looked like:

        message: "{{trigger.entity_id}} left {{trigger.zone}}"

If I could get a list of vars available in the jinja, that would be awesome, but i couldn’t find a way (easily) to do that last night. I’ve tried all sorts of things to get it to work (making the trigger into a list of triggers, etc…), and it all comes back to the the ‘trigger’ var not being available in the jinja context.

For the record, I was following the docs here for the zone platform: https://home-assistant.io/docs/automation/templating/

Oh, I also need to mention, I’m on 0.48.1 (can’t update for a few days) It could be that this was fixed in a later version?

I ran some quick tests on this.

When you force the trigger with the UI, none of the trigger elements actually caused the trigger, so you get the trigger undefined error. If that same automation is fired from a real trigger, it works fine.

So for example, this automation

- alias: autoTest
  trigger:
    - platform: state
      entity_id: input_boolean.attic_fan
      to: 'on'
  action:
    - service: notify.ios_tr7p
      data_template: 
        message: '{{trigger.entity_id}}'

When I switch the input_boolean to “on” I get the message input_boolean.attic_fan.
But, when I force the trigger using the UI, i get the error “trigger is undefined”.

That make sense. Maybe I should open a ticket on this, so that we might get this documented/fixed. Fixing might be difficult when there’s multiple triggers I suppose.

Testing this will be fun, as most of my zone triggers are a long ways off. Road trip.

Seems like the simple fix would be to define the UI as a trigger source.