Toggle switch from internal physical momentary pushbutton

Hi,

I wrote my first lines of ESPHome code today for a possible future large project. Physical momentary pushbuttons configured as binary sensors is supposed to toggle GPIO Switches (for lamps). The GPIO switches shall also be able to be controlled by switches in Home Assistant Lovelace interface.

This code is working for me. Is it ok written or do you have other suggestions?

switch:
  - platform: gpio
    pin: 25
    name: "Testswitch1"
    id: Testswitch1

binary_sensor:
  - platform: gpio
    name: "Button 1"
    pin:
      number: 32
      mode: INPUT_PULLUP
      inverted: False
    on_press:
      - switch.toggle: Testswitch1
    internal: True

Well, cleaned it up and made it a little more consequent. Seems to work anyway.

# Momentary Pushbuttons
binary_sensor:
  - platform: gpio
    name: "Button 1"
    pin:
      number: 32
      inverted: False
      mode:
        input: True
        pullup: True
    on_press:
      - switch.toggle: Testswitch1
    internal: True

# Outputs
switch:
  - platform: gpio
    name: "Testswitch1"
    id: Testswitch1
    pin:
      number: 25

Looks fine to me.

1 Like

Thanks alot. Time for me to read up on pin specifications, test some different boards and expand the concept then :slight_smile: