How do I use an ESP32 w/ Relay to mirror physical switch ONLY if the ESP is not connected to HA?

Here is my use case, an RV control panel HA retrofit. I have an awesome Lilygo T-Relay 8.

I plan on connecting the physical switches to GPIO pins and publishing as a binary sensor. The actual devices like lighting, propane water heater, etc will be wired to the relays.

When the ESP32 is connected to HA and everything is fine, I’ll use HA automations to set the relay to the binary sensor state (which I can override in automation, which is the whole point of the setup), but, I want the physical switches to control the relay IF esp32 can’t connect or HA is down as a fallback method.

Any tips on a conditional functionality in ESPHome for this?

You can use something like the below as a starting point. Swap out the actions for those relating to your physical switch functionality. (this is just from something I had so not adjusted for your needs)

interval:
  - interval: 10s
    then:
      if:
        condition:
          api.connected:
        then:
          #nothing if connected
        else:
          - switch.turn_off: p1
1 Like

Thank you, Dave!

Did you try thinking about it the other way around?

Having (momentary) switches control the relays completely inside ESP as the primary method, with HA actioning the switches in automation in secondary?
You could use led-enabled switches to show the relay state…

That would avoid the constant polling