Changeover Switch Automation

Hello everyone,

I am trying to build a changeover box to switch between 3 power sources.

So our house has a single-phase power connection but sometimes the phase would go down at the distributor and we would lose power. And, our house is the most unlucky one as every time it’s our house phase that goes down, an electrician would have to climb the power pole to change the phase wires.

So what I am trying to do is have a junction box, where all 3 phases (4 wires) come to and then output a single phase (2 wires) to my house.

I am trying to do it just with ESPHome automation as I don’t want to rely on HA for it.

While I can get it to switch it’s not very consistent and I am looking for someone to take at it and maybe give some better alternative.

The flow is like this

  1. Phase 1 has power, so output Phase 1 (Reference)
  2. Phase 1 is down and Phase 2 has power, so output Phase 2 (Secondary)
  3. Phase 1 and 2 are down and Phase 3 has power, so output Phase 3 (Tertiary)

FYI: those log voltages are simulated

esphome:
  name: esp32-phasebox
  includes:
  - reference.h
  - secondary.h
  - tertiary.h
  - EmonLib.h
  - EmonLib.cpp

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "XJeIO62+KUSond2T6Fi53ymTfhp1TR0pekD1qyLS3gY="

ota:
  password: "6844d542c72b59d703039a2163ccc9a9"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Phasebox Fallback Hotspot"
    password: "GhNn0yKEd9Yd"

captive_portal:

switch:
  - platform: gpio
    pin: GPIO19
    name: "IN1"
    id: in1_switch
    inverted: true
    restore_mode: ALWAYS_ON
    
  - platform: gpio
    pin: GPIO18
    name: "IN2"
    id: in2_switch
    inverted: true
    restore_mode: ALWAYS_OFF
    
  - platform: gpio
    pin: GPIO17
    name: "IN3"
    id: in3_switch
    inverted: true
    restore_mode: ALWAYS_OFF
    
  - platform: gpio
    pin: GPIO16
    name: "Lamp Post"
    inverted: true
    restore_mode: ALWAYS_ON
  
#Tried to use these custom switches to add a delay between off and on to prevent short-circuit but they  did not work

  - platform: template
    name: "Reference"
    id: reference_switch
    turn_on_action:
      - then:
        - switch.turn_off: in2_switch
        - switch.turn_off: in3_switch
        - delay: 1s
        - switch.turn_on: in1_switch
        - switch.template.publish:
            id: reference_switch
            state: ON
    turn_off_action:
      - then:
        - switch.turn_off: in1_switch
        - switch.template.publish:
            id: reference_switch
            state: OFF
    
  - platform: template
    name: "Secondary"
    id: secondary_switch
    turn_on_action:
      - then:
        - switch.turn_off: in1_switch
        - switch.turn_off: in3_switch
        - delay: 1s
        - switch.turn_on: in2_switch
        - switch.template.publish:
            id: secondary_switch
            state: ON
    turn_off_action:
      - then:
        - switch.turn_off: in2_switch
        - switch.template.publish:
            id: secondary_switch
            state: OFF
            
  - platform: template
    name: "Tertiary"
    id: tertiary_switch
    turn_on_action:
      - then:
        - switch.turn_off: in1_switch
        - switch.turn_off: in3_switch
        - delay: 1s
        - switch.turn_on: in3_switch
        - switch.template.publish:
            id: tertiary_switch
            state: ON
    turn_off_action:
      - then:
        - switch.turn_off: in3_switch
        - switch.template.publish:
            id: tertiary_switch
            state: OFF

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new Reference();
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Reference"
    id: reference_sensor
    unit_of_measurement: V
    accuracy_decimals: 2
    on_value:
      then:
        - script.execute: changeover_script

- platform: custom
  lambda: |-
    auto my_sensor = new Secondary;
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Secondary"
    id: secondary_sensor
    unit_of_measurement: V
    accuracy_decimals: 2

- platform: custom
  lambda: |-
    auto my_sensor = new Tertiary();
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Tertiary"
    id: tertiary_sensor
    unit_of_measurement: V
    accuracy_decimals: 2

script:
  - id: changeover_script
    then:
      - if:
          condition:
            - lambda: 'return id(reference_sensor).state > 110;'
          then:
            - switch.turn_off: in2_switch
            - switch.turn_off: in3_switch
            - delay: 1s
            - switch.turn_on: in1_switch
      - if:
          condition:
            and:
              - lambda: 'return id(reference_sensor).state < 110;'
              - lambda: 'return id(secondary_sensor).state > 110;'
          then:
            - switch.turn_off: in1_switch
            - switch.turn_off: in3_switch
            - delay: 1s
            - switch.turn_on: in2_switch
      - if:
          condition:
            and:
              - lambda: 'return id(reference_sensor).state < 110;'
              - lambda: 'return id(secondary_sensor).state < 110;'
              - lambda: 'return id(tertiary_sensor).state > 110;'
          then:
            - switch.turn_off: in1_switch
            - switch.turn_off: in2_switch
            - delay: 1s
            - switch.turn_on: in3_switch
INFO Reading configuration /config/esphome/esp32-phasebox.yaml...
INFO Starting log output from esp32-phasebox.local using esphome API
INFO Successfully connected to esp32-phasebox.local
[16:29:52][I][app:102]: ESPHome version 2022.6.2 compiled on Aug  1 2022, 15:10:04
[16:29:52][C][wifi:491]: WiFi:
[16:29:52][C][wifi:353]:   Local MAC: 84:CC:A8:7A:76:0C
[16:29:52][C][wifi:354]:   SSID: 'Docking Bay 94'[redacted]
[16:29:52][C][wifi:355]:   IP Address: 192.168.68.55
[16:29:52][C][wifi:357]:   BSSID: 0E:EB:B6:C6:17:68[redacted]
[16:29:52][C][wifi:358]:   Hostname: 'esp32-phasebox'
[16:29:52][C][wifi:360]:   Signal strength: -39 dB ▂▄▆█
[16:29:52][C][wifi:364]:   Channel: 9
[16:29:52][C][wifi:365]:   Subnet: 255.255.252.0
[16:29:52][C][wifi:366]:   Gateway: 192.168.68.1
[16:29:52][C][wifi:367]:   DNS1: 8.8.8.8
[16:29:52][C][wifi:368]:   DNS2: 8.8.4.4
[16:29:54][D][sensor:125]: 'Reference': Sending state 42.07473 V with 2 decimals of accuracy
[16:29:54][D][switch:017]: 'IN1' Turning OFF.
[16:29:54][D][switch:017]: 'IN2' Turning OFF.
[16:29:56][D][sensor:125]: 'Secondary': Sending state 32.92859 V with 2 decimals of accuracy
[16:29:57][D][sensor:125]: 'Tertiary': Sending state 1034.82324 V with 2 decimals of accuracy
[16:29:57][C][logger:275]: Logger:
[16:29:57][C][logger:276]:   Level: DEBUG
[16:29:57][C][logger:277]:   Log Baud Rate: 115200
[16:29:57][C][logger:278]:   Hardware UART: UART0
[16:29:59][D][sensor:125]: 'Secondary': Sending state 30.18653 V with 2 decimals of accuracy
[16:29:59][D][sensor:125]: 'Tertiary': Sending state 1025.41846 V with 2 decimals of accuracy
[16:30:01][D][sensor:125]: 'Reference': Sending state 49.59450 V with 2 decimals of accuracy
[16:30:01][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:01][D][switch:013]: 'IN3' Turning ON.
[16:30:01][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:30:01][D][sensor:125]: 'Tertiary': Sending state 1068.66553 V with 2 decimals of accuracy
[16:30:03][D][sensor:125]: 'Reference': Sending state 45.02429 V with 2 decimals of accuracy
[16:30:03][D][switch:017]: 'IN1' Turning OFF.
[16:30:03][D][switch:017]: 'IN2' Turning OFF.
[16:30:05][D][sensor:125]: 'Secondary': Sending state 29.98352 V with 2 decimals of accuracy
[16:30:05][C][switch.gpio:050]: GPIO Switch 'IN1'
[16:30:05][C][switch.gpio:050]:   Inverted: YES
[16:30:05][C][switch.gpio:051]:   Pin: GPIO19
[16:30:05][C][switch.gpio:073]:   Restore Mode: Always ON
[16:30:08][D][sensor:125]: 'Secondary': Sending state 32.83768 V with 2 decimals of accuracy
[16:30:08][D][sensor:125]: 'Tertiary': Sending state 1048.11011 V with 2 decimals of accuracy
[16:30:10][D][sensor:125]: 'Reference': Sending state 45.63897 V with 2 decimals of accuracy
[16:30:10][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:10][D][switch:013]: 'IN3' Turning ON.
[16:30:10][C][switch.gpio:050]: GPIO Switch 'IN2'
[16:30:10][C][switch.gpio:050]:   Inverted: YES
[16:30:10][C][switch.gpio:051]:   Pin: GPIO18
[16:30:10][C][switch.gpio:073]:   Restore Mode: Always OFF
[16:30:12][D][sensor:125]: 'Secondary': Sending state 34.70473 V with 2 decimals of accuracy
[16:30:12][D][sensor:125]: 'Tertiary': Sending state 1030.90930 V with 2 decimals of accuracy
[16:30:14][D][sensor:125]: 'Reference': Sending state 45.10583 V with 2 decimals of accuracy
[16:30:14][D][switch:017]: 'IN1' Turning OFF.
[16:30:14][D][switch:017]: 'IN2' Turning OFF.
[16:30:14][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:30:14][C][switch.gpio:050]: GPIO Switch 'IN3'
[16:30:14][C][switch.gpio:050]:   Inverted: YES
[16:30:14][C][switch.gpio:051]:   Pin: GPIO17
[16:30:14][C][switch.gpio:073]:   Restore Mode: Always OFF
[16:30:16][D][sensor:125]: 'Secondary': Sending state 33.00296 V with 2 decimals of accuracy
[16:30:17][D][sensor:125]: 'Tertiary': Sending state 1058.88940 V with 2 decimals of accuracy
[16:30:19][D][sensor:125]: 'Reference': Sending state 42.87979 V with 2 decimals of accuracy
[16:30:19][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:19][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:30:19][C][switch.gpio:050]: GPIO Switch 'Lamp Post'
[16:30:19][C][switch.gpio:050]:   Inverted: YES
[16:30:19][C][switch.gpio:051]:   Pin: GPIO16
[16:30:19][C][switch.gpio:073]:   Restore Mode: Always ON
[16:30:21][D][sensor:125]: 'Secondary': Sending state 34.25254 V with 2 decimals of accuracy
[16:30:21][D][sensor:125]: 'Tertiary': Sending state 1117.24524 V with 2 decimals of accuracy
[16:30:23][D][sensor:125]: 'Reference': Sending state 51.10090 V with 2 decimals of accuracy
[16:30:23][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:23][D][switch:013]: 'IN3' Turning ON.
[16:30:23][C][template.switch:058]: Template Switch 'Reference'
[16:30:23][C][template.switch:059]:   Restore State: NO
[16:30:23][C][template.switch:060]:   Optimistic: NO
[16:30:23][D][sensor:125]: 'Tertiary': Sending state 986.45685 V with 2 decimals of accuracy
[16:30:25][D][sensor:125]: 'Reference': Sending state 48.08076 V with 2 decimals of accuracy
[16:30:25][D][switch:017]: 'IN1' Turning OFF.
[16:30:25][D][switch:017]: 'IN2' Turning OFF.
[16:30:27][D][sensor:125]: 'Secondary': Sending state 32.04117 V with 2 decimals of accuracy
[16:30:29][D][sensor:125]: 'Secondary': Sending state 31.42759 V with 2 decimals of accuracy
[16:30:30][D][sensor:125]: 'Tertiary': Sending state 1053.50500 V with 2 decimals of accuracy
[16:30:32][D][sensor:125]: 'Reference': Sending state 41.99415 V with 2 decimals of accuracy
[16:30:32][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:32][D][switch:013]: 'IN3' Turning ON.
[16:30:32][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:30:32][C][template.switch:058]: Template Switch 'Secondary'
[16:30:32][C][template.switch:059]:   Restore State: NO
[16:30:32][C][template.switch:060]:   Optimistic: NO
[16:30:34][D][sensor:125]: 'Secondary': Sending state 33.94251 V with 2 decimals of accuracy
[16:30:34][D][sensor:125]: 'Tertiary': Sending state 1021.68030 V with 2 decimals of accuracy
[16:30:36][D][sensor:125]: 'Reference': Sending state 46.13744 V with 2 decimals of accuracy
[16:30:36][D][switch:017]: 'IN1' Turning OFF.
[16:30:36][D][switch:017]: 'IN2' Turning OFF.
[16:30:38][D][sensor:125]: 'Secondary': Sending state 32.82913 V with 2 decimals of accuracy
[16:30:39][D][sensor:125]: 'Tertiary': Sending state 1010.75159 V with 2 decimals of accuracy
[16:30:41][D][sensor:125]: 'Reference': Sending state 44.82219 V with 2 decimals of accuracy
[16:30:41][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:41][C][template.switch:058]: Template Switch 'Tertiary'
[16:30:41][C][template.switch:059]:   Restore State: NO
[16:30:41][C][template.switch:060]:   Optimistic: NO
[16:30:43][D][sensor:125]: 'Secondary': Sending state 32.49038 V with 2 decimals of accuracy
[16:30:43][D][sensor:125]: 'Tertiary': Sending state 1063.14868 V with 2 decimals of accuracy
[16:30:45][D][sensor:125]: 'Reference': Sending state 48.25756 V with 2 decimals of accuracy
[16:30:45][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:45][D][switch:013]: 'IN3' Turning ON.
[16:30:45][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:30:47][D][sensor:125]: 'Reference': Sending state 48.85452 V with 2 decimals of accuracy
[16:30:47][D][switch:017]: 'IN1' Turning OFF.
[16:30:47][D][switch:017]: 'IN2' Turning OFF.
[16:30:49][D][sensor:125]: 'Secondary': Sending state 31.83132 V with 2 decimals of accuracy
[16:30:50][D][sensor:125]: 'Tertiary': Sending state 1014.25897 V with 2 decimals of accuracy
[16:30:50][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:30:52][D][sensor:125]: 'Secondary': Sending state 32.39151 V with 2 decimals of accuracy
[16:30:52][D][sensor:125]: 'Tertiary': Sending state 1109.13599 V with 2 decimals of accuracy
[16:30:54][D][sensor:125]: 'Reference': Sending state 45.07037 V with 2 decimals of accuracy
[16:30:54][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:30:54][D][switch:013]: 'IN3' Turning ON.
[16:30:54][D][sensor:125]: 'Tertiary': Sending state 1084.25757 V with 2 decimals of accuracy
[16:30:56][D][sensor:125]: 'Reference': Sending state 47.89956 V with 2 decimals of accuracy
[16:30:56][D][switch:017]: 'IN1' Turning OFF.
[16:30:56][D][switch:017]: 'IN2' Turning OFF.
[16:30:58][D][sensor:125]: 'Secondary': Sending state 36.50465 V with 2 decimals of accuracy
[16:31:00][D][sensor:125]: 'Secondary': Sending state 37.41735 V with 2 decimals of accuracy
[16:31:01][D][sensor:125]: 'Tertiary': Sending state 1018.89185 V with 2 decimals of accuracy
[16:31:03][D][sensor:125]: 'Reference': Sending state 45.30906 V with 2 decimals of accuracy
[16:31:03][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:03][D][switch:013]: 'IN3' Turning ON.
[16:31:03][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:31:05][D][sensor:125]: 'Secondary': Sending state 32.98181 V with 2 decimals of accuracy
[16:31:05][D][sensor:125]: 'Tertiary': Sending state 1073.00232 V with 2 decimals of accuracy
[16:31:07][D][sensor:125]: 'Reference': Sending state 47.38411 V with 2 decimals of accuracy
[16:31:07][D][switch:017]: 'IN1' Turning OFF.
[16:31:07][D][switch:017]: 'IN2' Turning OFF.
[16:31:10][D][sensor:125]: 'Secondary': Sending state 38.32125 V with 2 decimals of accuracy
[16:31:10][D][sensor:125]: 'Tertiary': Sending state 1025.95129 V with 2 decimals of accuracy
[16:31:12][D][sensor:125]: 'Reference': Sending state 46.34484 V with 2 decimals of accuracy
[16:31:12][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:14][D][sensor:125]: 'Secondary': Sending state 37.29955 V with 2 decimals of accuracy
[16:31:14][D][sensor:125]: 'Tertiary': Sending state 1004.66718 V with 2 decimals of accuracy
[16:31:16][D][sensor:125]: 'Reference': Sending state 43.71157 V with 2 decimals of accuracy
[16:31:16][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:16][D][switch:013]: 'IN3' Turning ON.
[16:31:16][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:31:19][D][sensor:125]: 'Secondary': Sending state 34.89614 V with 2 decimals of accuracy
[16:31:19][D][sensor:125]: 'Tertiary': Sending state 1084.14246 V with 2 decimals of accuracy
[16:31:21][D][sensor:125]: 'Reference': Sending state 42.61497 V with 2 decimals of accuracy
[16:31:21][D][switch:017]: 'IN1' Turning OFF.
[16:31:21][D][switch:017]: 'IN2' Turning OFF.
[16:31:21][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:31:21][C][captive_portal:088]: Captive Portal:
[16:31:23][D][sensor:125]: 'Secondary': Sending state 36.62203 V with 2 decimals of accuracy
[16:31:23][D][sensor:125]: 'Tertiary': Sending state 1010.79559 V with 2 decimals of accuracy
[16:31:25][D][sensor:125]: 'Reference': Sending state 43.44024 V with 2 decimals of accuracy
[16:31:25][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:25][C][mdns:084]: mDNS:
[16:31:25][C][mdns:085]:   Hostname: esp32-phasebox
[16:31:25][D][sensor:125]: 'Tertiary': Sending state 1074.25256 V with 2 decimals of accuracy
[16:31:27][D][sensor:125]: 'Reference': Sending state 49.31693 V with 2 decimals of accuracy
[16:31:27][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:29][D][sensor:125]: 'Secondary': Sending state 31.52131 V with 2 decimals of accuracy
[16:31:29][D][switch:013]: 'IN3' Turning ON.
[16:31:30][C][ota:085]: Over-The-Air Updates:
[16:31:30][C][ota:086]:   Address: esp32-phasebox.local:3232
[16:31:30][C][ota:089]:   Using Password.
[16:31:32][D][sensor:125]: 'Secondary': Sending state 31.22525 V with 2 decimals of accuracy
[16:31:32][D][sensor:125]: 'Tertiary': Sending state 1069.67139 V with 2 decimals of accuracy
[16:31:34][D][sensor:125]: 'Reference': Sending state 46.76653 V with 2 decimals of accuracy
[16:31:34][D][switch:017]: 'IN1' Turning OFF.
[16:31:34][D][switch:017]: 'IN2' Turning OFF.
[16:31:34][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:31:34][C][api:138]: API Server:
[16:31:34][C][api:139]:   Address: esp32-phasebox.local:6053
[16:31:34][C][api:141]:   Using noise encryption: YES
[16:31:36][D][sensor:125]: 'Secondary': Sending state 34.23928 V with 2 decimals of accuracy
[16:31:36][D][sensor:125]: 'Tertiary': Sending state 1041.92529 V with 2 decimals of accuracy
[16:31:38][D][sensor:125]: 'Reference': Sending state 45.09075 V with 2 decimals of accuracy
[16:31:38][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:40][D][sensor:125]: 'Reference': Sending state 43.65619 V with 2 decimals of accuracy
[16:31:40][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:42][D][sensor:125]: 'Secondary': Sending state 31.32583 V with 2 decimals of accuracy
[16:31:43][D][sensor:125]: 'Tertiary': Sending state 985.13318 V with 2 decimals of accuracy
[16:31:43][D][switch:013]: 'IN3' Turning ON.
[16:31:45][D][sensor:125]: 'Secondary': Sending state 38.36620 V with 2 decimals of accuracy
[16:31:45][D][sensor:125]: 'Tertiary': Sending state 1061.70288 V with 2 decimals of accuracy
[16:31:47][D][sensor:125]: 'Reference': Sending state 50.81318 V with 2 decimals of accuracy
[16:31:47][D][switch:017]: 'IN1' Turning OFF.
[16:31:47][D][switch:017]: 'IN2' Turning OFF.
[16:31:47][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:31:47][D][sensor:125]: 'Tertiary': Sending state 1091.95288 V with 2 decimals of accuracy
[16:31:49][D][sensor:125]: 'Reference': Sending state 48.04338 V with 2 decimals of accuracy
[16:31:49][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:51][D][sensor:125]: 'Secondary': Sending state 30.61222 V with 2 decimals of accuracy
[16:31:52][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:31:54][D][sensor:125]: 'Secondary': Sending state 33.07884 V with 2 decimals of accuracy
[16:31:54][D][sensor:125]: 'Tertiary': Sending state 1053.50037 V with 2 decimals of accuracy
[16:31:56][D][sensor:125]: 'Reference': Sending state 45.69548 V with 2 decimals of accuracy
[16:31:56][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:31:56][D][switch:013]: 'IN3' Turning ON.
[16:31:58][D][sensor:125]: 'Secondary': Sending state 32.79788 V with 2 decimals of accuracy
[16:31:58][D][sensor:125]: 'Tertiary': Sending state 1064.99158 V with 2 decimals of accuracy
[16:32:01][D][sensor:125]: 'Reference': Sending state 46.25361 V with 2 decimals of accuracy
[16:32:01][D][switch:017]: 'IN1' Turning OFF.
[16:32:01][D][switch:017]: 'IN2' Turning OFF.
[16:32:03][D][sensor:125]: 'Secondary': Sending state 34.16474 V with 2 decimals of accuracy
[16:32:03][D][sensor:125]: 'Tertiary': Sending state 1034.23279 V with 2 decimals of accuracy
[16:32:05][D][sensor:125]: 'Reference': Sending state 43.99067 V with 2 decimals of accuracy
[16:32:05][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:05][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:32:05][D][sensor:125]: 'Tertiary': Sending state 1089.49658 V with 2 decimals of accuracy
[16:32:07][D][sensor:125]: 'Reference': Sending state 48.01156 V with 2 decimals of accuracy
[16:32:07][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:09][D][sensor:125]: 'Secondary': Sending state 29.91165 V with 2 decimals of accuracy
[16:32:09][D][switch:013]: 'IN3' Turning ON.
[16:32:11][D][sensor:125]: 'Secondary': Sending state 32.71479 V with 2 decimals of accuracy
[16:32:12][D][sensor:125]: 'Tertiary': Sending state 1058.86743 V with 2 decimals of accuracy
[16:32:14][D][sensor:125]: 'Reference': Sending state 45.48133 V with 2 decimals of accuracy
[16:32:14][D][switch:017]: 'IN1' Turning OFF.
[16:32:14][D][switch:017]: 'IN2' Turning OFF.
[16:32:16][D][sensor:125]: 'Secondary': Sending state 34.11821 V with 2 decimals of accuracy
[16:32:16][D][sensor:125]: 'Tertiary': Sending state 1067.49731 V with 2 decimals of accuracy
[16:32:18][D][sensor:125]: 'Reference': Sending state 50.22393 V with 2 decimals of accuracy
[16:32:18][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:18][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:32:18][D][sensor:125]: 'Tertiary': Sending state 1054.49329 V with 2 decimals of accuracy
[16:32:20][D][sensor:125]: 'Reference': Sending state 52.76831 V with 2 decimals of accuracy
[16:32:20][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:22][D][sensor:125]: 'Secondary': Sending state 32.92593 V with 2 decimals of accuracy
[16:32:22][D][switch:013]: 'IN3' Turning ON.
[16:32:22][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:32:25][D][sensor:125]: 'Secondary': Sending state 35.25837 V with 2 decimals of accuracy
[16:32:25][D][sensor:125]: 'Tertiary': Sending state 1020.60010 V with 2 decimals of accuracy
[16:32:27][D][sensor:125]: 'Reference': Sending state 44.98706 V with 2 decimals of accuracy
[16:32:27][D][switch:017]: 'IN1' Turning OFF.
[16:32:27][D][switch:017]: 'IN2' Turning OFF.
[16:32:29][D][sensor:125]: 'Secondary': Sending state 35.00117 V with 2 decimals of accuracy
[16:32:30][D][sensor:125]: 'Tertiary': Sending state 1038.13342 V with 2 decimals of accuracy
[16:32:32][D][sensor:125]: 'Reference': Sending state 51.61735 V with 2 decimals of accuracy
[16:32:32][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:34][D][sensor:125]: 'Secondary': Sending state 32.85672 V with 2 decimals of accuracy
[16:32:34][D][sensor:125]: 'Tertiary': Sending state 1058.30981 V with 2 decimals of accuracy
[16:32:36][D][sensor:125]: 'Reference': Sending state 47.13456 V with 2 decimals of accuracy
[16:32:36][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:36][D][switch:013]: 'IN3' Turning ON.
[16:32:36][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:32:38][D][sensor:125]: 'Secondary': Sending state 36.32795 V with 2 decimals of accuracy
[16:32:38][D][sensor:125]: 'Tertiary': Sending state 1047.07422 V with 2 decimals of accuracy
[16:32:40][D][sensor:125]: 'Reference': Sending state 47.65826 V with 2 decimals of accuracy
[16:32:40][D][switch:017]: 'IN1' Turning OFF.
[16:32:40][D][switch:017]: 'IN2' Turning OFF.
[16:32:42][D][sensor:125]: 'Reference': Sending state 50.66559 V with 2 decimals of accuracy
[16:32:42][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:44][D][sensor:125]: 'Secondary': Sending state 31.07316 V with 2 decimals of accuracy
[16:32:45][D][sensor:125]: 'Tertiary': Sending state 1019.56555 V with 2 decimals of accuracy
[16:32:47][D][sensor:125]: 'Secondary': Sending state 33.54048 V with 2 decimals of accuracy
[16:32:47][D][sensor:125]: 'Tertiary': Sending state 1127.57239 V with 2 decimals of accuracy
[16:32:49][D][sensor:125]: 'Reference': Sending state 48.40438 V with 2 decimals of accuracy
[16:32:49][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:32:49][D][switch:013]: 'IN3' Turning ON.
[16:32:49][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:32:51][D][sensor:125]: 'Secondary': Sending state 32.51038 V with 2 decimals of accuracy
[16:32:51][D][sensor:125]: 'Tertiary': Sending state 1052.43994 V with 2 decimals of accuracy
[16:32:53][D][sensor:125]: 'Reference': Sending state 47.84275 V with 2 decimals of accuracy
[16:32:53][D][switch:017]: 'IN1' Turning OFF.
[16:32:53][D][switch:017]: 'IN2' Turning OFF.
[16:32:53][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:32:55][D][sensor:125]: 'Secondary': Sending state 33.95401 V with 2 decimals of accuracy
[16:32:56][D][sensor:125]: 'Tertiary': Sending state 1052.66504 V with 2 decimals of accuracy
[16:32:58][D][sensor:125]: 'Reference': Sending state 42.56108 V with 2 decimals of accuracy
[16:32:58][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:00][D][sensor:125]: 'Secondary': Sending state 35.86930 V with 2 decimals of accuracy
[16:33:00][D][sensor:125]: 'Tertiary': Sending state 1056.23157 V with 2 decimals of accuracy
[16:33:02][D][sensor:125]: 'Reference': Sending state 50.52472 V with 2 decimals of accuracy
[16:33:02][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:02][D][switch:013]: 'IN3' Turning ON.
[16:33:04][D][sensor:125]: 'Reference': Sending state 47.61371 V with 2 decimals of accuracy
[16:33:04][D][switch:017]: 'IN1' Turning OFF.
[16:33:04][D][switch:017]: 'IN2' Turning OFF.
[16:33:07][D][sensor:125]: 'Secondary': Sending state 30.29005 V with 2 decimals of accuracy
[16:33:07][D][sensor:125]: 'Tertiary': Sending state 1043.22705 V with 2 decimals of accuracy
[16:33:07][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:33:09][D][sensor:125]: 'Secondary': Sending state 33.25961 V with 2 decimals of accuracy
[16:33:09][D][sensor:125]: 'Tertiary': Sending state 1007.31311 V with 2 decimals of accuracy
[16:33:11][D][sensor:125]: 'Reference': Sending state 45.00408 V with 2 decimals of accuracy
[16:33:11][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:11][D][switch:013]: 'IN3' Turning ON.
[16:33:13][D][sensor:125]: 'Reference': Sending state 52.24640 V with 2 decimals of accuracy
[16:33:13][D][switch:017]: 'IN1' Turning OFF.
[16:33:13][D][switch:017]: 'IN2' Turning OFF.
[16:33:15][D][sensor:125]: 'Secondary': Sending state 32.19556 V with 2 decimals of accuracy
[16:33:16][D][sensor:125]: 'Tertiary': Sending state 1055.73950 V with 2 decimals of accuracy
[16:33:18][D][sensor:125]: 'Secondary': Sending state 33.23019 V with 2 decimals of accuracy
[16:33:18][D][sensor:125]: 'Tertiary': Sending state 1048.03516 V with 2 decimals of accuracy
[16:33:20][D][sensor:125]: 'Reference': Sending state 41.57199 V with 2 decimals of accuracy
[16:33:20][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:20][D][switch:013]: 'IN3' Turning ON.
[16:33:20][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:33:20][D][sensor:125]: 'Tertiary': Sending state 1049.35376 V with 2 decimals of accuracy
[16:33:22][D][sensor:125]: 'Reference': Sending state 45.61613 V with 2 decimals of accuracy
[16:33:22][D][switch:017]: 'IN1' Turning OFF.
[16:33:22][D][switch:017]: 'IN2' Turning OFF.
[16:33:24][D][sensor:125]: 'Secondary': Sending state 30.68077 V with 2 decimals of accuracy
[16:33:24][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:33:26][D][sensor:125]: 'Secondary': Sending state 32.19431 V with 2 decimals of accuracy
[16:33:27][D][sensor:125]: 'Tertiary': Sending state 1101.09814 V with 2 decimals of accuracy
[16:33:29][D][sensor:125]: 'Reference': Sending state 44.43146 V with 2 decimals of accuracy
[16:33:29][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:29][D][switch:013]: 'IN3' Turning ON.
[16:33:31][D][sensor:125]: 'Secondary': Sending state 37.56519 V with 2 decimals of accuracy
[16:33:31][D][sensor:125]: 'Tertiary': Sending state 1094.50037 V with 2 decimals of accuracy
[16:33:33][D][sensor:125]: 'Reference': Sending state 43.29077 V with 2 decimals of accuracy
[16:33:33][D][switch:017]: 'IN1' Turning OFF.
[16:33:33][D][switch:017]: 'IN2' Turning OFF.
[16:33:36][D][sensor:125]: 'Reference': Sending state 40.97929 V with 2 decimals of accuracy
[16:33:36][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:38][D][sensor:125]: 'Secondary': Sending state 31.64477 V with 2 decimals of accuracy
[16:33:38][D][sensor:125]: 'Tertiary': Sending state 1069.77112 V with 2 decimals of accuracy
[16:33:38][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:33:40][D][sensor:125]: 'Secondary': Sending state 32.36559 V with 2 decimals of accuracy
[16:33:40][D][sensor:125]: 'Tertiary': Sending state 1046.29675 V with 2 decimals of accuracy
[16:33:43][D][sensor:125]: 'Reference': Sending state 44.05656 V with 2 decimals of accuracy
[16:33:43][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:43][D][switch:013]: 'IN3' Turning ON.
[16:33:45][D][sensor:125]: 'Secondary': Sending state 34.20146 V with 2 decimals of accuracy
[16:33:45][D][sensor:125]: 'Tertiary': Sending state 1071.01819 V with 2 decimals of accuracy
[16:33:47][D][sensor:125]: 'Reference': Sending state 47.44057 V with 2 decimals of accuracy
[16:33:47][D][switch:017]: 'IN1' Turning OFF.
[16:33:47][D][switch:017]: 'IN2' Turning OFF.
[16:33:49][D][sensor:125]: 'Secondary': Sending state 35.15437 V with 2 decimals of accuracy
[16:33:49][D][sensor:125]: 'Tertiary': Sending state 1048.91504 V with 2 decimals of accuracy
[16:33:51][D][sensor:125]: 'Reference': Sending state 41.94804 V with 2 decimals of accuracy
[16:33:51][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:51][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:33:53][D][sensor:125]: 'Reference': Sending state 43.49284 V with 2 decimals of accuracy
[16:33:53][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:33:55][D][sensor:125]: 'Secondary': Sending state 33.54456 V with 2 decimals of accuracy
[16:33:55][D][sensor:125]: 'Tertiary': Sending state 1033.14856 V with 2 decimals of accuracy
[16:33:55][D][switch:013]: 'IN3' Turning ON.
[16:33:55][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:33:58][D][sensor:125]: 'Secondary': Sending state 33.57300 V with 2 decimals of accuracy
[16:33:58][D][sensor:125]: 'Tertiary': Sending state 1083.94934 V with 2 decimals of accuracy
[16:34:00][D][sensor:125]: 'Reference': Sending state 48.01091 V with 2 decimals of accuracy
[16:34:00][D][switch:017]: 'IN1' Turning OFF.
[16:34:00][D][switch:017]: 'IN2' Turning OFF.
[16:34:02][D][sensor:125]: 'Secondary': Sending state 33.62620 V with 2 decimals of accuracy
[16:34:02][D][sensor:125]: 'Tertiary': Sending state 1048.65662 V with 2 decimals of accuracy
[16:34:04][D][sensor:125]: 'Reference': Sending state 47.31202 V with 2 decimals of accuracy
[16:34:04][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:06][D][sensor:125]: 'Secondary': Sending state 31.83449 V with 2 decimals of accuracy
[16:34:07][D][sensor:125]: 'Tertiary': Sending state 1087.59045 V with 2 decimals of accuracy
[16:34:07][D][switch:013]: 'IN3' Turning ON.
[16:34:09][D][sensor:125]: 'Reference': Sending state 46.71851 V with 2 decimals of accuracy
[16:34:09][D][switch:017]: 'IN1' Turning OFF.
[16:34:09][D][switch:017]: 'IN2' Turning OFF.
[16:34:09][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:34:11][D][sensor:125]: 'Secondary': Sending state 32.58744 V with 2 decimals of accuracy
[16:34:11][D][sensor:125]: 'Tertiary': Sending state 1045.25562 V with 2 decimals of accuracy
[16:34:13][D][sensor:125]: 'Reference': Sending state 47.06385 V with 2 decimals of accuracy
[16:34:13][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:13][D][sensor:125]: 'Tertiary': Sending state 1133.35596 V with 2 decimals of accuracy
[16:34:15][D][sensor:125]: 'Reference': Sending state 47.67562 V with 2 decimals of accuracy
[16:34:15][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:17][D][sensor:125]: 'Secondary': Sending state 28.47865 V with 2 decimals of accuracy
[16:34:17][D][switch:013]: 'IN3' Turning ON.
[16:34:19][D][sensor:125]: 'Secondary': Sending state 36.35407 V with 2 decimals of accuracy
[16:34:20][D][sensor:125]: 'Tertiary': Sending state 1048.25732 V with 2 decimals of accuracy
[16:34:22][D][sensor:125]: 'Reference': Sending state 44.22134 V with 2 decimals of accuracy
[16:34:22][D][switch:017]: 'IN1' Turning OFF.
[16:34:22][D][switch:017]: 'IN2' Turning OFF.
[16:34:22][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:34:24][D][sensor:125]: 'Secondary': Sending state 34.47196 V with 2 decimals of accuracy
[16:34:24][D][sensor:125]: 'Tertiary': Sending state 986.82996 V with 2 decimals of accuracy
[16:34:26][D][sensor:125]: 'Reference': Sending state 46.29856 V with 2 decimals of accuracy
[16:34:26][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:26][D][api:102]: Accepted ::FFFF:C0A8:4439
[16:34:28][D][sensor:125]: 'Reference': Sending state 43.26129 V with 2 decimals of accuracy
[16:34:28][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:30][D][sensor:125]: 'Secondary': Sending state 30.21635 V with 2 decimals of accuracy
[16:34:31][D][sensor:125]: 'Tertiary': Sending state 1047.98096 V with 2 decimals of accuracy
[16:34:31][D][switch:013]: 'IN3' Turning ON.
[16:34:33][D][sensor:125]: 'Secondary': Sending state 30.69161 V with 2 decimals of accuracy
[16:34:33][D][sensor:125]: 'Tertiary': Sending state 1085.45642 V with 2 decimals of accuracy
[16:34:35][D][sensor:125]: 'Reference': Sending state 41.54087 V with 2 decimals of accuracy
[16:34:35][D][switch:017]: 'IN1' Turning OFF.
[16:34:35][D][switch:017]: 'IN2' Turning OFF.
[16:34:37][D][sensor:125]: 'Reference': Sending state 45.93863 V with 2 decimals of accuracy
[16:34:37][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:39][D][sensor:125]: 'Secondary': Sending state 32.69286 V with 2 decimals of accuracy
[16:34:40][D][sensor:125]: 'Tertiary': Sending state 1040.25916 V with 2 decimals of accuracy
[16:34:40][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[16:34:42][D][sensor:125]: 'Secondary': Sending state 38.37558 V with 2 decimals of accuracy
[16:34:42][D][sensor:125]: 'Tertiary': Sending state 1035.92749 V with 2 decimals of accuracy
[16:34:44][D][sensor:125]: 'Reference': Sending state 45.89974 V with 2 decimals of accuracy
[16:34:44][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:44][D][switch:013]: 'IN3' Turning ON.
[16:34:44][D][sensor:125]: 'Tertiary': Sending state 1096.03516 V with 2 decimals of accuracy
[16:34:46][D][sensor:125]: 'Reference': Sending state 43.43227 V with 2 decimals of accuracy
[16:34:46][D][switch:017]: 'IN1' Turning OFF.
[16:34:46][D][switch:017]: 'IN2' Turning OFF.
[16:34:48][D][sensor:125]: 'Secondary': Sending state 35.66298 V with 2 decimals of accuracy
[16:34:50][D][sensor:125]: 'Secondary': Sending state 32.34710 V with 2 decimals of accuracy
[16:34:51][D][sensor:125]: 'Tertiary': Sending state 1051.02515 V with 2 decimals of accuracy
[16:34:53][D][sensor:125]: 'Reference': Sending state 44.97039 V with 2 decimals of accuracy
[16:34:53][W][script:011]: Script 'changeover_script' is already running! (mode: single)
[16:34:53][D][switch:013]: 'IN3' Turning ON.
[16:34:53][D][api.connection:156]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439) requested disconnected
[16:34:55][D][sensor:125]: 'Secondary': Sending state 30.58859 V with 2 decimals of accuracy
[16:34:55][D][sensor:125]: 'Tertiary': Sending state 1011.59741 V with 2 decimals of accuracy

I saw the script was slow for the third switch and instant for the first. I added a 2-second delay to the first switch and it stopped switching. (Maybe the script is triggered over due to the sensors updating voltage continuously).

I made some updates where I interlocked some of the relays and removed the delays from the script. It works much better now, but I would love that delay to prevent any short-circuit

esphome:
  name: esp32-phasebox
  includes:
  - reference.h
  - secondary.h
  - tertiary.h
  - EmonLib.h
  - EmonLib.cpp

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "XJeIO62+KUSond2T6Fi53ymTfhp1TR0pekD1qyLS3gY="

ota:
  password: "6844d542c72b59d703039a2163ccc9a9"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Phasebox Fallback Hotspot"
    password: "GhNn0yKEd9Yd"

captive_portal:

switch:
  - platform: gpio
    pin: GPIO19
    name: "IN1"
    id: in1_switch
    interlock: &in_switch_interlock [in1_switch, in2_switch, in3_switch]
    inverted: true
    restore_mode: ALWAYS_ON
    
  - platform: gpio
    pin: GPIO18
    name: "IN2"
    id: in2_switch
    interlock: *in_switch_interlock
    inverted: true
    restore_mode: ALWAYS_OFF
    
  - platform: gpio
    pin: GPIO17
    name: "IN3"
    id: in3_switch
    interlock: *in_switch_interlock
    inverted: true
    restore_mode: ALWAYS_OFF
    
  - platform: gpio
    pin: GPIO16
    name: "Lamp Post"
    inverted: true
    restore_mode: ALWAYS_ON

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new Reference();
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Reference"
    id: reference_sensor
    unit_of_measurement: V
    accuracy_decimals: 2
    on_value:
      then:
        - script.execute: changeover_script

- platform: custom
  lambda: |-
    auto my_sensor = new Secondary;
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Secondary"
    id: secondary_sensor
    unit_of_measurement: V
    accuracy_decimals: 2

- platform: custom
  lambda: |-
    auto my_sensor = new Tertiary();
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Tertiary"
    id: tertiary_sensor
    unit_of_measurement: V
    accuracy_decimals: 2

script:
  - id: changeover_script
    then:
      - if:
          condition:
            - lambda: 'return id(reference_sensor).state > 110;'
          then:
            - switch.turn_on: in1_switch
      - if:
          condition:
            and:
              - lambda: 'return id(reference_sensor).state < 110;'
              - lambda: 'return id(secondary_sensor).state > 110;'
          then:
            - switch.turn_on: in2_switch
      - if:
          condition:
            and:
              - lambda: 'return id(reference_sensor).state < 110;'
              - lambda: 'return id(secondary_sensor).state < 110;'
              - lambda: 'return id(tertiary_sensor).state > 110;'
          then:
            - switch.turn_on: in3_switch
INFO Reading configuration /config/esphome/esp32-phasebox.yaml...
INFO Starting log output from esp32-phasebox.local using esphome API
INFO Successfully connected to esp32-phasebox.local
[17:29:48][I][app:102]: ESPHome version 2022.6.2 compiled on Aug  1 2022, 17:21:20
[17:29:48][C][wifi:491]: WiFi:
[17:29:48][C][wifi:353]:   Local MAC: 84:CC:A8:7A:76:0C
[17:29:48][C][wifi:354]:   SSID: 'Docking Bay 94'[redacted]
[17:29:48][C][wifi:355]:   IP Address: 192.168.68.55
[17:29:48][C][wifi:357]:   BSSID: 0E:EB:B6:C6:17:68[redacted]
[17:29:48][C][wifi:358]:   Hostname: 'esp32-phasebox'
[17:29:48][C][wifi:360]:   Signal strength: -39 dB ▂▄▆█
[17:29:48][C][wifi:364]:   Channel: 9
[17:29:48][C][wifi:365]:   Subnet: 255.255.252.0
[17:29:48][C][wifi:366]:   Gateway: 192.168.68.1
[17:29:48][C][wifi:367]:   DNS1: 8.8.8.8
[17:29:48][C][wifi:368]:   DNS2: 8.8.4.4
[17:29:48][D][sensor:125]: 'Tertiary': Sending state 1054.81653 V with 2 decimals of accuracy
[17:29:50][D][sensor:125]: 'Reference': Sending state 83.23483 V with 2 decimals of accuracy
[17:29:50][D][switch:013]: 'IN3' Turning ON.
[17:29:50][D][switch:017]: 'IN1' Turning OFF.
[17:29:50][D][switch:037]: 'IN1': Sending state OFF
[17:29:50][D][switch:037]: 'IN3': Sending state ON
[17:29:52][D][sensor:125]: 'Secondary': Sending state 359.36792 V with 2 decimals of accuracy
[17:29:52][D][api:102]: Accepted ::FFFF:C0A8:4439
[17:29:52][C][logger:275]: Logger:
[17:29:52][C][logger:276]:   Level: DEBUG
[17:29:52][C][logger:277]:   Log Baud Rate: 115200
[17:29:52][C][logger:278]:   Hardware UART: UART0
[17:29:52][D][sensor:125]: 'Tertiary': Sending state 1228.61011 V with 2 decimals of accuracy
[17:29:54][D][sensor:125]: 'Reference': Sending state 72.80309 V with 2 decimals of accuracy
[17:29:54][D][switch:013]: 'IN2' Turning ON.
[17:29:54][D][switch:017]: 'IN3' Turning OFF.
[17:29:54][D][switch:037]: 'IN3': Sending state OFF
[17:29:54][D][switch:037]: 'IN2': Sending state ON
[17:29:55][D][sensor:125]: 'Secondary': Sending state 939.20581 V with 2 decimals of accuracy
[17:29:55][D][sensor:125]: 'Tertiary': Sending state 1109.67065 V with 2 decimals of accuracy
[17:29:57][D][sensor:125]: 'Reference': Sending state 53.97317 V with 2 decimals of accuracy
[17:29:57][D][switch:013]: 'IN2' Turning ON.
[17:29:57][D][sensor:125]: 'Secondary': Sending state 815.85455 V with 2 decimals of accuracy
[17:29:57][C][switch.gpio:050]: GPIO Switch 'IN1'
[17:29:57][C][switch.gpio:050]:   Inverted: YES
[17:29:57][C][switch.gpio:051]:   Pin: GPIO19
[17:29:57][C][switch.gpio:073]:   Restore Mode: Always ON
[17:29:57][C][switch.gpio:075]:   Interlocks:
[17:29:57][C][switch.gpio:079]:     IN2
[17:29:57][C][switch.gpio:079]:     IN3
[17:29:58][D][sensor:125]: 'Tertiary': Sending state 1122.57385 V with 2 decimals of accuracy
[17:30:00][D][sensor:125]: 'Reference': Sending state 60.19492 V with 2 decimals of accuracy
[17:30:00][D][switch:013]: 'IN2' Turning ON.
[17:30:00][D][sensor:125]: 'Secondary': Sending state 876.68298 V with 2 decimals of accuracy
[17:30:00][D][api.connection:861]: Home Assistant 2022.7.6 (::FFFF:C0A8:4439): Connected successfully
[17:30:00][C][switch.gpio:050]: GPIO Switch 'IN2'
[17:30:00][C][switch.gpio:050]:   Inverted: YES
[17:30:00][C][switch.gpio:051]:   Pin: GPIO18
[17:30:00][C][switch.gpio:073]:   Restore Mode: Always OFF
[17:30:00][C][switch.gpio:075]:   Interlocks:
[17:30:00][C][switch.gpio:079]:     IN1
[17:30:00][C][switch.gpio:079]:     IN3
[17:30:00][D][sensor:125]: 'Tertiary': Sending state 1092.17371 V with 2 decimals of accuracy
[17:30:02][D][sensor:125]: 'Reference': Sending state 50.43154 V with 2 decimals of accuracy
[17:30:02][D][switch:013]: 'IN2' Turning ON.
[17:30:02][D][sensor:125]: 'Secondary': Sending state 813.11121 V with 2 decimals of accuracy
[17:30:02][C][switch.gpio:050]: GPIO Switch 'IN3'
[17:30:02][C][switch.gpio:050]:   Inverted: YES
[17:30:02][C][switch.gpio:051]:   Pin: GPIO17
[17:30:02][C][switch.gpio:073]:   Restore Mode: Always OFF
[17:30:02][C][switch.gpio:075]:   Interlocks:
[17:30:02][C][switch.gpio:079]:     IN1
[17:30:02][C][switch.gpio:079]:     IN2
[17:30:03][D][sensor:125]: 'Tertiary': Sending state 1064.41992 V with 2 decimals of accuracy

My latest take on it. Still have to test this

esphome:
  name: esp32-phasebox
  includes:
  - reference.h
  - secondary.h
  - tertiary.h
  - EmonLib.h
  - EmonLib.cpp

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "XJeIO62+KUSond2T6Fi53ymTfhp1TR0pekD1qyLS3gY="

ota:
  password: "6844d542c72b59d703039a2163ccc9a9"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Phasebox Fallback Hotspot"
    password: "GhNn0yKEd9Yd"

captive_portal:

switch:
  - platform: gpio
    pin: GPIO19
    name: "IN1"
    id: in1_switch
    interlock: &in_switch_interlock [in1_switch, in2_switch, in3_switch]
    interlock_wait_time: 2s
    inverted: true
    restore_mode: ALWAYS_ON
    
  - platform: gpio
    pin: GPIO18
    name: "IN2"
    id: in2_switch
    interlock: *in_switch_interlock
    interlock_wait_time: 2s
    inverted: true
    restore_mode: ALWAYS_OFF
    
  - platform: gpio
    pin: GPIO17
    name: "IN3"
    id: in3_switch
    interlock: *in_switch_interlock
    interlock_wait_time: 2s
    inverted: true
    restore_mode: ALWAYS_OFF
    
  - platform: gpio
    pin: GPIO16
    name: "Lamp Post"
    inverted: true
    restore_mode: ALWAYS_ON
  
  - platform: template
    name: "Reference"
    id: reference_switch
    turn_on_action:
      - then:
        - switch.turn_off: in2_switch
        - switch.turn_off: in3_switch
        - delay: 2s
        - switch.turn_on: in1_switch
        - switch.template.publish:
            id: reference_switch
            state: ON
    turn_off_action:
      - then:
        - switch.turn_off: in1_switch
        - switch.template.publish:
            id: reference_switch
            state: OFF
    
  - platform: template
    name: "Secondary"
    id: secondary_switch
    turn_on_action:
      - then:
        - switch.turn_off: in1_switch
        - switch.turn_off: in3_switch
        - delay: 2s
        - switch.turn_on: in2_switch
        - switch.template.publish:
            id: secondary_switch
            state: ON
    turn_off_action:
      - then:
        - switch.turn_off: in2_switch
        - switch.template.publish:
            id: secondary_switch
            state: OFF
            
  - platform: template
    name: "Tertiary"
    id: tertiary_switch
    turn_on_action:
      - then:
        - switch.turn_off: in1_switch
        - switch.turn_off: in2_switch
        - delay: 2s
        - switch.turn_on: in3_switch
        - switch.template.publish:
            id: tertiary_switch
            state: ON
    turn_off_action:
      - then:
        - switch.turn_off: in3_switch
        - switch.template.publish:
            id: tertiary_switch
            state: OFF

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new Reference();
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Reference"
    id: reference_sensor
    unit_of_measurement: V
    accuracy_decimals: 2

- platform: custom
  lambda: |-
    auto my_sensor = new Secondary;
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Secondary"
    id: secondary_sensor
    unit_of_measurement: V
    accuracy_decimals: 2

- platform: custom
  lambda: |-
    auto my_sensor = new Tertiary();
    App.register_component(my_sensor);
    return {my_sensor->supplyvoltage_sensor};
  sensors:
  - name: "Tertiary"
    id: tertiary_sensor
    unit_of_measurement: V
    accuracy_decimals: 2

interval:
  - interval: 5s
    then:
      - script.execute: changeover_script

script:
  - id: changeover_script
    then:
      - if:
          condition:
            - lambda: 'return id(reference_sensor).state > 110;'
          then:
            - while:
                condition:
                  - lambda: 'return id(reference_sensor).state > 110;'
                then:
                  - switch.turn_on: reference_switch
      - if:
          condition:
            and:
              - lambda: 'return id(reference_sensor).state < 110;'
              - lambda: 'return id(secondary_sensor).state > 110;'
          then:
            - while:
                condition:
                  and:
                    - lambda: 'return id(reference_sensor).state < 110;'
                    - lambda: 'return id(secondary_sensor).state > 110;'
                then:
                  - switch.turn_on: secondary_switch
      - if:
          condition:
            and:
              - lambda: 'return id(reference_sensor).state < 110;'
              - lambda: 'return id(secondary_sensor).state < 110;'
              - lambda: 'return id(tertiary_sensor).state > 110;'
          then:
            - while:
                condition:
                  and:
                    - lambda: 'return id(reference_sensor).state < 110;'
                    - lambda: 'return id(secondary_sensor).state < 110;'
                    - lambda: 'return id(tertiary_sensor).state > 110;'
                then:
                  - switch.turn_on: tertiary_switch
1 Like