Ignoring the syntax of ‘to_dict()’ vs string splitting, the difference is this.
Service Data:
https://www.home-assistant.io/docs/configuration/events/#event-call_service
Trigger Data:
https://www.home-assistant.io/docs/automation/templating/#event
The way I’ve done it, trigger.event.data will return the 1st linked url which only defines those things…service_data being one of them. data.entity_id ‘might’ be in that data, or it might not be. It will depend on the service and/or integration as to what data is put in trigger.event.data. In your case, you seem to be getting lucky that ‘entity_id’ is defined there. It might not always be true.
It’s similar to how you call a service. You can do the following 2 things, both valid.
- service: light.turn_on
entity_id: light.my_light
- service: light.turn_on
data:
entity_id: light.my_light
Technically, we should all be doing the 2nd one according to the documentation. But for some reason, the first one works. Maybe home assistant is automatically copying the entity_id into the service data for us. My other guess is, if you were to look at this event data, trigger.event.data.entity_id would not exist for the second call, but will for the first.
Actually, I want to test this…brb.
Ok, here’s the difference. Here’s my test script.
light_service_test_1:
sequence:
- service: light.turn_on
entity_id: light.office_dimmer_switch
- delay: "00:00:01"
- service: light.turn_on
data:
entity_id: light.office_dimmer_switch
Here are the capture service calls.
First event from script
{
"event_type": "call_service",
"data": {
"domain": "light",
"service": "turn_on",
"service_data": {
"entity_id": [
"light.office_dimmer_switch"
]
}
},
"origin": "LOCAL",
"time_fired": "2020-04-14T15:25:03.530618+00:00",
"context": {
"id": "ecaf69738b454a6cb5edf46410144fc2",
"parent_id": null,
"user_id": "e39b6e3266eb4b3a81937449708a4f74"
}
}
Second Event from script
{
"event_type": "call_service",
"data": {
"domain": "light",
"service": "turn_on",
"service_data": {
"entity_id": "light.office_dimmer_switch"
}
},
"origin": "LOCAL",
"time_fired": "2020-04-14T15:21:14.262167+00:00",
"context": {
"id": "83b09dbae4474c858803377885214d63",
"parent_id": null,
"user_id": "e39b6e3266eb4b3a81937449708a4f74"
}
}
So a few things.
-
it looks like by not specifying the service data, it auto generated a service data LIST with the entity id in the list.
-
in no cases would ‘trigger.event.data.entity_id’ exist. At least for this service. The persistent_notification domain might be different.