Help accessing trigger data from service_call

I’ve got an event that triggers on a service call. The data looks like:

{
    "event_type": "call_service",
    "data": {
        "domain": "shopping_list",
        "service": "add_item",
        "service_data": {
            "name": "***retrieve this name***"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2022-01-24T20:45:43.162198+00:00",
    ...
}

How do I access the value of “name” in service_data as a string?
I tried:
{{trigger.event.as_dict()['data']['service_data']['name']}}
But that doesn’t appear to be a string. I also tried:
{{trigger.event.as_dict()['data']['service_data']['name'] | string}}
with no luck.

edit: Also tried:
{{trigger.event['data']['service_data']['name'] | string}}
and
{{trigger.event.data.service_data.name | string}}

What’s the best way to inspect trigger info? Usually, I’d work in the template editor till I figured out the structure of the data, but I don’t know how to grab trigger data for inspection.

Do you really need as_dict()?
I would try the first attempt but without as_dict.

Yes. I had tried it without as_dict() before hitting the forum and finding an example that did that.

Adding the string filter won’t influence how the value’s type is interpreted. Home Assistant employs native typing that sets the value’s type based on its appearance. If it looks like a string its type will be string but if it looks like a number then its type will be number.

Theoretically, this should retrieve the value of the name key:

{{ trigger.event.data.service_data.name }}

FWIW, JSONpathfinder.com can be used to determine the JSONpath:

When the automation’s Event Trigger is triggered, that’s when the trigger object is defined and populated with properties. Otherwise, that variable doesn’t exist.


EDIT

If you want to experiment with it in the Template Editor then you have to define a Jinja2 variable that has the same structure as the automation’s trigger object will have.

Copy-paste this into the Template Editor:

{% set trigger = { "event":
{
    "event_type": "call_service",
    "data": {
        "domain": "shopping_list",
        "service": "add_item",
        "service_data": {
            "name": "***retrieve this name***"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2022-01-24T20:45:43.162198+00:00"
}
} %}

{{ trigger.event.data.service_data.name }}

Thanks. This still isn’t doing the trick for me.

This is outputting something like
"[object Object]": null
When I watch the event fire.

I guess that the structure of the received trigger data is different than the format that’s shown in the events developer’s tool.

I doubt it. What you see in Developer Tools > Events represents the data your automation’s Event Trigger will receive. In fact, it’s the primary tool for examining event data.

Post the complete event data, as opposed to the fragment shown in the first post, as well as the automation you are using for testing purposes.

Ah. Looks like my unfamiliarity with the GUI event editor was the problem (I just recently moved from text-file yaml setup to using the GUI builder). I opened up the YAML source to cut/paste it here and I had:

  - service: script.speak
    data:
      message:
        '[object Object]':

in the automation’s sequence. I wasn’t quoting the template in the data for the service call. I didn’t realize that the GUI was going to alter it. I put the template for “message” in quotes and it’s working as expected now. Thanks for pushing me through the debug process.