How to retrieve attributes of an event in an automation?

Hi! I’m using the Xiaomi Aqara platform / component. The xiaomi “smart button” (which is represented by a binary sensor internally) gives me events like this:

2017-10-14 16:34:49 INFO (MainThread) [homeassistant.core] Bus:Handling <Event click[L]: click_type=single, entity_id=binary_sensor.switch_xxxxxxxxxxxx>

(Xes replace the mac address)

I would like to forward events like these to MQTT, to generate MQTT messages like this:

some/topic/<entity_id> <click_type>

This automation is the closest I’ve come:

- alias: Forward clicks to MQTT
  trigger:
    platform: event
    event_type: click
  action:
    service: mqtt.publish
    data_template:
      topic: >
	    some/topic/{{trigger.event.entity_id}}
      payload: >
   	    {{trigger.event.click_type}}

However, all I get on MQTT is:

some/topic/   (null)

It looks like I cannot access the attributes of the event in the template. However, I can access the event itself. If I change the template for the payload to {{trigger.event}}, I get these MQTT messages:

some/topic/ <Event click[L]: click_type=single, entity_id=binary_sensor.switch_xxxxxxxxxxx>

So, the event is part of the ‘trigger’ object, I just cannot access its attributes somehow. I also tried the syntax trigger['event']['entity_id'] or trigger['event'].entity_id, all to the same effect.

Does anyone have an idea how to solve this? Any help is appreciated!

trigger.event.data

With thanks to this thread

3 Likes

Thanks a lot, works like a charm!