Switching 4 relays in ESPhome

Hi Everyone,
I am getting familiar with home assistant and esphome. I have some ESP32 boards that i’d like to use for some sensors & actors.

I can control a relay board with 4 relais with the slider-buttons in Home Assistant / esphome. So far so good.
What is the best way to implement some logic to it. Instead of switching each individual relay on or off. I would like to select option A, B, C or D.

Option A: relay 1 is on. relays 2-4 are off
Option B: relay 2 is on. relays 1, 3, 4 are off
Option C: relay 3 is on. relays 1,2,4 are off
Option D: relay 4 is on. relays 1-3 are off

Has anyone done something similar? Do you use automations in home assistant or additional add-ons like node-red. Could you provide some code for a yaml file how to handle this?


Use ha scripts

alias: option a
  - service: switch.turn_off
      entity_id: switch.relay1
    data: {}
  - service: switch.turn_off
         - switch.relay2
         - switch.relay3
mode: single

Thanks nick. I didn’t make it to have it running with the script. I think due to syntax issues. But i will try to have it as a script in the next step. For now i found a easier solution with interlocks:


  • platform: gpio
    pin: 22
    name: “s0”
    id: “s0”
    interlock: [s1, s2, s3]

  • platform: gpio
    pin: 23
    name: “s1”
    id: “s1”
    interlock: [s0, s2, s3]

  • platform: gpio
    pin: 18
    name: “s2”
    id: “s2”
    interlock: [s0, s1, s3]

  • platform: gpio
    pin: 19
    name: “s3”
    id: “s3”
    interlock: [s0, s1, s2]


Please post your yaml correctly.