Tag Archives: automations

Multi click workaround for single click buttons in Home Assistant

I recently bought a Leviton Decora Smart Controller Switch with the hopes that I could put it in my bathroom for quick and easy music selection. I was dismayed to find out that the device doesn’t support multi-click – it registers single click only. Thanks to Home Assistant, I was able to bypass this problem. It took a bit of doing but thanks to this forum post I was able to get it working.

All I wanted this switch to do was play music based on which button was tapped and how many times. The switch was designed to also control a light/fan circuit, but I didn’t need that functionality. I wired it into the wall with line, neutral, and ground, but didn’t put anything on the load wire. This gave me the freedom to use all four buttons without messing anything else up. I wanted local control, so I set this device up via HomeKit.

To accomplish multi-click functionality, I wrote an automation that does the following:

  • Trigger on click of the button
  • Repeat the trigger five times – one for each trigger plus an extra (not sure why, but needed to be 5 instead of 4)
  • Wait up to half a second for another trigger, caused by a subsequent press of the button. If there is no subsequent press, then do the first action. If there IS a subsequent press, do the second action, and so on through the 4th action.
  • Stop the automation if a timeout has been reached (station selected.)
  • Run in single mode – don’t run subsequent automations if one is already running.
  • The action is simply changing an option on an input_select helper. I have a separate automation to initiate the speaker to play the desired station based on the value of the input_select helper.

Here is the full automation:


alias: Master Bath scene controller Button 1 multi click
description: ""
triggers:
  - device_id: 8dad5dce6044df4d28b566a412379bdf
    domain: homekit_controller
    type: button1
    subtype: single_press
    trigger: device
    enabled: true
conditions: []
actions:
  - repeat:
      count: 5
      sequence:
        - wait_for_trigger:
            - entity_id:
                - >-              event.master_bath_scene_controller_stateless_programmable_switch_1
              trigger: state
          timeout:
            seconds: 0.5
        - if:
            - condition: template
              value_template: "{{ not wait.trigger }} "
          then:
            - choose:
                - conditions:
                    - condition: template
                      value_template: "{{ repeat.index  == 2 }} "
                  sequence:
                    - action: input_select.select_option
                      metadata: {}
                      data:
                        option: Station_1
                      target:
                        entity_id: input_select.master_bath_stations
                - conditions:
                    - condition: template
                      value_template: "{{ repeat.index  == 3 }} "
                  sequence:
                    - action: input_select.select_option
                      metadata: {}
                      data:
                        option: Station_2
                      target:
                        entity_id: input_select.master_bath_stations
                - conditions:
                    - condition: template
                      value_template: "{{ repeat.index  == 4}} "
                  sequence:
                    - action: input_select.select_option
                      metadata: {}
                      data:
                        option: Station_3
                      target:
                        entity_id: input_select.master_bath_stations
                - conditions:
                    - condition: template
                      value_template: "{{ repeat.index  == 5 }} "
                  sequence:
                    - action: input_select.select_option
                      metadata: {}
                      data:
                        option: Station_4
                      target:
                        entity_id: input_select.master_bath_stations
            - stop: ""
mode: single

It works quite well! I’m so pleased. I don’t fully understand why I had to start the index with 2. If I didn’t, then the first station would not play on one click – it just got skipped. ChatGPT also doesn’t quite understand why, it seems, as its explanation says that a single click should do nothing and five clicks should do something. However, a single click does in fact play Station_1, and five clicks does nothing. All the desired stations play when the button is pressed the requisite number of times.


ChatGPT generated explanation

This Home Assistant automation is designed to respond to button presses from a HomeKit-enabled scene controller in the master bathroom. Here is a detailed breakdown of how it works:

Overview

  • Alias: Master Bath scene controller Button 1 multi click
  • Trigger: A single press of button 1 on the scene controller.
  • Actions: Repeats a sequence up to 5 times, checking for additional button presses. Depending on the number of presses, it selects a different option from an input select entity.

Triggers

The automation is triggered when button 1 (type: button1) on the scene controller (device_id: 8dad5dce6044df4d28b566a412379bdf) is pressed once (subtype: single_press).

Conditions

There are no conditions specified, so the actions will always run when the trigger occurs.

Actions

The actions consist of a repeat loop that runs up to 5 times. Here is a step-by-step explanation of the actions:

  1. Repeat Loop: The loop will run up to 5 times (count: 5).
  2. Wait for Trigger: Inside the loop, it waits for another state change event from the scene controller’s button 1 (event.master_bath_scene_controller_stateless_programmable_switch_1) for up to 0.5 seconds (timeout: 0.5).
  3. Check for Additional Presses:
    • If no additional press is detected within the timeout period ({{ not wait.trigger }}), it checks the current index of the repeat loop (repeat.index).
    • Depending on the value of repeat.index, it selects a different option from the input_select.master_bath_stations entity.
  4. Select Options Based on Press Count:
    • If repeat.index is 2: Selects Station_1.
    • If repeat.index is 3: Selects Station_2.
    • If repeat.index is 4: Selects Station_3.
    • If repeat.index is 5: Selects Station_4.
  5. Stop Action: If an option is selected (based on the number of presses), the loop stops executing further (stop: "").

Mode

The automation runs in single mode, which means it will not start a new instance of the automation if there is already one running.

Example Use Case

  • Single Press: If the button is pressed once, it waits for up to 0.5 seconds for additional presses. If no additional presses are detected, nothing happens.
  • Double Press: If the button is pressed twice within 0.5 seconds, Station_1 is selected.
  • Triple Press: If the button is pressed three times within 0.5 seconds, Station_2 is selected.
  • Quadruple Press: If the button is pressed four times within 0.5 seconds, Station_3 is selected.
  • Five Presses: If the button is pressed five times within 0.5 seconds, Station_4 is selected.

This automation allows for different actions to be taken based on the number of times the button is pressed within a short period.