Control a Single Output from 3 different HA switches

Hi I have a SONOFF running ESPHome. I want to control the relay from 3 different switches in HA (Climate Controls i.e. 3 thermostats)

So if any switch is ON I want the relay to come on, but all 3 need to be off for the relay to turn OFF.

I could do it in HA with helpers, but the ESPHome solution seems better.

In electronics it would just be 3 diodes to a relay

Regards Dave

I have exactly this problem but with 2 switches, and at the moment I am solving it using NodeRed, because my heating relay is HTTP controlled, but the climate entities in Home Assistant are intergrated with MQTT. So I am able to intercept the command in NodeRed and decide if it should really be on or off ( after a short 30 second delay to allow multiple commands to come in, before I make a single http call )

Yep. 3 diodes.
One way to do this in ESPhome would entail:
Setup 3 binary_sensors using HA as the ‘platform’ so they now represent the state of those switches in HA.
Setup an ‘interval’ component in ESPhome that checks, e.g. every second if:
switch a is on
or
switch b is on
or
switch c is on
then
turn relay on
else
turn relay off

This assumes you don’t mind not having instantaneous response at the relay to the state of the switches in HA. This approach results in an average of a half-second delay (half of whatever you set the interval timer to).

To avoid that delay, you could instead:
define on_change automation logic on the binary_sensors that turn the relay on if any of them turns on, and if any of them turns off, checks the other two to see if this is the last one turning off and then turns the relay off.
That logic could be in a separate ESPHome ‘script’ so you don’t have to replicate it on each of the sensors. And it would react much faster than the polling method using the interval timer.

Here is a small automation I whipped up to test with some helpers
just for testing i created 4 input_booleans - 3 testswitches and one testoutput

hopefully it will give you some ideas

alias: testswitching
description: ''
trigger:
  - platform: state
    entity_id:
      - input_boolean.testswitch1
      - input_boolean.testswitch2
      - input_boolean.testswitch3
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: input_boolean.testswitch1
                state: 'on'
              - condition: state
                entity_id: input_boolean.testswitch2
                state: 'on'
              - condition: state
                entity_id: input_boolean.testswitch3
                state: 'on'
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.testoutput
    default:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.testoutput
mode: single

This is the exact logic of a group with the all option set to false (the default).

Creat a group of your switches and import that into esphome as a sensor. Use the on_value triggers in esphome (under the group sensor) to switch your relay on/off as the group sensor changes.

1 Like

Thanks for the reply, yes you are right, the group operation is correct.

Dont fully understand you instructions, is the logic taking place in HA or my ESP device?

Regards Dave

In HA, I believe.
The HA group object has a state which is the boolean ‘OR’ product of its members’ states.
Importing that group into ESPhome gives you an ESPHome component that you’ll use to set the relay’s state.

Been working on this today and may have cracked it, seems to work

switch:
  - platform: gpio
    id: relay
    pin: GPIO12

# === Psudo Relay 1, Control 1 ===============
  - platform: template
    name: "SonOff Pow Access 1"
    optimistic: true
    id: acc1
    turn_on_action:
    - switch.turn_on: relay
    turn_off_action:
    - script.execute: relayoff

# === Psudo Relay 1, Control 2 ===============
  - platform: template
    name: "SonOff Pow Access 2"
    optimistic: true    
    id: acc2
    turn_on_action:
    - switch.turn_on: relay
    turn_off_action:
    - script.execute: relayoff

# === Psudo Relay 1, Control 3 ===============    
  - platform: template
    name: "SonOff Pow Access 3"
    optimistic: true
    id: acc3
    turn_on_action:
    - switch.turn_on: relay
    turn_off_action:
    - script.execute: relayoff
 
script:
  - id: relayoff
    then:
      - delay: 100ms    
      - if:
          condition:
            or:
              - switch.is_on: acc1
              - switch.is_on: acc2
              - switch.is_on: acc3            
          then:
          # Do nothing         

          else:
          # All acc's are off
            - switch.turn_off: relay      

Edit 22-12-2021, indention and missing - in my code, which is why the delay did not work, now corrected

Any thoughts welcome

Regards Dave

Edit: I have tried and reduced the delay to 1mS and it still functions ok. If you don’t have a delay then the acc1,2 & 3 ID flags are still set when the script runs (that’s what took me a while to sort out) even though they are technically all off, obviously a timing issue within ESPhome of when those ID’s get updated.

I have 2 thermostats controlling the same SonOff, if you use the same switch in the climate settings, they fight each other, also running my another old HA on a Pi3, that I also want to control the heating from until I move over to my Pi4 (hence 3 outputs), but you can have as many or as few as you like.

Hope this helps anyone.

Both.

The group logic happens in HA. This is then imported into ESPHome as a sensor where you use it to automate your relay.