Page 1 of 1

Using an Aqara door sensor as a selector switch

Posted: Monday 18 January 2021 0:39
by peterbos
I posted this on the forum earlier but in a subject that most people probably wont't read.

I have a couple of Aqara door & window sensors which I hacked and now use to make my light switches smart. On the internet you can find out how to do that. But I wanted a more than On/Off switches only and wrote a dzVents script that activates different actions based on how often the switch is clicked.

In my case:
1 click = toggle the lights on/of
2 clicks = jump to the next light settings (daytime, evening, nighttime, morning)
3 clicks = switch the light to maximum brightness and cool white (handy when your DIY'ing)
4 clicks = overrule the timer setting, so the lights are not switched off after 15 minutes

The great thing is that you can use a rather cheap component (the Aqara switches cost me 13 euros each but are much cheaper when ordered from China) to create a great switch.

This is the code framework:

Code: Select all

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

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

    execute = function(dz, item)

        if (item.name == 'Switch') 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
}
Peter