How to extract data from trigger.event.data?

I’m trying to extract an image file name from an event using trigger.event.data but I tried & searched here for hours and have no idea how to do that. Maybe a guru can help?

The event looks like this

event_type: call_service
data:
  domain: telegram_bot
  service: send_photo
  service_data:
    target: xxxxxxxxxx
    url: >-
      http://192.168.1.200:3000/api/storage/matches/snapshot.jpg
    caption: 'Caption'
origin: LOCAL
time_fired: "2023-09-20T16:14:07.690043+00:00"
context:
  id: 01HASNXHT9B28YNNVJAAMCMD52
  parent_id: null
  user_id: null

This is the automation I use. I tried many different variations of trigger.event.data.url and trigger.event.data.service_data.url but none of them worked. I’m getting “invalid key” or similar errors.

- id: '20230920171511'
  alias: 'Notification Image Test'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: call_service
    event_data:
        domain: telegram_bot
        service: send_photo
  action:
    - service: notify.robert
      data:
        message: 'Notification Image Test'
        data:
          photo:
            - url: {{ trigger.event.data.url }}
              caption: 'Image'

Wrap the template in quotes.

url: '{{ trigger.event.data.url }}'

Reference
Important template rules

Thank you. Unfortunately I’m now getting this error:

Logger: homeassistant.helpers.template
Source: helpers/template.py:232
First occurred: 22:03:27 (1 occurrences)
Last logged: 22:03:27

Template variable warning: 'dict object' has no attribute 'url' when rendering '{{ trigger.event.data.url }}'

Check the automation’s trace and confirm trigger.event.data has a url property.

Based on the example you posted above, I believe it should be like this:

url: '{{ trigger.event.data.service_data.url }}'
1 Like

Trying to do something similar. I want to get the remoteJid of a whatsapp group every time I receive a new whatsapp group message and then send it in a whatsapp message

Output from listening service is as below:

event_type: new_whatsapp_message
data:
  clientId: default
  type: conversation
  key:
    remoteJid: [email protected]
    fromMe: false
    id: 711E3107xxxxxxxxxxx06176FC23B
    participant: [email protected]
  messageTimestamp: 1708420501
  pushName: Lxxxxxxxxey
  broadcast: false
  message:
    conversation: Hel
origin: REMOTE
time_fired: "2024-02-20T09:15:02.148603+00:00"
context:
  id: 01HQ2WR4M4A6A5Q2S4D7V3HHVZ
  parent_id: null
  user_id: e9c44cxxxxxxxxxxxb34c0e

this is my (failed) attempt to get the remoteJid and send it in a Whatsapp:

alias: get remote Jid
description: ""
trigger:
  - platform: event
    event_type: new_whatsapp_message
condition: []
action:
  - service: whatsapp.send_message
    data:
      clientId: default
      to: [email protected]
      body: "{{ trigger.event.data.remoteJid }}"
mode: single

The trace says
“Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘event’”

Not sure what I’ve missed?

to answer my own question,

{{ trigger.event.data.key.remoteJid }}

is what I should have used