How to listen to button single click and double click?

Hello everyone,

I have some buttons that I used to trigger scenes on single click, double click or long click.
I would like to remove all my scenes and use only AppDaemon instead.
However I do not manage to get anything triggering in AppDameon when I press the switch/Button.

self.listen_event(self.button_1click,'click', entity_id='button.light1_identify', click_type="single")
  def button_1click(self, event_name, data, kwargs):
    self.log("event_name:"+str(event_name)+"; data:"+str(data))
    self.log("btn1: single click")

If anyone has an idea, it would be very helpful.

Thanks in advance

Can you post the working code you use in Home Assistant to trigger the scenes? Also, try removing the click_type in the listen_event call to see if you get anything at all.

Thanks a lot for your idea, it helped me to figure out how to make it work.

here is the code that I have done so far:

self.listen_event(self.button1_1click, entity_id="button.button1_identify")`

and the callback:

  def button1_1click(self, event_name, data, kwargs):
    self.log("event_name:"+str(event_name))
    self.log("data:"+str(data))

and finally the answered I got for single click: ‘command’: ‘toggle’

event_name:zha_event
data:{'device_ieee': '00:12:4b:00:23:9f:3f:52', 'unique_id': '00:12:4b:00:23:9f:3f:52:1:0x0006', 'device_id': '59a6923c1340c95972a4f30c926d3f28', 'endpoint_id': 1, 'cluster_id': 6, 'command': 'toggle', 'args': [], 'params': {}, 'metadata': {'origin': 'LOCAL', 'time_fired': '2022-12-29T15:47:36.963588+00:00', 'context': {'id': '01GNF92HC3AT73JWWTGPV81X72', 'parent_id': None, 'user_id': None}}}

for a double click: ‘command’: ‘on’

event_name:zha_event
data:{'device_ieee': '00:12:4b:00:23:9f:3f:52', 'unique_id': '00:12:4b:00:23:9f:3f:52:1:0x0006', 'device_id': '59a6923c1340c95972a4f30c926d3f28', 'endpoint_id': 1, 'cluster_id': 6, 'command': 'on', 'args': [], 'params': {}, 'metadata': {'origin': 'LOCAL', 'time_fired': '2022-12-29T15:47:45.195107+00:00', 'context': {'id': '01GNF92SDBB48FVJDA9M5T4E3C', 'parent_id': None, 'user_id': None}}}

for a long press: ‘command’: ‘off’

event_name:zha_event
data:{'device_ieee': '00:12:4b:00:23:9f:3f:52', 'unique_id': '00:12:4b:00:23:9f:3f:52:1:0x0006', 'device_id': '59a6923c1340c95972a4f30c926d3f28', 'endpoint_id': 1, 'cluster_id': 6, 'command': 'off', 'args': [], 'params': {}, 'metadata': {'origin': 'LOCAL', 'time_fired': '2022-12-29T15:48:09.594903+00:00', 'context': {'id': '01GNF93H7TEFKG9BYJF3VS00DD', 'parent_id': None, 'user_id': None}}}

I don’t know if it helped but I also added a change in HA seen somewhere:
Going to “Developer tools/Events” and in the section “Listen to events” I added “zha_event”

Great that you got it working!

Finally it doesn’t work :pensive:
When I let the app running, I get all the callbacks (single click, double click) called permanently even if I do not click.

2022-12-29 18:16:02.064871 INFO buttons: btn laura: single click
2022-12-29 18:16:02.126218 INFO buttons: btn laura: single click
2022-12-29 18:16:02.221216 INFO buttons: btn laura: single click
2022-12-29 18:16:02.303424 INFO buttons: btn laura: single click
2022-12-29 18:16:02.387863 INFO buttons: btn laura: single click
2022-12-29 18:16:02.495270 INFO buttons: btn laura: single click
2022-12-29 18:16:02.585833 INFO buttons: btn laura: single click
2022-12-29 18:16:02.687990 INFO buttons: btn laura: single click
2022-12-29 18:16:02.829068 INFO buttons: btn laura: single click
2022-12-29 18:16:02.871461 INFO buttons: btn laura: single click
2022-12-29 18:16:02.925431 INFO buttons: btn laura: single click
2022-12-29 18:16:03.006827 INFO buttons: btn laura: single click
2022-12-29 18:16:03.107624 INFO buttons: btn laura: single click
2022-12-29 18:16:03.175270 INFO buttons: btn laura: single click
2022-12-29 18:16:03.251393 INFO buttons: btn laura: single click
2022-12-29 18:16:03.322738 INFO buttons: btn laura: single click

However I set the listener on command=“toggle” only:

self.listen_event(self.button1_1click, entity_id="button.button1_identify", command="toggle")

it looks like the event needs to be acknowledged so it doesn’t call the event callback again and again

Finally I got it right now that I switched from ZHA to Z2MQTT. I do not listen for Event but for state (otherwise it was the same issue):

> self.listen_state(self.button_state,self.button)
> 
> #--------------------------------------------------------------
>   def button_state(self,entity,attribute,old,new,kwargs):
>     #self.log("attribute:"+str(attribute))
>     #self.log("old:"+str(old))
>     #self.log("new:"+str(new))
> 
>     if self.type=="single" and (old=='single' or new=='single'):
>       #self.log("single click")
>       self.toggle(self.light)
> 
>     if self.type=="double" and (old=='double' or new=='double'):
>       #self.log("double click")
>       self.toggle(self.light)
>     if self.type=="state":
>       #self.log("state changed")
>       self.toggle(self.light)

as a complement, for one of my switches I didn’t get anything triggered until I found in Zigbee2MQQT “Settings (Specific)” tab that an option “State action” can be set to true.