Is it possible to pre-empt in an automation?

I recently had a quietcool whole-house fan installed. I’m trying to attach it to home assistant, and according to their wiring diagram, the three speeds are controlled with power to three different leads. There’s a high, medium and low wire, and depending on which you give power to the fan will operate at that speed.

I’m not sure what happens if you give two ‘speeds’ power at the same time, so in the interest of safety, what I’m trying to do is create an automation that makes sure only one relay can be on at a time. I have a basic automation working, but if I have one speed on, and i select another speed, there’s a brief moment when ‘both are on at once’.

I’ve created a test harness for this and attached some simple lightbulbs to test with before attaching it to the fan, and what I’d like to do is configure the automation to first turn off a relay, before it turns another one on.

Is there a way to tell the automation ‘if this call is made, first do this automation before letting the call continue’? For example, if relay 1 is on, and I want to turn on relay 2, i’d like the automation to turn off relay 1 before turning on relay 2.

… or is this better done with a series of scripts? I was hoping to take care of it with one automation and a bunch of if/then statements. Here’s the current state of it:

alias: whole_house_fan_relay_rules
description: makes sure only one relay can be on at a time.
trigger:
  - platform: state
    entity_id:
      - switch.multirelay_2
    id: relay1_on
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - switch.multirelay_2_2
    from: "off"
    to: "on"
    id: relay2_on
  - platform: state
    entity_id:
      - switch.multirelay_3
    from: "off"
    to: "on"
    id: relay3_on
condition: []
action:
  - if:
      - condition: trigger
        id: relay1_on
    then:
      - type: turn_off
        device_id: b8400563518b0e56437de44f632f485e
        entity_id: switch.multirelay_2_2
        domain: switch
      - type: turn_off
        device_id: b8400563518b0e56437de44f632f485e
        entity_id: switch.multirelay_3
        domain: switch
  - if:
      - condition: trigger
        id: relay2_on
    then:
      - type: turn_off
        device_id: b8400563518b0e56437de44f632f485e
        entity_id: switch.multirelay_2
        domain: switch
      - type: turn_off
        device_id: b8400563518b0e56437de44f632f485e
        entity_id: switch.multirelay_3
        domain: switch
  - if:
      - condition: trigger
        id: relay3_on
    then:
      - type: turn_off
        device_id: b8400563518b0e56437de44f632f485e
        entity_id: switch.multirelay_2
        domain: switch
      - type: turn_off
        device_id: b8400563518b0e56437de44f632f485e
        entity_id: switch.multirelay_2_2
        domain: switch
mode: single

All you need to do is switch all relays off before turning the new one on.

Logically that makes perfect sense - but is it possible to tell an automation to do that? or is it better to just make a script?

Specifically, I’m wondering if its possible to basically ‘intercept’ the call to turn on a relay and have the automation not actually turn on the relay, but perform the automation action first. It seems like automations can’t do that, but if it’s possible I just don’t know how.

Where are the requests to change the speed coming from?

An automation?

A dashboard button?

A physical switch?

All of the above?

Using an automation for this is not ‘in the interest of safety’ since there is always a) a way to bypass them, b) chance of a f-up situation.

Best practice is to wire them such that they can’t both be on at once. This is very easy when you wire the relays.

During testing, I made manual buttons, but the idea was to have a one-press button on a dashboard that cycled through the relays, to recreate the off/low/medium/high functionality of a simple remote.

I was able to sort it out by creating an automation, and a script. The automation prevents more than 1 relay from being on at a time, and this functions as a sort of backup safety measure.

The automation does what you’ve suggested earlier - it turns off whatever is on, then turns on the intended relay.

I plan on writing up an entire howto and posting it here, I can see other people have asked the same questions and since I sorted it out I figured I’d write it up!

Thanks for the help!

Just to expand on my comment about doing the interlocking in hardware rather than software, here is how I did it for a 3 speed fan controller myself. All you need to do is take the wires from points 1, 2 and 3 to run to your fan. (in my case they went to a speed control capacitor)
image