Trigger Undefined for Zone Automation

I am trying to create an automation which triggers a restful command to update my Slack status based on entering work/home zones. The automation triggers, but when it does I get the

homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined

error. I thought this was because I was manually testing it which I have read is because the zone trigger is not actually triggering. However I came into work this morning and I still get the same error. My automation is;

- id: 'Arrived_Work'
  alias: 'Arrived Work'
  trigger:
    - platform: zone
      entity_id: device_tracker.my_device
      zone: zone.work
      event: enter
  action:
  - service_template: rest_command.slack_status_set_from_zone

my rest command is;

slack_status_set_from_zone:
  url: 'https://slack.com/api/users.profile.set'
  method: POST
  headers:
    content-type: application/json
    authorization: !secret slack_token
  payload: '{"profile":{"first_name":"John","last_name":"Doe","email":"[email protected]","status_text":"at {{ trigger.zone.attributes.friendly_name }}","status_emoji":"{{ trigger.zone.attributes.slack_icon }}"}}'

I have another rest command to set my status to {{states.media_player.playstation_4.attributes.source}} which works just fine and also another one which sets my status using the state of my device tracker but I’m struggling with the zone trigger template.

I’m on HA version 0.70.1 (just going to update today!) and the latest Hassbian.

trigger.zone.attributes.slack_icon wont work because triggers don’t exist in all contexts.

Ah, ok that’s a shame. That was the reason for me using the zones, as I could set an icon for the zone and pull that through. I guess I could put an IF statement in the template and set the icon from there.

The service calls support variables, this may work:

slack_status_set_from_zone:
  url: 'https://slack.com/api/users.profile.set'
  method: POST
  headers:
    content-type: application/json
    authorization: !secret slack_token
  payload: '{"profile":{"first_name":"John","last_name":"Doe","email":"[email protected]","status_text":"at {{ name }}","status_emoji":"{{ emoji }}"}}'
  action:
  - service_template: rest_command.slack_status_set_from_zone
    data_template:
      name: '{{ trigger.zone.attributes.friendly_name }}'
      emoji: '{{ trigger.zone.attributes.slack_icon }}'

Excellent, thank you I will give that a try. I had come up with this, but your suggestion is much neater, if it works! =]

{% if is_state("device_tracker.my_device", "Work") -%}
  {% set my_slack_profile = {
    "status": "at work",
    "icon": ":office"
  } %}
{%- elif is_state("device_tracker.my_device", "Home") -%}
  {% set my_slack_profile = {
    "status": "at home",
    "icon": ":house_with_garden:"
  } %}
{%- else -%}
  {% set my_slack_profile = {
    "status": "on the move",
    "icon": ":red_car:"
  } %}
{%- endif %}

I don’t know if it works, I think it does because the documents mention passing variables.

Another option, if you know it’s for a given device_tracker entity, and you know that entity is in a defined zone (e.g., it just entered a zone, which triggered an automation that runs the rest_command :wink:), could be something like:

{{ state_attr('zone.'~states('device_tracker.my_device').lower(), 'friendly_name') }}

But, I think @petro’s suggestion is more flexible. Depends what you’re trying to do.

Well nothing I tried here worked so I just gave up and created specific commands/automations for the few case scenarios I have. Thanks for your input @pnbruckner and @petro.