How to control a relay based on GPIOs states

Hi,
I’d like to control a simple relay based on the output from GPIOs. I have a relay controlled by ESP8266, and its on GPIO0. Based on a state of GPIO1, 2 and 3 I’d like to turn on/off the relay. E.g. if GPIO1, GPIO2 are in a low state, I’d like to turn on a relay, if any of them are in a high state, I want to turn off the relay.
I defined all GPIOs as binary sensors as below, but I have no idea how can I make relay e.g. turned on while all of the binary sensors are in a low state. Could you please help me out here? I have that logic written in C++ and it works. Right now I’d like to make it in Esphome but unfortunately I don’t understand the idea how it can be done.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO2
      mode: INPUT_PULLUP   
      inverted: True
    id: contracton_1_switch
    name: "Contracton 1"

  - platform: gpio
    pin:
      number: GPIO3
      mode: INPUT_PULLUP   
      inverted: True
    id: contracton_2_switch
    name: "Contracton 2"

  - platform: gpio
    pin:
      number: GPIO1
      mode: INPUT_PULLUP   
      inverted: True
    id: contracton_3_switch
    name: "Contracton 3"

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

See also: Actions, Triggers, Conditions — ESPHome

If you logic is that simple, you can make automation within every binary sensor.
For the first:

on_press:
      then:
        - switch.turn_off: my_relay
on_release:
  then:
    - if:
        condition:
          lambda: 'return id(contracton_2_switch).state = 0 && id(contracton_3_switch).state = 0;'
        then:
          - switch.turn_on: my_relay

If your logic is more complicated, you can add template binary sensor and use the code you have in C++

Thank you for the answer. Finally, I modified the code and right now it works as expected but… Is there any possibility write the logic for the relay in one place instead of in each binary sensor block?

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO2
      mode: INPUT_PULLUP   
      inverted: True
    id: kontrakton_1
    name: "Kontrakton 1"
    on_press:
      then:
        - if:
            condition:
              lambda: 'return id(kontrakton_2).state == 1 && id(kontrakton_3).state == 1;'
            then:
              switch.turn_off: relay_switch
    on_release: 
      then:
        - switch.turn_on: relay_switch    

  - platform: gpio
    pin:
      number: GPIO3
      mode: INPUT_PULLUP  
      inverted: True
    id: kontrakton_2
    name: "Kontrakton 2"
    on_press:
      then:
        - if:
            condition:
              lambda: 'return id(kontrakton_1).state == 1 && id(kontrakton_3).state == 1;'
            then:
              switch.turn_off: relay_switch
    on_release: 
      then:
        - switch.turn_on: relay_switch    

  - platform: gpio
    pin:
      number: GPIO1
      mode: INPUT_PULLUP  
      inverted: True
    id: kontrakton_3
    name: "Kontrakton 3"
    on_press:
      then:
        - if:
            condition:
              lambda: 'return id(kontrakton_1).state == 1 && id(kontrakton_2).state == 1;'
            then:
              switch.turn_off: relay_switch
    on_release: 
      then:
        - switch.turn_on: relay_switch   
switch:
  - platform: gpio
    pin: 
      mode: 
        output: True
      inverted: True
      number: GPIO0 
    name: "Relay"
    id: relay_switch

You could put it in a script and call that as needed.

For that I posted link to template sensor documentation.
You can leave binary sensors without any automation and do everything in one template sensor with C++ code in lambda. It’s equal to code in Arduino loop.

Thanks, I’ll try to do that