Using informationfrom a trigger in automation action

Hi,

I am trying to use information from the triggering event when handling telegram commands.

I can see that the templating works, as I can use the states variable when generating the response

{
  "message": "Pong! {{ states.person.shaun_barlow.state }} "
}

But when I try to use the triggering event according to the documentation

{
  "message": "Pong! {{ trigger.data.from_frist }} "
}

I get the error “Error while executing automation automation.telegram_pingpong. Error rendering template for call_service at pos 1: UndefinedError: ‘trigger’ is undefined”

Is the syntax for accessing the triggering event different for telegram_command events?

Extract from automations.yaml (as gnerated by editor) is

- id: '1570539050004'
  alias: Telegram PingPong
  trigger:
  - event_data:
      command: /ping
    event_type: telegram_command
    platform: event
  condition: []
  action:
  - data:
      message: 'Pong! {{ trigger.data.from_first }} '
    service: notify.shaun_telegram

A sample event when subscribed from the HA UI is below (chat and user ID’s have been removed)

{
    "event_type": "telegram_command",
    "data": {
        "user_id": xxxxxxx,
        "from_first": "Shaun",
        "from_last": "Last_name",
        "chat_id": xxxxxxx,
        "command": "/ping",
        "args": []
    },
    "origin": "LOCAL",
    "time_fired": "2019-10-08T14:14:21.387450+00:00",
    "context": {
        "id": "48c0298cf6564833b2bc1104bd33a643",
        "parent_id": null,
        "user_id": null
    }
}

I have tried looking at other topics, but cannot seem to find a solution

- data: in the action section needs to be - data_template:

Also, I believe the template should be {{ trigger.event.data.from_first }}

See here for more info.

1 Like

The automation below is working perfrectly with templates and -data. I have tried using data_template, and then get errors that there is no data member present (Using 0.99.3)

- id: '1570539050004'
  alias: Telegram PingPong
  trigger:
  - event_data:
      command: /ping
    event_type: telegram_command
    platform: event
  condition: []
  action:
  - data:
      message: 'Pong works! {{ states.person.shaun_barlow.state }} '
    service: notify.shaun_telegram

I tried changing to {{ trigger.event.data.from_first }}, but I still get “Error while executing automation automation.telegram_pingpong. Error rendering template for call_service at pos 1: UndefinedError: ‘trigger’ is undefined”

As @Tediore said, you need to change data: to data_template:, otherwise the notify.shaun_telegram service will be trying to evaluate the template, and at that point the trigger variable does not exist. It only exists in the context of the automation’s condition and action sections. When you use data_template with the service: call, the trigger variable will exist.

1 Like

Ok. So updated to use data_template, and the trigger template is now resolving.

Sorry @Tediore for not listening to you in the first place.

So this automation now needs to be maintained by hand in automations.yaml, as the frontend editor ignores data_template. Which is fine, the last time I used HA there was no editor so I’m comfrtable with the idea.

Once I have HA up and running with the Texecom panel sensor I’m developing I’ll check out the frontentd and see if I can submit something to get the editor to use data_template