Page 1 of 1

Differentiate once or twice pressing a switch

Posted: Sunday 11 November 2018 10:47
by saik
I have one push button and try to run different external scripts depending on number of pushes within 2 seconds.
If I push once, only the script for single pressed should be executed. If I push twice, only the script for double pressed should be executed.
Unfortunately, I have no clue how realize that with DzVents.

Re: Differentiate once or twice pressing a switch

Posted: Sunday 11 November 2018 11:55
by waaren
saik wrote: Sunday 11 November 2018 10:47 I have one push button and try to run different external scripts depending on number of pushes within 2 seconds.
If I push once, only the script for single pressed should be executed. If I push twice, only the script for double pressed should be executed.
Unfortunately, I have no clue how realize that with DzVents.
Assuming this is not a push-button capable of making the distinction between single or double push itself (like the Xiaomi push-button)

My personal preference would be to combine the two scripts into one script with an if / else block. But if they really have to stay separated, you need an activation script and two user variables, dummy switches or two different callbacks from an domoticz.openURL call. Implementation below is with two uservariables and a helper script.
Activation script

Code: Select all

--[[ -- doublePush
     This script requires two domoticz uservariables (integer type) set to 0
]]--

local buttonName  = "doublePush"                            -- Name  of your trigger button
local singlePush  = "scriptName1"                           -- Names of your uservariables (type integer and set to 0)
local doublePush  = "scriptName2"

return {
    on   =    { devices   =   { buttonName }},
    
    logging =   {   level   = domoticz.LOG_DEBUG ,     
                    marker  = "doublePush"},            
    
    data =  { lastState =  { initial = "" }},

    execute = function(dz, item )
        singlePush    = dz.variables(singlePush)
        doublePush    = dz.variables(doublePush)
        delaySeconds  = 2
        
        if item.state == "On" and dz.data.lastState == "On" and item.lastUpdate.secondsAgo <= delaySeconds then 
            singlePush.cancelQueuedCommands()
            doublePush.set(1)
            dz.data.lastState = ""
        elseif item.state ==  "On" then
            dz.data.lastState = "On"
            singlePush.set(1).afterSec(delaySeconds + 2) -- Maybe + 1 also works but to stay at the safe side
        else
            dz.data.lastState = item.state
        end
    end
}    
script1

Code: Select all

-- doublePush_script1
 
local singlePush  = "scriptName1"                           -- Names of your uservariables (type integer and set to 0)

return {
    on   =    { variables   =   { singlePush }},
    
    logging =   {   level   = domoticz.LOG_DEBUG ,     
                    marker  = "doublePush_script1"},            
    
    
    execute = function(dz)
        dz.log("Script 1 is executing",dz.LOG_DEBUG)
    end
}    
script2

Code: Select all

-- doublePush_script2
 
local doublePush  = "scriptName2"                           -- Names of your uservariables (type integer and set to 0)

return {
    on   =    { variables   =   {doublePush }},
    
    logging =   {   level   = domoticz.LOG_DEBUG ,     
                    marker  = "doublePush_script2"},            
    
    
    execute = function(dz)
        dz.log("Script 2 is executing",dz.LOG_DEBUG)
    end
}