Lock switch state

I’m trying to compile a config for an ESP32 board with 8 relays https://devices.esphome.io/devices/ESP32E-Relay-X8 and I want relay 5 to turn off and stay in that state until relay 6 is turned on. When relay 6 is turned off, relay 5 is free to switch on or off.

I have written this:

- platform: gpio
  pin: GPIO14
  name: 6 enable/disable PdC
  id: relay6
  on_switch.is_on:
    if:
      condition:
        switch.turn_off: relay5

But when I validate the config, it fails. If I change “on_switch.is_on” to “on_turn_on” and remove the “if” and “condition” lines, the validation goes well and the automation works, but only once when the state of relay 6 changes. However, I don’t get what I want because the trigger only works on state changes.

I have also tried the option
interblock: [relay5]
but obviously it only works in one direction.

Where am I going wrong?

ESPHome version 2023.12.9

type of installation Docker

platform ESP32

Board ESP32 Realy Board x8

on_switch.is_on:

should be:

on_turn_on:

I think

if

on_turn_on

it only works once because it only sees the change of state not the state

never mind, I see the mentioned device doesn’t use switches

Your syntax came from OpenAI maybe? It tends to make actions etc up…

Not 100% sure of your requirements. Does relay 5 have to stay off until relay 6 has has been on at least once? If so then you will need to add a global and set that when relay 6 turned off the first time, then check it when switching 5. The example below does that.

  1. Define relay 5 as internal: true (i.e. remove the name).

  2. Create a template switch, give it the name of relay 5.

  3. In the template switch add some checks in the turn_on_action:.

 globals:
   - id: relay6_lock
     type: bool
     restore_value: no
     initial_value: true

switch:
  - platform: gpio
    pin: GPIO14
    name: 6 enable/disable PdC
    id: relay6
    on_turn_off:
      lambda: id(relay6_lock) = false;

  - platform: gpio
    pin: GPIO12 # whatever pin
    id: relay5

  - platform: template
    name: "relay 5 name"
    id: t_relay5
    lambda: |-
      if (id(relay5).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      if:
        condition:
          lambda: 'return id(relay6_lock) == false;'
        then:
          - switch_turn_on: relay5
          # any other logic and actions 
    turn_off_action:
      - switch_turn_off: relay5

Hello, the syntax is taken directly from the webpage of ESP32 Realy Board x8 | devices.esphome.io for this board

switch:
.........
  - platform: gpio
    pin: GPIO27
    name: Relay5
    id: relay5
  - platform: gpio
    pin: GPIO14
    name: Relay6
    id: relay6
.......

and the condition from the esphome documentation Switch Component — ESPHome, although I’m sure I haven’t taken the correct parts :sweat_smile:.
Anyway, I would like relay 5 to turn off and stay off only if relay 6 is on. In practice:
relay 6 ON → relay 5 switches to OFF and remains OFF even if I try to switch it to ON manually.
relay 6 OFF → relay 5 is free to be controlled.

Soooo… Did you actually read the Interlock part of the Docs? It only “works in one direction” because interlocks are Switch Groups, not a single switch.

So, if you want to Interlock switch 5 and switch 6 then you add an interlock to each one and you interlock them opposite of each other. so switch 5 will have an ‘interlock for switch 6’ and vise versa. As far as “disabling” relay 5 if relay 6 is on… I guess I would ask you, How would you accidentally or manually turn on relay5? Through HA?

switch:
  - platform: gpio
    pin: GPIO27
    name: Relay5
    id: relay5
    interlock: [relay6]
    interlock_wait_time: 250ms

  - platform: gpio
    pin: GPIO14
    name: Relay6
    id: relay6
    interlock: [relay5]
    interlock_wait_time: 250ms

I can only think of 2 ways. Either an esphome automation that when relay5 is turned on it first checks to see is relay6 is On. If it is On then it won’t turn ON.

The other Option is you could get creative and put the relay controls in an “Entity Filter” card in HA. You can choose which entities to show, based on whatever state you want… For example, you could show a list of switches…
relay1
relay2
relay3
etc.
If relay 6 is Off then it will also show relay 5. Once relay 6 goes on, it boots the relay5 switch from the menu(filter) and then you can’t accidentally turn relay5 On because it wont be there. Once relay6 is Off, then relay5 shows back up on the list.

It would be ideal if you could grey the button out sort of like how the up/down button on a cover will grey out and cant be chosen. Im not sure about that one though.

Here is a Restrict Card for HA. This, it looks like it shows a lock symbol next to the entity that is locked and can’t be toggled if its locked. This way the switch doesn’t dissapear like if you used the Entity Filter card.
I’ve never used this FYI. I ran across this while looking for ways to grey out buttons in HA.

Maybe the best choice, but I don’t know how to make it…

this too, but I only managed to hide the button that changes state and not the other one

I already use this card for the confirmation pop up when I press the button. I asked on the author’s github how to use “hide” because it’s not documented

So, for something like this you would want to create a template switch and this will now be your primary switch instead of the gpio switch. You still need both, the template switch will be handling the logic/conditon that checks if the opposite switch is On or Off. What this switch controls is the actual gpio switch.

So, you just need to look up the configuration options for “switch” in the esphome Docs. It lists all the configs you can use with a switch entity.

These are the configuration options for a template switch and they give you examples.

Now you need the Automation part because you are doing an automation that checks the state of switch5/6 and thats called a condition. So next you look under “conditions” in the Automation section of the Docs. Here are the condition options and right there are options for “switches” thats what you want.

When you open “switch conditions” right here are examples of how to use it.

Where it sais
on_…:
if:
This is where you use one of the first configuration options you looked at for setting up a switch, like on_turn_on:

See how it basically walks you through this by following the links ftom whatever category yoir looking at?

This is how you work out creating your automations.