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.
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:
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.
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:
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.