How to use the event component? Is the a best practise example?

Hello,
I read about the relatively new Event component, and I understand the concept that a momentary switch (like in my case the doorbell button) is considered as a stateless thing that kicks off events. I don’t understand how to do this.

The documentation doesn’t reveal which platforms event can have:

# Example event configuration
event:
  - platform: ...
    name: Motion Detected Event
    id: my_event

My physical momentary switch is connected to a GPIO so I tried this:

event:
  - platform: gpio
    name: "Klingeltaster mitte"
    id: klingeltaster_mitte
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 1
      mode: INPUT
      inverted: true
    icon: mdi:bell-circle-outline

This gives an error “Platform not found: ‘event.gpio’.”

I know how to solve this with a binary sensor, but now I wanted to follow the idea to use a stateless entity as it was proposed by Frenck: New entity component: `event` · home-assistant/architecture · Discussion #929 · GitHub

It looks like there’s only one event platform available, event.template.

From how I understand it, you use it to define events for later use. So to get it working with a GPIO pin, it seems like you need to do something like this:

event:
  - platform: template
    id: my_event
    event_types:
      - 'state_change'

binary_sensor:
  - platform: gpio
    internal: true # don't expose to HA
    name: "Klingeltaster mitte"
    id: klingeltaster_mitte
    ...
    on_state:
      - event.trigger:
          id: my_event
          event_type: 'state_change'

Ok, thanks, I can confirm that this works - somehow :slight_smile:
I see in the logs of the ESP that it triggers the event. But I didn’t succeed to catch that in Home Assistant. I tired an automation with an even trigger:

alias: event test
description: ""
triggers:
  - trigger: event
    event_type: klingeltaster_testbutton
conditions: []
actions:
  - action: notify.signal_me
    metadata: {}
    data:
      message: klingeltaster_testbutton
mode: single

The event type in the HA automation is the same literal as in the ESP, but nothing happens. I also tried to find it somewhere in the developer tools, but couldn’t see it. I guess I have to try more when I feel more motivated.

It feels a bit odd and over-complicated to use a stateful binary senor, hide that (internal: true) and to define an event template and trigger that event from the binary sensor … that is a lot more to do and more potential for failures. And I do not see the benefit of this approach. With my current knowldge about the event thing it appears much better to just use a binary sensor. Anything I’m missing?

You can enable the event logger (Developer Tools > Events) to see if the events actually appear on the bus.

As for this feeling odd and over-complicated: I completely agree. But I don’t know if this is the actual intended use for events, it’s just one way I found to get it working. I might be missing something too :smiley:

1 Like

There is a good example in the code for Apollo Automation’s BTN-1:

Sorry to bump an old topic. I have read the documentation on the event Component but to me it leaves room for clarification.

I think I understand the concept of a stateless event (I have some background in programming parallal discrete event simulation systems).

So I have made this ESPHome doorbell which has some functionality built in to prevent abuse. This leads to a few interesting events that are not directly related to GPIO's or binary sensors or switches. So I defined the Event component with two event_types which are triggered in the YAML at the right point.

It works allright and an event entity shows up in the esphome dashboard for the device, with the correct event_type. So far so good.

But now my problem is - how to make this event trigger an automation? The events are not in the list of events generated with the device, there is only an event entity, with one single attribute which is event_type. I could not find a way to capture the actual event. Only a change in event type can be detected. If the same event_type is triggered twice (with some time in between) the second is ignored by my automation trigger, apparently while there was no state change detected.

There must be a way to separately handle subsequent events of the same event_type. Could be a time stamp, but I have not found it.

Maybe someone can shed a light on this.

The state of an event entity is a datetime string of when the underlying event occurred. So you use an "open" State trigger, i.e. one without a defined "to" value. Then filter based on the event type attribute value in the Conditions block.

triggers:
  - trigger: state
    entity_id:
      - event.my_esphome_device
    not_to: 
      - unknown
      - unavailable
conditions:
  - "{{ trigger.to_state.attributes.event_type == 'press' }}"

Thanks for a swift reply, I will try this.

EDIT: This works! Thanks a lot!