Get entity from event

I am stuck at this, maybe you can help me out.

alias: test cover event call service
description: ''
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: cover
      service: open_cover
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: cover.close_cover
    target:
      entity_id: '{{ trigger.event.data }}'
mode: single

I want get the entity from the event and deal with it later in the automation. That is how far I came. Is there a way?

note: Of course not open and closing again, that’s just for testing.

{{ trigger.event.data.entity_id }}

This gives me ‘Error: Template rendered invalid entity IDs:’

Is it because I don’t have an entity defined in the trigger?

No. If there’s an entity_id available it will be in the event’s data.

  1. Go to Developer Tools > Events.
  2. Enter call_service in “Event to subscribe to” and click “Start Listening”.
  3. Execute whatever it is you are using to open the cover.
  4. Observe the event reported in the UI. It should contain information that looks something like this:
    "event_type": "call_service",
    "data": {
        "domain": "cover",
        "service": "open_cover",
        "service_data": {
            "entity_id": [
                "cover.garage_door"
            ]
        }
    },
  1. In the except shown above, it contains entity_id and the entity is cover.garage_door.

You should be seeing an entity_id in your received event data.

@123 I did exactly this. That is how I made my way until there (little proud of myself).
Anyways.
I was dealing now with this

domain: cover
service: open_cover
service_data:
  entity_id: 
    - cover.rollo_waschraum

This wasn’t even triggering the automation. What’s wrong with that?
This then did trigger the automation:

domain: cover
service: open_cover
service_data:
  entity_id: cover.rollo_waschraum

But again gave me ‘Error: Template rendered invalid entity IDs:’

I don’t know what’s the bug here… first it triggers with the entity ID, but then for action it is invalid.

edit:

I just did it this way:

service: cover.close_cover
target:
  entity_id: 
  - '{{ trigger.event.data.entity_id }}'

which gave me ‘Error: Template rendered invalid entity IDs: [’’]’
So I guess, this is a list and the list is empty. So it renders nothing. If this is any useful.

I just did the same with light and it gave me the same error. Rebooted multiple times. Seems like a bug, doesn’t it?

I have to agree that it doesn’t appear to work the way it did in the past. I don’t remember seeing it described in recent Release Notes but perhaps I missed it. Or, like you said, it’s a bug.

One of the most recent examples of using trigger.event.data.entity_id is in pnbruckner’s post back in April:

However, I can confirm that this simple automation fails to report the entity_id (the generated persistent_notification lacks an entity_id).

- alias: event test
  id: event_test
  trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: light
      service: turn_on
  action:
  - service: notify.persistent_notification
    data:
      title: '{{ now().timestamp() | timestamp_local }}'
      message: >
        Entity ID: {{ trigger.event.data.entity_id }}

However, if I do this:

- alias: event test
  id: event_test
  trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: light
      service: turn_on
  action:
  - service: notify.persistent_notification
    data:
      title: '{{ now().timestamp() | timestamp_local }}'
      message: >
        Event Type: {{ trigger.event.event_type }},
        Event Data: {{ trigger.event.data }},
        Entity ID: {{ trigger.event.data.service_data.entity_id | join }}

The persistent_notification reports the following:

Event Type: call_service,
Event Data: {'domain': 'light', 'service': 'turn_on', 'service_data': {'entity_id': ['light.hallway_light']}},
Entity ID: light.hallway_light

In other words, entity_id is part of service_data and its value contains a list. By using a template to get the list and flatten it, it produces light.hallway_light.

In theory, trigger.event.data.entity_id should have worked. However, for whatever reason, it now doesn’t.

1 Like

Absolutely fantastic. service_data did the job. But when you think of it, it could have catched my eye.

So with that out of the way, I am looking for a way to get the friendly name.

'{{ trigger.event.data.service_data.entity_id.attributes.friendly_name }}'

won’t get the job done, which is (with my little knowledge) somehow logic, because it is not part of the event.
Maybe I can find a solution.

End goal is to send a MQTT message via zigbee2mqtt, but there you either need the friendly name or the ID. Maybe I can do something with split, don’t know. Let’s see.

But many thanks for your effort!

I have a run today…
So I changed directions and started listening to the mqtt events.
I ende up with:

alias: test cover event call service mqtt
description: ''
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: mqtt
      service: publish
      service_data:
        payload: OPEN
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: notify.mobile_app_XX
    data:
      message: '{{ trigger.event.data.service_data.topic.split("/")[1] }}'
mode: single

Love it!

Edit: This '{{ trigger.event.data.service_data.topic.split("/")[1] }}' gives me the friendly name from zigbee2mqtt, which in my case is the same as in HA.

btw: I don’t want to spam you all, but I thought I’ll have this monologue so others can see my way of progress/thoughts and some other could learn from this in the future. So I thought I’ll write it down. If this is not desired by this forum, tell me.

Greetings!

Edit 2:

  - service: mqtt.publish
    data:
      topic: >-
        zigbee2mqtt/{{ trigger.event.data.service_data.topic.split("/")[1]
        }}/set
      payload: CLOSE

In this case, I could not use the quotation. Otherwise they would be sent in the topic. Like zigbee2mqtt/'FRIENDLY_NAME'/set
When I leave the template without quotations, the quotations are gone.
Does someone know why? Wonder how the template was registered as a template then.

Like this (based on the original Event Trigger):

{{ state_attr(trigger.event.data.service_data.entity_id[0], 'friendly_name') }}

The outer quotes are needed when you define the template on the same line as the option. They serve to define the boundary of the string value being assigned to the option.

      message: '{{ trigger.event.data.service_data.topic.split("/")[1] }}'

The outer quotes are not needed when you define the template on a separate line using the line-continuation character (either a greater than symbol or a vertical bar).

      message: >
        {{ trigger.event.data.service_data.topic.split("/")[1] }}

If you do use them in this case then they will be included in the final result (i.e. they are used literally).

In the Building Templates section, the first example shows the use of a line continuation character and a template spread out over multiple lines. You’ll notice none of the phrases are delimited by quotes because they would be considered to be part of the phrase and shown in the final result.

1 Like

quit logical… thanks

Could you maybe help me out one more time.
Triggering the automation by mqtt message open and close works fine. I am trying to set the trigger for set position also.
Automation:

alias: test cover event call service mqtt
description: ''
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: mqtt
      service: publish
      service_data:
        payload:
          position: 20

Event:

"event_type": "call_service",
    "data": {
        "domain": "mqtt",
        "service": "publish",
        "service_data": {
            "topic": "zigbee2mqtt/Rollo Waschraum/set",
            "qos": 0,
            "retain": false,
            "payload": "{ \"position\": 20 }"
        }

I had no luck with different variations for the payload part in the trigger. Doesn’t work.
I actually wanted to trigger the automation by setting the position to any kind of value. I took a step back and was trying to get it with a given value.

Ok, got it one more time…

So this from the event:

"event_type": "call_service",
    "data": {
        "domain": "mqtt",
        "service": "publish",
        "service_data": {
            "topic": "zigbee2mqtt/Rollo Waschraum/set",
            "qos": 0,
            "retain": false,
            "payload": "{ \"position\": 20 }"
        }

Translates to this in the trigger:

domain: mqtt
service: publish
service_data:
  payload_position: null

Short:
this: "payload": "{ \"position\": 20 }" to this: payload_position: null or 20 if you want it specific.

Case closed!