Trigger different actions on a single, double or double click on a binary sensor

This automation might be quite useful for me. I have attached Shelly 2.5 to my roller shutters. They are controlled by the Shelly via Home Assistant, or old school via two buttons (Up, Down).
The buttons return to their original possition, when released (off). Currently the buttons are connected to the relais of the Shelly, but they can be configured as detached buttons. Then they would not trigger anyhting in the Shelly directly, but the state of the button can be read by home assistant and home assistant can trigger actions.
I would like to use this to for example control the roller shutter (cover) in a normal manner on a single click (e.g. call close cover when down is pressed once, call stop cover, when pressed again). But if a double click is detected, close cover for all covers in the room could be triggered. I think this blueprint will help me to get there.

I tried to import the blueprint into my configuration, but received an error.
Is there a way to manually copy the automation? If not using the blueprint itself, where would the input definitions be defined?
I have now tried the following:

- alias: Test - Office Cover Down Pressed
  id: 'test_office_cover_down_pressed'
  trigger:
    - platform: state
      entity_id: binary_sensor.office_shutter_button_down
      to: 'on'
  action:
    - variables:
          button_id: binary_sensor.office_shutter_button_down
    - wait_template: "{{ is_state(button_id, 'off') }}"
      timeout: 0.3
      continue_on_timeout: true
    - choose:
        - conditions: "{{ not wait.completed }}" # long click
          service: notify.mobile_app_oneplus_6t
          data:
            title: 'Down Button Pressed'
            message: 'Long Click Detected'
      default:  
        - wait_template: "{{ is_state(button_id, 'on') }}" # second click?
          timeout: 0.3
          continue_on_timeout: true
        - choose:
            - conditions: "{{ not wait.completed }}" #single click
              service: notify.mobile_app_oneplus_6t
              data:
                title: 'Down Button Pressed'
                message: 'Short Click Detected'
          default: 
            service: notify.mobile_app_oneplus_6t
            data:
              title: 'Down Button Pressed'
              message: 'Double Click Detected'

But the service call at that position seems to be wrong.

What error did you receive when importing the blueprint?

I also noticed that the blueprint defines a delay, which is defined to be a decimal number (the mask only accepts integers (i.e. full numbers). However the delay is not used by the automation. Here the timeout is hardcoded to 0.3 seconds not using the delay defined above.

Thanks, I fixed this.
For info, I renamed the blueprint (to change switch with binary sensor) so a reimport is needed. Sorry for that

Thanks. I managed to import the blueprint correctly and setup an automation to use it. I had to tweak the delay a little bit as initially mostly a longpress was detected, no matter what how long/often I pressed. I have increased the value from 0.3 to 0.5 and am now receiving the correct notifications for each button press. Thanks for the blueprint.
I will now need to find out how to action the cover correctly according to its current state and see if that introduced delay is recognizable in the short click scenario as it is expected to open/close/stop the cover immediately.

I made a little improvement.
If no double click action is defined, do not wait for the timeout of the delay after the first click -> it makes the automation more reactive in the scenario where no double click is configured.

1 Like

Thank you it is working just fine for me. If have implemented it with the cover actions now:

- alias: Test - Office Cover Down Pressed
  id: 'test_office_cover_down_pressed'
  description: ''
  use_blueprint:
    path: >-
      slashback/trigger-different-actions-on-a-single-double-or-double-click-on-a-binary-sensor.yaml
    input:
      delay: '0.5'
      switch_id: binary_sensor.office_shutter_button_down
      long_click_action:
        - service: >
            {% if is_state('cover.upstairs', 'open') %}
              cover.close_cover
            {% else %}
              cover.stop_cover
            {% endif %}
          entity_id: cover.upstairs
      short_click_action:
        - service: >
            {% if is_state('cover.office', 'open') %}
              cover.close_cover
            {% else %}
              cover.stop_cover
            {% endif %}
          entity_id: cover.office

Thanks for the blueprint!
That is exactly what I was searching for.
I only have the problem that a single press is not reliably detected by the blueprint. I have a button connected to a Shelly 1. The Single press is not detected when I press it really short. (Like when you go in a room and you quickly want to switch on the light)
However it is possible to trigger a single press by pressing slightly longer.
I have tried to play around with the timeout but this only effects the Long press.

When I listen to event shellyforhass.click in the Developer Tools in Home Assistant however it will be displayed as following:


{
    "event_type": "shellyforhass.click",
    "data": {
        "entity_id": "binary_sensor.shelly_shsw_1_8caab55xxxxx_switch",
        "click_type": "single"
    },
    "origin": "LOCAL",
    "time_fired": "2021-02-12T09:53:48.036867+00:00",
    "context": {
        "id": "xxxxx",
        "parent_id": null,
        "user_id": null
    }
}

Do you experience the same effect? Do you have any tips for me?

Hello,
I use the same delay to detect a short vs. long click as for the pause between 2 clicks to consider it as a double click.
So basically, if the time between the binary_sensor being on and off again is < the delay (0,3 s by default), it will be considered as a single click (if the binary sensor doesn’t change to on again before the delay (0,3 s by default)). Otherwise, it is consider as a long click.

So what I’m saying is that, in theory, if a on/off event occurs on the binary sensor, the automation will trigger a single or a long click action, but it will trigger an action.

I suggest you verify that, when you do you ‘quick single click’, the binary sensor really goes from off to on. Maybe it is you shelly that doesn’t trigger an event if it is two quick/or trigger a wrong event??

Okay, thanks.
A stupid question: (sorry I just started with home assistant. :grin: )

How can I do this verification of the binary_sensor?
I tried already with Developer Tools > EVENTS > Listen to events
and entered binary_sensor.* but I can’t see any event when I press the button.

When I enter shellyforhass.click there I will get the following for a single (quick single click) and long press:

Event 1 fired 8:16 PM:
{
    "event_type": "shellyforhass.click",
    "data": {
        "entity_id": "binary_sensor.shelly_shsw_25_40f5xxxxx_2_switch",
        "click_type": "long"
    },
    "origin": "LOCAL",
    "time_fired": "2021-02-12T19:16:33.591085+00:00",
    "context": {
        "id": "xxxxx",
        "parent_id": null,
        "user_id": null
    }
}
Event 0 fired 8:16 PM:
{
    "event_type": "shellyforhass.click",
    "data": {
        "entity_id": "binary_sensor.shelly_shsw_25_40f5xxxxx_2_switch",
        "click_type": "single"
    },
    "origin": "LOCAL",
    "time_fired": "2021-02-12T19:16:29.288515+00:00",
    "context": {
        "id": "xxxxx",
        "parent_id": null,
        "user_id": null
    }
}

You have to go in the States screen. And look at the historic of your binary sensor.

I can’t see any entry from this particular shelly. But I can see the binary sensor value changing when I go to Configuration > Device > [the shelly I want to use] > [select the binary sensor I want].
See the screenshot below.

The Problem is that the “quick single click” isn’t detected here!

Which Shelly integration do you use? the official or the one from HACS?

I’m not using Shelly

This is exactly what I was after thanks slashback.

Question though is it easy enough to add in the option of multiple binary inputs.
for example I have a set of lights that can be controlled from 4 locations, atm I have just duplicated this 4 times but would be great if it could all be done in.

Thanks again

It’s a possible evolution indeed.

Thanks, slashback!
Although I’m rather new to Home Assistant, I was looking for that to implement in my ESP32. I’m planning to use 3 touchpads and one push-button to start 12 different automations and your blueprint seems to enable that (I still have to learn how to quadruplicate that…) . Anyway, I’ll study that and post here either questions or ideas.

Amazing! This is exactly what I was looking for. Thanks so much, slashback. I’m using it with an ESPhome binary sensor connected to a push-button.

For me, the short click does not work. Double click works fine. But any click, no longer how long I hold it down for registers as a long click. I have tried to change the time interval, but this doesn’t help.

EDIT: so it turn out the button on ESP home that I was using defaults to an ‘on’ state when not pressed and ‘off’ when pressed. And that’s why the short press was not working. I inverted the state value and now it works great. Thanks again.

Hi! Thanks for your blueprint!
I use blitzwolf switch module with analog switches, and create template binary sensor, that listen status of switch. And by this blueprint, i can use double click actions for my home)
Thanks a lot!

Thank you very much. Pretty close to what I was looking for.
Just one question - being very new to this whole automation topic.
For click, dbl click and long click I can add actions - one for each light.
How would I change this to be able to toggle groups of lights?

Cheers!

You can just set the entity_id with the group entity