Conditional button presses to trigger different relays all local to ESP for aquaponics pumps

Hi there, I’m trying to see if I can accomplish this using only a single button. without relying on input booleans and automations within HA (in the event wifi is down, but i still have power at the ESP and want local manual control).

Essentially I have 4 relays and one button. what i want to accomplish

  • if I hold the button for 5 -7 seconds, switch on relay 4 (my manual override mode indicator led)
  • if i hold the button for 3-4 seconds, switch off relay 4
  • only while relay 4 is switched on
    – on single press, toggle relay 1
    – on double press, toggle relay 2
    – on triple press, toggle relay 3

Is this possible? Below is what i’ve worked up so far, but seems to want to toggle the 3 relays on the first time activating the “dev_mode”, other-wise, seems to act as intended.

hope it helps others and maybe some of you might find a better way to automate this. I’m not sure if there is a better way to handle this


binary_sensor:
  - platform: template
    name: "ESP Aquaponics - Program Mode Active"
    id: dev_mode
    lambda: |-
      if (id(esp_r4).state) {
        // Program Mode Active.
        return true;
      } else {
        // Program Mode Off.
        return false;
      }
  
  ## button - toggle relay
  - platform: gpio
    name: "ESP Aquaponics Pump Button"
    pin:
      number: 21
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_on_off: 20ms
    
    on_multi_click:
    # Dev Mode ON
    - timing:
        - ON for 5s to 7s
        - OFF for at least 0.5s
      then:
        - logger.log: "Dev Mode On"
        - switch.turn_on: esp_r4
        - logger.log: "Relay 4 turned on"
    # dev mode active
    - timing:
        - ON for 3s to 4s
        - OFF for at least 0.5s
      then:
        - logger.log: "Dev Mode OFF"
        - switch.turn_off: esp_r4
        - logger.log: "Relay 4 turned off"
    # Relay 1 Toggle
    - timing:
        - ON for at most 1s
        - OFF for at least 0.7s
      then:
        - wait_until:
            binary_sensor.is_on: dev_mode
        - logger.log: "Single Short-Clicked"
        - switch.toggle: esp_r1
    # Relay 2 Toggle
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at least 0.2s
      then:
        - wait_until:
            binary_sensor.is_on: dev_mode
        - logger.log: "Double Clicked"
        - switch.toggle: esp_r2
    # Relay 3 Toggle
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at least 0.2s
      then:
        - wait_until:
            binary_sensor.is_on: dev_mode
        - logger.log: "Triple Clicked"
        - switch.toggle: esp_r3

      
      
switch:
  
  ## restart
  - platform: restart
    name: "ESP Fish Pond Restart"
  
  - platform: gpio
    name: "ESP R1 - Aquaponics Pump"
    id: esp_r1
    pin: 
      number: 17
      inverted: false
      
  - platform: gpio
    name: "ESP R2 - Air Pump"
    id: esp_r2
    pin: 
      number: 16
      inverted: false
      
  - platform: gpio
    name: "ESP R3 - Heater"
    id: esp_r3
    pin: 
      number: 26
      inverted: false
      
  - platform: gpio
    name: "ESP R4 - Aux"
    id: esp_r4
    pin: 
      number: 25
      inverted: false

Hi, I dont think that condition inside timing is not possible but you can do this with creating 3 template switches which can check condition and act accordingly. Try the below code.

binary_sensor:
  - platform: template
    name: "ESP Aquaponics - Program Mode Active"
    id: dev_mode
    lambda: |-
      if (id(esp_r4).state) {
        // Program Mode Active.
        return true;
      } else {
        // Program Mode Off.
        return false;
      }
  
  ## button - toggle relay
  - platform: gpio
    name: "ESP Aquaponics Pump Button"
    pin:
      number: 21
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_on_off: 20ms
    
    on_multi_click:
    # Dev Mode ON
    - timing:
        - ON for 5s to 7s
        - OFF for at least 0.5s
      then:
        - logger.log: "Dev Mode On"
        - switch.turn_on: esp_r4
        - logger.log: "Relay 4 turned on"
    # dev mode active
    - timing:
        - ON for 3s to 4s
        - OFF for at least 0.5s
      then:
        - logger.log: "Dev Mode OFF"
        - switch.turn_off: esp_r4
        - logger.log: "Relay 4 turned off"
    # Relay 1 Toggle
    - timing:
        - ON for at most 1s
        - OFF for at least 0.7s
      then:
        - switch.turn_on: relay1
    # Relay 2 Toggle
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at least 0.2s
      then:
        - switch.turn_on: relay2
    # Relay 3 Toggle
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at most 1s
        - ON for at most 1s
        - OFF for at least 0.2s
      then:
        - switch.turn_on: relay3

      
      
switch:
  - platform: template
    optimistic: true 
    name: "Relay 1"
    id: relay1 
    on_turn_on:
        - if:
            condition:
              switch.is_on: esp_r4              
            then:
                - wait_until:
                    binary_sensor.is_on: dev_mode
                - logger.log: "Single Short-Clicked"
                - switch.toggle: esp_r1 
                - switch.turn_off: relay1
  - platform: template
    optimistic: true 
    name: "Relay 2"
    id: relay2
    on_turn_on:
        - if:
            condition:
              switch.is_on: esp_r4              
            then:
                - wait_until:
                    binary_sensor.is_on: dev_mode
                - logger.log: "Double Clicked"
                - switch.toggle: esp_r2 
                - switch.turn_off: relay2
  - platform: template
    optimistic: true 
    name: "Relay 3"
    id: relay3
    on_turn_on:
        - if:
            condition:
              switch.is_on: esp_r4              
            then:
                - wait_until:
                    binary_sensor.is_on: dev_mode
                - logger.log: "Triple Clicked"
                - switch.toggle: esp_r3
                - switch.turn_off: relay3
  ## restart
  - platform: restart
    name: "ESP Fish Pond Restart"
  
  - platform: gpio
    name: "ESP R1 - Aquaponics Pump"
    id: esp_r1
    pin: 
      number: 17
      inverted: false
      
  - platform: gpio
    name: "ESP R2 - Air Pump"
    id: esp_r2
    pin: 
      number: 16
      inverted: false
      
  - platform: gpio
    name: "ESP R3 - Heater"
    id: esp_r3
    pin: 
      number: 26
      inverted: false
      
  - platform: gpio
    name: "ESP R4 - Aux"
    id: esp_r4
    pin: 
      number: 25
      inverted: false

You may have to check the code for errors and mismatches but I think you have got the idea.