Issue Leveraging Button Entity

Now that we have support for the new button entity in HA and ESPHome, I’m trying to convert one of my existing configs to change an existing momentary switch into a button (mainly for UI reasons):

switch:
  - platform: gpio
    pin: GPIO12
    id: relay
  - platform: template
    icon: "mdi:arrow-up-down-bold-outline"
    name: "Garage Door Opener"
    turn_on_action:
    - switch.turn_on: relay
    - delay: 600ms
    - switch.turn_off: relay

The issue is making a button work with a GPIO output.

I am probably just dense, but I can’t figure out how to make the two work together. Looking at the button examples in HA, they seem more geared towards triggering general automations versus what I am trying to do.

To be clear, the button press would be initiated in my Lovelace UI, not from the device itself. Can anyone suggest a way to accomplish this in ESPHome?

You just need to move the template switch actions under a button.

button:
  - platform: template
    name: "Template Button"
    on_press:
      - switch.turn_on: relay
      - delay: 600ms
      - switch.turn_off: relay

Your code is almost correct. But:

switch:
  - platform: gpio
    pin: GPIO12
    id: relay
 
 - platform: template
    icon: "mdi:arrow-up-down-bold-outline"
    name: "Garage Door Opener"
    id: gdopener  #add your button id 
    turn_on_action:
    - switch.turn_on: relay
    - delay: 600ms
    - switch.turn_off: relay
    - switch.turn_off: gdopener  # turn off your button after a given time

No the template switch is no longer used and should be removed from the config. Those actions are now moved under the button.

Buttons have no state and do not need to be shut off, beside you are calling switch.turn_off

For OP since I was not clear that the template switch should be deleted, the relay stays under switch but the button under the button key

switch:
  - platform: gpio
    pin: GPIO12
    id: relay

button:
  - platform: template
    name: "Template Button"
    on_press:
      - switch.turn_on: relay
      - delay: 600ms
      - switch.turn_off: relay

Works perfectly now. This was the one variation I didn’t try.

Thanks for this!

1 Like