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: