I am sorry for this code, I am sure it is quite a mess, as I am very inexperienced in appdaemon (and programming). I am trying to make it so when you push a button (binary sensor) it waits 350ms, if the button is pushed again within that time frame, it runs a script, if not, it runs another. This is my attempt at something so far:
#Standard import for appdaemon apps
import hassapi as hass
# Defining the class name to be DoubleClick
class DoubleClick(hass.Hass):
#The starting function
def ininitialize(self):
#Set counter variable to check if button has been pressed recently starts at 0
counter = 0
#Listing for the event that the button "binary_sensor.button is pressed and the running the function check
self.listen_state(self.check_double,"binary_sensor.button", new="on")
# this is the function running after the button is pressed, it is supposed to check if the same button is pressed again within 350 ms, if so, run script_double and start listening again, if no, run script single.
def check_double (self, entity, attribute, old, new, kwargs):
#how do i set the delay in milliseconds? Now it is set to 1 second.
delay = 1
if counter = 1 #checking if the button has been pressed within the prevous delay
self.turn_on(self.args["script_double"]) #run script double from the input arguments
counter = 0 #reset the counter for next button press
else:
counter = 1 #set the counter to 1, so if button is pressed before delay, it runs script doble
self.run_in(self.args["script_single"], delay) # Wait the delay, and then run the single click script.
counter = 0 #reset the counter if the single click script has been run
The biggest and most obvious problem is your misspelled “initialize” method.
Another issue is the “else” portion of “check_double”. If you set the counter to 1, schedule a “run_in” and then set the counter to 0, the counter is only equal to 1 for a VERY short period of time.
When you call “turn_on” you should be calling it on an entity. When you call “run_in” the first parameter should be a python method you want to run. I can’t tell if you have these parts right since you’re using parameters for both of them however, I am almost certain that your “script_single” doesn’t exist since I don’t see any other methods in this class.
In the “if” section for “counter = 1” you’ll need to cancel the run_in() that the “else” portion started. Unless you want both the “single click” and “double click” actions to fire.
Counter will not remember its value between runs. You need to use a class variable instead. Like… “self.counter = 1”.
Also, if your desire is to wait exactly 350ms to determine a single click, you aren’t doing that here. You’re waiting, effectively, 1 second (as determined by delay). This is probably better than 350ms, so I would leave that, just pointing out the difference.
Start with these changes:
- fix the misspelling
- add a method called “do_single” that your run_in calls. In that method set self.counter = 0 and do whatever should happen on a single click.
- fix the class variable issue.
If that doesn’t get you on the right track, post here again with the new code.
Thanks a lot, I have tried to illustrate my thinking/what i tried to obtain in the comments.
When you say the counter is only 1 for a very short time, is that shorter than the delay? I only want it to be 1 for 350ms, and if the button is pressed again, I run the doubleclick script.
The scripts i want to use as input arguments, so the function will run the scripts used as input (or that is what I am trying to do).
I don’t want the counter to remember its value between runs I think? I want it to be 0, unless the button was pressed the previous 350 ms.
Do you know how to set the delay in ms instead of seconds? (1 second is way to long, as it will make the button very sluggish)