Page 1 of 1

Doubleclick with a simple pushbutton

Posted: Monday 23 November 2020 13:47
by peterbos
Hi,

I have a pushbutton that can have only two states: Open and Closed. I want to use it with single and double clicks. For example when I click the button twice within 750 millisecs it should switch lamp 1 on, if I push the button once (= more than 750 ms after the previous click) lamp 2 must be switched on. I know how to describe in normal language but am not able to translate it to dzVents:

- when the button is clicked check if it was last clicked less than 750 ms before
- if yes then switch on lamp 1, if no then wait for 750 ms max
- if there's no new click within 750 ms then switch on lamp 2

I hope someone can help me!

Thanks in advance!
Peter

Re: Doubleclick with a simple pushbutton

Posted: Monday 23 November 2020 14:26
by waaren
peterbos wrote: Monday 23 November 2020 13:47 I have a pushbutton that can have only two states: Open and Closed. I want to use it with single and double clicks.
Can you please show the loglines where the clicks are registered? This to be certain the 750 ms are recognized by domoticz. If domoticz does not get these clicks at the interval you need, dzVents cannot help.

Re: Doubleclick with a simple pushbutton

Posted: Monday 23 November 2020 14:45
by peterbos
Hi Waaren,

I use the Aqara Door & Windows sensor with the reed contact being replaced by a pushbutton. I tried two double clicks:

2020-11-23 14:33:21.146 Status: dzVents: Info: Handling events for: "PushButton", value: "Closed"
2020-11-23 14:33:21.388 Status: dzVents: Info: Handling events for: "PushButton", value: "Open"
2020-11-23 14:33:21.685 Status: dzVents: Info: Handling events for: "PushButton", value: "Closed"
2020-11-23 14:33:21.919 Status: dzVents: Info: Handling events for: "PushButton", value: "Open"

2020-11-23 14:33:40.101 Status: dzVents: Info: Handling events for: "PushButton", value: "Closed"
2020-11-23 14:33:40.171 Status: dzVents: Info: Handling events for: "PushButton", value: "Open"
2020-11-23 14:33:40.256 Status: dzVents: Info: Handling events for: "PushButton", value: "Closed"
2020-11-23 14:33:40.421 Status: dzVents: Info: Handling events for: "PushButton", value: "Open"

The time between the two "Closed" values is 539 and 155 milliseconds - well under 750.

Yours,
Peter

Re: Doubleclick with a simple pushbutton

Posted: Monday 23 November 2020 17:09
by waaren
peterbos wrote: Monday 23 November 2020 14:45 The time between the two "Closed" values is 539 and 155 milliseconds - well under 750.
The script logic could look like below. I tested it with a normal switch and somewhat longer times so I would not be surprised if the domoticz timings are not accurate enough for your requirement.. but it's worth a try :)

Code: Select all

return
{
    on =
    {
        devices =
        {
            'PushButton',
        },
        customEvents =
        {
            'Single Click',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all ok
        marker = 'Double click',
    },

    data =
    {
        millisecondsAgo =
        {
            initial = -1,
        },
    },

    execute = function(dz, item)
        dz.log('at script start ' .. dz.data.millisecondsAgo, dz.LOG_DEBUG )

        lamp1 = dz.devices('lamp1')
        lamp2 = dz.devices('lamp2')

        if item.isCustomEvent then
            dz.log("item.isCustomEvent --> "  .. dz.data.millisecondsAgo ,dz.LOG_DEBUG)
            if dz.data.millisecondsAgo ~= -1 then
                dz.log("dz.data.millisecondsAgo ~= -1 -->> "  .. dz.data.millisecondsAgo ,dz.LOG_DEBUG)
                lamp2.switchOn()
                dz.data.millisecondsAgo = -1
            end
            return
        end

        if item.state == 'Close' and dz.data.millisecondsAgo == -1 then
            dz.log("item.state == 'Close' and dz.data.millisecondsAgo == -1" , dz.LOG_DEBUG)
            dz.data.millisecondsAgo = 0
            dz.emitEvent('Single Click').afterSec(1)
        elseif item.state == 'Open' and dz.data.millisecondsAgo == 0 then
            dz.log("item.state == 'Open' and dz.data.milliSecondsAgo == 0" , dz.LOG_DEBUG)
            dz.data.millisecondsAgo = item.lastUpdate.millisecondsAgo
        elseif item.state == 'Close' then
            dz.log("item.state == 'Close' -->> " .. item.lastUpdate.millisecondsAgo .. '   ' ..  dz.data.millisecondsAgo ,dz.LOG_DEBUG)
            if ( dz.data.millisecondsAgo + item.lastUpdate.millisecondsAgo ) < 750 then
                 dz.log("dz.data.millisecondsAgo + item.lastUpdate.millisecondsAgo ) < 750  --> "  .. dz.data.millisecondsAgo ,dz.LOG_DEBUG)
                lamp1.switchOn()
                dz.data.millisecondsAgo = -1
            end
        end
    end
}

Re: Doubleclick with a simple pushbutton

Posted: Wednesday 25 November 2020 0:03
by peterbos
Hi Waaren,

Your script got me in the right direction. It took me two nights of trial and error but I simplified it and made it into something that suits me even better: a single push button selector switch with virtually unlimited positions. I dropped the 750 ms and used the 1 second of the afterSec() command. I also used a virtual On/Off switch named MultiClick.

Code: Select all

return
{
    on =
    {
        devices =
        {
            -- the real push button
            'Schakelaar - Test',
            -- the virtual on/off switch
            'MultiClick',
        },
    },

    data =
    {
        Clicks =
        {
            initial = 0,
        },
    },

    execute = function(dz, item)

        if (item.name == 'Schakelaar - Test') and (item.state == 'Closed') then
            -- cancel any previous setState() commands which were issued less then 1 second ago
            dz.devices('MultiClick').cancelQueuedCommands()
            -- increase the number of clicks
            dz.data.Clicks = dz.data.Clicks + 1
            -- give the next setState() command
            -- this command will succeed if it's the last time the button is pushed
            -- otherwise it will be canceled too
            dz.devices('MultiClick').setState('On').afterSec(1)
            return
        end

        if (item.name == 'MultiClick') and (item.state == 'On') then
            -- dz.data.Clicks now holds the number of times the switch was clicked
            -- so do some action here, based on the number of clicks
            -- before the virtual switch and the number of clicks will be reset
            dz.devices('MultiClick').setState('Off').silent()
            dz.data.Clicks = 0
            return
        end

    end
}
I tested it and found no problems. Great solution: with a relatively cheap push button (I bought mine for 13 euros) you can maken a great switch.

Yours,
Peter