Switch a variable with two binary switches

Hi guys, unfortunately I don’t know the syntax of ESP Home but I hope you can help me. I have 2 binary switches. I want to switch on a variable in Homeassistant with the first switch. With the second switch in Homeassistant switch off. Could you help me? Enclosed my code:

globals:

  • id: Post
    type: bool
    restore_value: yes
    initial_value: ‘false’

binary_sensor:

  • platform: gpio
    pin:
    number: D1
    mode: INPUT_PULLUP
    name: “Postklappe Oben”

  • platform: gpio
    pin:
    number: D2
    mode: INPUT_PULLUP
    inverted: True
    name: “Posttür Vorne”

In HA, create input_boolean.YOUR_VARIABLE. Then create an automation:

trigger:
  - platform: state
    entity_id: binary_sensor.postklappe_oben
    to: 'on'
  - platform: state
    entit_id: binary_sensor.posttur_vorne
    to: 'on'
action:
  service:  >
    input_boolean.turn_o{{'on' if trigger_to_state('binary_sensor.postklappe_oben') else 'off'}}
    entity_id: input_boolean.YOU_NEED_TO_CREAT_ONE_FOR_HERE

hopefully that works…

Thank you for your quick reply.
I would like to have a solution directly in ESPHome. without auxiliary variable from Homeassistant. Therefore also restore_value=yes. Is this possible?

so you don’t want to do this above?

You can do the automation in ESPhome instead then. The syntax is different but not difficult

yes the automation should run in esp. It is a mailbox and does not always have 100% wifi. So I have already not noticed some letters. How would the code look then?

One way to do it would be to create an output, and switch which correlates to HA. Docs

output:
  - platform: gpio
    id: 'mailbox_output'
    pin: xx (choose a pin to use here)

switch:
  - platform: output
    name: 'Mailbox'
    output: 'mailbox_output'

then add an esphome automation to your existing binary_sensor's. Docs

binary_sensor:
  - platform: gpio
    pin:
    number: D1
    mode: INPUT_PULLUP
    name: “Postklappe Oben”
    on_press:
      then:
        - switch.turn_on: mailbox

  - platform: gpio
    pin:
    number: D2
    mode: INPUT_PULLUP
    inverted: True
    name: “Posttür Vorne”
    on_press:
      then:
        - switch.turn_off: mailbox

Thanks for the quick solution. Attached is the final code:

switch:

  • platform: gpio
    pin:
    number: D3
    name: “Post”
    id: Post

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
    name: "Postklappe Oben"
    on_press:
      then:
        - switch.turn_on: Post 
        
  - platform: gpio
    pin:
      number: D2
      mode: INPUT_PULLUP
      inverted: True
    name: “Posttür Vorne”
    on_press:
      then:
        - switch.turn_off: Post