Ho can I implement a tri-state switch in esphome?

I’m building an ESPHome-driven controller for my SGC SG-230 antenna matching unit. I’m using a NodeMCU borad (because I has laying around), an Elegoo 4-channel relay board for the control lines, and an opto-isolated input for sense the tune state.

The SG-230 has a 12V power feed and a tri-state control line. 12V means “Tune”, 0V means “reset” with floating meaning do nothing.

I currently have the following code.

The current arrangement requires the Tune and Reset to be controlled separately, whereas with a physical switch, it would simply be a centre-biased single-pole 2-way toggle switch.

Could I implement such a switch in esphome or HA?

  # SG-230 Power switch
  - platform: gpio
    pin: D3
    name: "SG-230 Power"
    id: sg_230_power
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF

  # Relays D4 and D5 are configured to "Tune" or "Reset" the SG-230
  # D4 NO is connected to 12V
  # D5 NO is connected to 0V

  # The control line to the SG-230 is connected to the common of both relays
  # The control line is also connected to 12V and 0V via hi-value resistors to help it "float"
  # D4 and D5 are interlocked to prevent them both being closed.
  # SG-230 "Tune"
  - platform: gpio
    pin: D4
    name: "SG-230 Tune"
    id: sg_230_tune
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
    interlock: &sg230_interlocks [sg_230_tune, sg_230_reset]

  # SG-230 "Reset"
  - platform: gpio
    pin: D5
    name: "SG-230 Reset"
    id: sg_230_reset
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
    interlock: *sg230_interlocks

I probably start with: Template Select — ESPHome if you wanted to keep all the logic in ESPhome.

Your selection can trigger an action (switch the lines appropriately), the actual switches can be made internal and then you only present the template select to HA.

1 Like

Thanks, Daryl