Multi Toggle Actions

I have a Shelly 2.5 running ESPHome in my master … uh … toilet closet. It’s relays are connected to a fan and a light switch (but kept always on because it has a smart light) and both inputs are connected to toggle switches. This closet is kind of my toy / automation playroom as it’s low impact on the rest of the house. :sweat_smile: I wanted to play around with changing the light conditions with multi-toggles of the bottom/fan switch (for low light level settings) and multi-toggles of the top/light switch (for high light level settings).

The ESPHome documentation has a good section on multi-presses for buttons, but I could not for the life of me do something useful with on_multi_click for the toggle switches. Messed with the lockout timer and higher debug settings, and it just wasn’t working how I imagined it. I also couldn’t really find anything here that dealt with multi-toggle actions. So, I ended up with the following method and am just wanting to share / ask if anyone has a better method.

script:
  - id: fan_action_1
    mode: parallel
    then:
      - switch.toggle: shelly_relay_1
  - id: fan_action_2
    mode: parallel
    then:
      - homeassistant.service:
          service: script.set_light_ct
          data:
            light_name: light.toilet_closet_bulb
            kelvin: '2000'
            brightness_pct: '50'
  - id: fan_action_3
    mode: parallel
    then:
      - homeassistant.service:
          service: script.set_light_ct
          data:
            light_name: light.toilet_closet_bulb
            kelvin: '2000'
            brightness_pct: '25'
  - id: fan_action_4
    mode: parallel
    then:
      - homeassistant.service:
          service: script.set_light_ct
          data:
            light_name: light.toilet_closet_bulb
            kelvin: '2000'
            brightness_pct: '10'
  - id: fan_toggle_1
    mode: single
    then:
      - delay: 500ms
      - script.execute: fan_action_1
  - id: fan_toggle_2
    mode: single
    then:
      - delay: 500ms
      - script.execute: fan_action_2
  - id: fan_toggle_3
    mode: single
    then:
      - delay: 500ms
      - script.execute: fan_action_3

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
    name: "Fan Input"
    on_state:
      then:
        - if:
            condition:
              or:
                - script.is_running: fan_toggle_1
                - script.is_running: fan_toggle_2
                - script.is_running: fan_toggle_3
            then:
              - if:
                  condition:
                    - script.is_running: fan_toggle_3
                  then:
                    - script.stop: fan_toggle_3
                    - script.execute: fan_action_4
              - if:
                  condition:
                    - script.is_running: fan_toggle_2
                  then:
                    - script.stop: fan_toggle_2
                    - script.execute: fan_toggle_3
              - if:
                  condition:
                    - script.is_running: fan_toggle_1
                  then:
                    - script.stop: fan_toggle_1
                    - script.execute: fan_toggle_2
            else:
              script.execute: fan_toggle_1

The idea is that you run scripts with delays each time the switch is toggled. If the delay in a script finishes, it executes the action. If no scripts are running, you start script 1. If script N is running already, you stop script N and start script N+1, and if you are at the end of the sequence, you execute the final action.

I hope this (or the comments below with maybe a better solution) help someone out!

Hey Tony, came here with basically the question - hoping that ESPhome had support for multiple toggles of a switch. Haven’t been able to find anything, but Tasmota does allow for a “2X Change”. The documentation is here, look for SwitchMode 8.

Let me know if you have found any better solutions, I’d actually prefer a “3X Change” or “4X Change”.