Newbie button

hello !
first post ,im new in the esp32 and i need some help.

i have a relay that is activating a water pump and i can control it through HA as a switch and not momentary button . i want to add and a physical button and when it pressed will activate the pump for 30 sec. and if for some reasson need it to stop before the time ends, press again to stop it.

i have search but with no luck
this is what i have for now

esphome:
  name: pump
  platform: ESP8266
  board: nodemcuv2

switch:
  - platform: gpio
    name: "pump"
    pin: D1
    id: pump
    inverted: yes
binary_sensor:
  - platform: gpio
    pin: D7
    name: "pumpbutton"
    id: button1
    on_press:
      then:
        - switch.turn_on: pump
        - delay: 30s
        - switch.turn_off: pump
    on_double_click: #(I prefer to turnit off with a single click but i havent use labda to make the state)
      then:
        - switch.turn_off: pump

The button is connected at +5v and D7,should i add mode: INPUT_PULLUP ?

(Sorry for my english!)

Any help?

Try this as your binary_sensor:

binary_sensor:
  - platform: gpio
    pin:
      number: D7
      mode: INPUT_PULLUP
      inverted: True
    name: "pumpbutton"
    id: button1
    on_press:
      then:
        - if:
            condition:
              switch.is_off: pump
            then:
              - switch.turn_on: pump
              - delay: 30s
              - switch.turn_off: pump
            else:
              - switch.turn_off: pump

You should connect the physical button to pin D7 and to GND (ground).
Hope it works!

Yes !! thank you very much !!! its working like a charm !
At HA, should i create an automation because when i toggle the switch,stays on

If you don’t need the binary sensor in HA you may delete name: “pumpbutton” line in your code, so you will not expose the binary sensor to HA and will only have the switch to control the pump directly!!