First Time Automation - 'trigger' is undefined

trying my hand at my first automation and received a trigger is undefined error: Error rendering template for call_service at pos 1: UndefinedError: ‘trigger’ is undefined

I checked my config multiple times but obviously I am missing something.

- id: '1581610932185'
  alias: Arrive At Work
  trigger:
    platform: zone
    entity_id: device_tracker.life360_ken
    zone: zone.dc3
    event: enter
  condition: []
  action:
    service: notify.gmail
    data:
      message: '{ trigger.entity_id.attributes.friendly_name }} has entered {{ trigger.zone
        }}'

that seems correct. It’ll give you the error as there is no actual trigger yet and this is a template.
you’ll need check triggering the actual automation.

  1. If you use a template for the message you need to use data_template
  2. You’re missing a curly bracket at the start of your template.

Try

  action:
    service: notify.gmail
    data_template:
      message: "{{ trigger.entity_id.attributes.friendly_name }} has entered {{ trigger.zone }}"
1 Like

I would also double-check your config.yaml . (better safe than sorry). Typically any “undefined” errors points to some sort of incomplete setup in your configuration.yaml file (from my experience).

I’m assuming you got that error after you tried to trigger the automation manually from the pop-up box?

If so then the problem is as @juan11perez said.

you can’t manually trigger a automation that uses a trigger object in the template because if you trigger it manually there was never any trigger since the automation ignores the trigger and conditions when you trigger it like that.

you have to actually leave the zone and re-enter to trigger it properly so the action knows what to do with the template.

If it’s some other reason for the error then you need to give more information.

ok, so I arrived at work this morning, and the automation shows as trigger, BUT I did not get any email notification…

image

Got this error: Error rendering data template: UndefinedError: ‘str object’ has no attribute ‘attributes’

I am going to strip back the message to something simple like “ken has arrived at work” and see if I can get the automation to complete by sending the email.

- id: '1581610932185'
  alias: Arrive At Work
  trigger:
    platform: zone
    entity_id: device_tracker.life360_ken
    zone: zone.dc3
    event: enter
  condition: []
  action:
    service: notify.gmail
    data_template:
      message: "{{ trigger.to_state.name }} has entered {{ trigger.zone }}"

Explanation

When you use the zone platform for a trigger, the Trigger State object has the following properties:

Zone

Template variable Data
trigger.platform Hardcoded: zone
trigger.entity_id Entity ID that we are observing.
trigger.from_state Previous state object of the entity.
trigger.to_state New state object of the entity.
trigger.zone State object of zone
trigger.event Event that trigger observed: enter or leave .

If we choose trigger.to_state that will provide us with the new State object. We can now refer to the State object’s properties (listed here). We need this one:

state.name
Name of the entity. Based on friendly_name attribute with fall back to object ID. Example: Kitchen Ceiling .

So the correct reference is: trigger.to_state.name

try this in your template:

{{ state_attr(trigger.event.data.entity_id, 'friendly_name') }}  has entered {{ trigger.zone }}