Translating Automation Code into ESPhome

I have the yaml code below working in a Home Assistant automation, however when pressing the buttons (it has 3 buttons) on the zigbee unit the results in the esp32 are randomly weird… lights coming on, but not as intended or correct brightness etc.

Therefore I figure I will try adding the zigbee button functionality currently in the automation directly into the esp32 yaml code.

Here’s the HA automation yaml code… that uses button 1 out of 3

alias: Bedroom Lights - Rainbow
description: ""
  - platform: device
    domain: mqtt
    device_id: f501bb73628ac7c2f2b115de0c19a330
    type: action
    subtype: 1_single
    discovery_id: 0x804b50fffe21cd57 action_1_single
condition: []
  - service: light.turn_on
      entity_id: light.fastled_ws2812b_light
      effect: Rainbow Effect
      brightness_pct: 90
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
      entity_id: light.fastled_ws2812b_light
    data: {}
mode: single

I figure it is a case of creating a binary sensor… but I am unsure how to add a zigbee button device with three buttons into a binary sensor in esphome?

Any tips to point me in the right direction greatly appreciated.

So it turns out the easiest way to do this might be via MQTT… let me explain.

So in MQTT Explorer the Zigbee 3 button switch shows up as Q-3Button-Remote.

Below is what it looks like in MQTT Explorer when I press button 3 on the Zigbee switch.

   Q-3Button-Remote = {"action":null,"battery":89,"linkquality":66,"voltage":2900}
   availability = online
   action = 3_single

So if I look at the MQTT options in esphome manual, we see the option on_message in which an action can be taken when a topic message is received.

     - topic: some/topic
         - # ...
     - topic: some/other/topic
         - # ...

Only issue… the zigbee switch has 3 buttons… 1_single, 2_single, and 3_single

Therefore I need to code into the esphome yaml something that will look for the above switches being pushed, after which an action can be taken.

Perhaps this requires a lambda function? Not sure? Any suggestions on the yaml code for capturing the _single button presses appreciated.