ESP Home simple switch / button wiring and code

Hi guys,
I am now a newbie and playing around with my ESP32 board and Home Assistant. I have succesfully added the RGB diode which shows me entity status from Home Assistant (i.e. Peak electricity tariff light RED, non-peak green).

I am struggling to wire and code a simple button / microswitch.
I got it wired as it is shown on the picture.

What I want to have is:

  • when the button is pressed, it toggles a Home Assistant switch.
  • meaning when I press the button, it will start a heating
  • I already have an automation for this via zigbee button which works fine.
  • I need to change this zigbee button to my new ESP32 button.

I have tried many codes, went through the documentation, but still no success.

Any more info you need, please let me know…

Thanks very much for your kind assistance

Try this

Hey man,
thanks a lot for your answer.
I have tried it and either I am dumb (preferably) or I am doing something wrong. Still can not get the result.

Pressing the button makes no difference in Home Assitant - I can not see the HA switch going ON / OFF when I physically press the button.

You need gpio binary sensor
And set internal pullup

binary_sensor:
  - platform: gpio
    pin:
      number: D2
      mode:
        input: true
        pullup: true
    name: My Button
    inverted: true
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms

And then add your automation
on_press:
Binary Sensor Component — ESPHome

Hi,
thank you. Gives me this error:

INFO ESPHome 2025.2.2
INFO Reading configuration /config/esphome/esphome-web-d3b75c.yaml...
Failed config

binary_sensor.gpio: [source /config/esphome/esphome-web-d3b75c.yaml:44]
  platform: gpio
  pin: 
    
    Cannot resolve pin name 'D19' for board esp32dev.
    number: D19
    mode: 
      input: True
      pullup: True
  name: My Button
  
  [inverted] is an invalid option for [binary_sensor.gpio]. Please check the indentation.
  inverted: True
  filters: 
    - delayed_on: 10ms

Any idea?

Sorry for that, it was supposed to be on pin configuration:

binary_sensor:
  - platform: gpio
    pin:
      number: D2
      inverted: true
      mode:
        input: true
        pullup: true
       
    name: My Button
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms

Also, I strongly suggest to use GPIO pin definition, not Dxx.

I have just asked first time in my life Chat GPT and this bastard gave me the right code :slight_smile:

binary_sensor:
  - platform: gpio
    pin:
      number: 19  # Změněno na D19 (GPIO19)
      mode: INPUT_PULLUP  # Aktivujeme interní pull-up rezistor
      inverted: true  # Stisknuté tlačítko = LOW, invertujeme na HIGH
    name: "ESP32 Tlačítko"
    device_class: power  # Můžeš změnit na "motion", "occupancy", "door", podle použití
    on_press:
      then:
        - switch.toggle: tlacitko_stav  # Přepíná stav tlačítka

switch:
  - platform: gpio
    id: tlacitko_stav
    pin: 18  # Můžeš změnit na libovolný pin pro indikaci stavu
    restore_mode: ALWAYS_OFF  # Nezůstane zapnuté po restartu, pokud je to požadováno
    name: "Stav Tlačítka"

Now everything works perfect…

He was lucky this time, often not the case…
Add the filters there to have little bit debounce.

Done, thanks very much man for your help, really appreciate it!