Moving automation into esphome code

Good morning everyone…
I have a esp01 relay board and s d1 device that have binary sensors, that i monitor in HASS automations to do tasks, that i would like to move from hass into the esphome code.
First one is a pool pump controller made of a single esp01 relay board and i added a physical switch to the esp01 that triggers a binary sensor and from there it toggles the pump switch via hass autoamtion.
Here the esphome code:

switch:
  - platform: gpio
    pin: GPIO0
    name: ""
    inverted: True
    restore_mode: ALWAYS_OFF

binary_sensor:
  - platform: gpio
    name: ""
    pin:
      number: GPIO2 
      inverted: True
      mode:
        input: True
        pullup: True
    device_class: door

So how would i code this in esphome to say when binary sensor state changes from off to on, then toggle switch?
Thx a lot for any input.
Second would be similar.
I have a similar physical button on the d1 that is meant to open or close the door locally depending on if it is open or closed right now.
I tried to code it already but failed…
Right now it is toggled via hass automation again which i want to move into the esphome code

binary_sensor:  
  - platform: gpio
    pin:
      number: GPIO13
      mode:
        input: true
        pullup: true
      inverted: true
    id: lsw1_open
    name: "Chicken Coop open"
    filters:
      - delayed_on: 30ms
      - delayed_off: 30ms       
    on_press:
      then:
        - button.press: button_stop    

  - platform: gpio
    pin:
      number: GPIO14
      mode:
        input: true
        pullup: true
      inverted: true
    id: lsw2_closed
    name: "Chicken Coop closed"
    filters:
      - delayed_on: 30ms
      - delayed_off: 30ms      
    on_press:
      then:
        - button.press: button_stop         

  - platform: gpio
    name: "Toggle Door"
    pin:
      number: GPIO3 
      inverted: True
      mode:
        input: True
        pullup: True
    filters:
      - delayed_on: 30ms
      - delayed_off: 30ms    
    device_class: door 

#  - platform: gpio
#   name: "Toggle Door"
#   pin:
#     number: GPIO3 
#     inverted: True
#     mode:
#       input: True
#       pullup: True
#   filters:
#     - delayed_on: 30ms
#     - delayed_off: 30ms    
  #   on_press:
  #       if: 
  #         condition: 
  #           binary_sensor.is_off: lsw1_open   
  #         then:
  #           - fan.turn_on: 
  #               id: chickendoormotor
  #               speed: 30
  #               direction: forward
  #           - wait_until:
  #               condition:
  #                 binary_sensor.is_on: lsw1_open
  #           - fan.turn_off: chickendoormotor
  #         else:
  #           if:
  #             condition: 
  #               binary_sensor.is_off: lsw2_closed 
  #             then:
  #               - fan.turn_on: 
  #                   id: chickendoormotor
  #                   speed: 20
  #                   direction: REVERSE
  #               - wait_until:
  #                   condition:
  #                     binary_sensor.is_on: lsw2_ closed
  #               - fan.turn_off: chickendoormotor
  #             else:
  #               - fan.turn_off: chickendoormotor

Thx for input on this one as well

First of all you need to be providing an ID for your entities because, thats how you call them in automations, scripts, lambdas, etc so, thats your first problem and the switch your trying to toggle in your automation, it doesnt even have a name or id so, how do you suppose your suppposed to say which entity(switch) to toggle if it doesnt have a unique id?

After you fix that, you should look at the Esphome documentation. It really goes over all of this and explains it with examples.

You can even find lots of related examples included in the documentation for each entity like, switch or binary_sensor.

Thx for the hint…
I will dive right in it…lets see if i have t come back for details

Due to your great hint, i already was able to move the poolpump over.
Now i have to try myself on the if then, elseif then section for the door

Hello again…

I have the second one now in trial…


  - platform: gpio
    pin:
      number: GPIO14
      mode:
        input: true
        pullup: true
    id: toggle
    name: "Toggle Chicken Coop door"
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms
      on_press:
        then:
          - if:
            condition:
              binary_sensor.is_off: lsw2_closed 
            then:
            - button.press: button_on_sunset
       - if: 
          condition: 
            binary_sensor.is_off: lsw1_open   
          then:
            - button.press: button_on_sunrise

Hope it will do…have to wait until i get home and find 5 min to test

Problem solved…all works as planned