Physical toggle switch - Multiple states

Hello everyone,
I’m new on ESPHome and I’m trying to replace a physical toggle button logic with a SonOff T3.

In my living room I’ve six spotlights that are managed with a physical relay that change the number of them by pressing the toggle switch multiple times.
I can rapresent the 4 states in this way:
0 → All Spotlights are off
1 → All Six Spotlights are on
2 → 4 of 6 Spotlights are on
3 → 2 of 6 Spotlights are on

I’ve just configured ESPhome to turn automatically off the button 250ms after the pression of the physical button but I’d like to have a virtual object on HA where, selecting the “number of Spotlights”, it knows how many times the button should be pressed for reach the goal or, if I press the button X times, HA knows in wich state the lights are and, if I turn off the light object via HA, it knows how many steps it should do for reach the 0 state.

Hope you can help me.
Kindly regards,
Danny

Sounds like a select component would do it?

Another good option is this option on the binary sensor component (on_multi_click):

Binary Sensor Component — ESPHome

1 Like

These are two good start point! Thank you.
Maybe the Select component is the more appropriate to my needs but, as I have described before, my problem is that is not a “logic” selector: everytime I change the option on the dropdown(?), the relay has to act with a specific ON->OFF order.

In other words:
If I start from OFF state, and I want to have four spotlights ON, the relay has to turn two times. Same if I want to shut down all of them.
If I want to have all the six spotlights turned ON, and I’m on 4 spotlight state, the relay has to turn 3 times.

That’s because the states that I’ve previously described, are cyclic and at every click on the switch (or relay cycle) the spotlights react as the table below:

  • 0 → All Spotlights are off
    (One click on the switch)
  • 1 → All Six Spotlights are on
    (One click on the switch)
  • 2 → 4 of 6 Spotlights are on
    (One click on the switch)
  • 3 → 2 of 6 Spotlights are on
    (One click on the switch)
  • 0 → All Spotlights are off
    […]

Hope I was more clear than before!
Thanx in advance.
Danny

If anyone is interested in the solution, I put here what I just successfully configured on my SonOFF T1US:

substitutions:
  device_name: "salone-switch"
  friendly_name: "Salone-Switch"
  ip_address: [...]
  wifi_ssid: !secret wifi_ssid
  wifi_pass: !secret wifi_password
  api_encryption: "[...]"
  ota_pass: "[...]"
  ap_pass: "[...]"

<<: !include .common-config.yaml

esphome:
  name: "${device_name}"
  platform: ESP8266
  board: esp01_1m

sensor:
  - <<: !include .sensor-config.yaml

text_sensor:
  - <<: !include .text_sensor-config.yaml

switch:
  - <<: !include .switch-config.yaml

binary_sensor:
  - <<: !include .binary_sensor-config.yaml

  - platform: gpio
    pin:
      number: GPIO0
      mode:
        input: true
        pullup: true
      inverted: true
    id: button_1
    on_press:
      then:
        - select.next: light_1

globals:
  - id: selected_index
    type: int
    restore_value: no
    initial_value: '0'

select:
  - platform: template
    name: "${friendly_name}0"
    id: light_1
    options:
     - "OFF"
     - "6 Spotlights"
     - "2 Spotlights"
     - "4 Spotlights"
    initial_option: "OFF"
    optimistic: true
    on_value:
      then:
        - while:
            condition:
              lambda: |-
                return id(light_1).active_index() != id(selected_index);
            then:
              - script.execute: relay_cycle
              - script.wait: relay_cycle
              - lambda: !lambda |-
                  auto size = id(light_1).size();
                  id(selected_index) += 1;
                  if (id(selected_index) == size) { id(selected_index) = 0; }

1 Like