Page 1 of 1

Dubble press

Posted: Wednesday 15 July 2020 13:12
by Varazir
Hello,

I have several IKEA zigbee devices and latest I bought a on/off/dim remote and wall plug and a light bulb ( that has dim support) that are going to be in the same room.

The remote has "Off, ON, Up, down, stop" as default selectors.

I have a script that I'm using for another IKEA remote/switch and I thought I wold borrow the dimmer part from it.

I like to control both the bulb and the wall plug with the same remote, example dubble press for one device and singel for another

How can I check if the button has pressed recently ?


Script:

Code: Select all

--
-- Creater Varazir ( And big help from waaren and others on domotiz forum )
-- e-mail: varazir .. gmail.com
-- Version: 1.5
--
-- groups = {'group1', 'group2', 'group3', 'group4', 'group5', 'group6', 'group7' }

return  {
    on =        {
                    devices         = { 'IKEA Remote*' }},

    logging =   { 
                    level           = domoticz.LOG_DEBUG, 
                    marker          = "IKEARemote" 
                },

    data =      { 
                    currentGroup    = { initial = 1 }
                }, 

    execute = function(dz, item)
        
        control = 
        {
            'Taklampa Sovrum',
            group1 =
            { 
                IKEAlamp        = { idx = 115, toggle = true, blink = true, dimmer = true}
            },
            'Uttag Sovrum',
			group2 =
            { 
                Blinder         = { idx = 157, toggle = true, blink = true }, 
            },
            'Rullgardin Sovrum',
			group3 =
            { 
                Blinder         = { idx = 59, toggle = true, blinder = true },
            },
            
            'Taklampa Hall',
            group4 =  
            {
                IKEAlamp        = { idx = 193, toggle = true, blink = true, dimmer = true }, 
			},
            
            'Taklampa Datorrum',
			group5 =
            { 
                IKEAlamp        =  { idx = 198, toggle = true, blink = true, dimmer = true}
            },
            
            'Taklampa Vardagsrum',
            group6 =
            { 
                IKEAlampGroup   = { idx = 195, toggle = true, blink = true, dimmer = true } 
            },
            
            'TVbänk',
			group7 = 
            { 
                Lamp            = { idx = 42, toggle = true, blink = true},
                TvGroup         = { idx = 2,  toggle = true, group = true}
            },
            
            'Rullgardin Vardagsrum',
			group8 =
            { 
                Blinder         = { idx = 83, toggle = true, blinder = true }, 

            },
        }

        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local selectedGroupNumber = dz.data.currentGroup
        local maxGroup = #control
        local dummyDimmer = dz.devices(82)

        local function doAction(action, direction)
            logWrite("10 Current group number........" .. selectedGroupNumber)
            selectedGroup = "group" .. selectedGroupNumber

            logWrite("11 Current selected group is..." .. selectedGroup)
            selectedControlGroup = control[selectedGroup]

            logWrite("12 Selected Control Group is a " .. type(control[selectedGroup]))
            
            switchSelectorGroupNumber = math.floor( selectedGroupNumber * 10)
            logWrite("13 Selected switchSelectorGroupNumber " ..  switchSelectorGroupNumber)
            dz.devices('IKEA Remote Groups').switchSelector(switchSelectorGroupNumber).silent()

            for device, attributes in pairs(selectedControlGroup) do
                logWrite("20 Current device is........." .. device)
                logWrite("21 Attribute type is........." .. type(attributes))
                logWrite("22 IDX is...................." .. attributes["idx"])
                currentIDx = attributes["idx"]
                
                if attributes["group"] then
                    logWrite("24 Current Device is group......." .. dz.groups(currentIDx).name)
                    currentDevice = dz.groups(currentIDx)
                else
                    logWrite("25 Current Device is device......" .. dz.devices(currentIDx).name)
                    currentDevice = dz.devices(currentIDx)
                end
                
                for attribute, value in pairs(attributes) do
                    logWrite("30 Current attribute is......" .. device)
                    if attribute == action then
                        logWrite("31 Current acction is......" .. action)
                        -- Blinking 
                        if action == 'blink' then
                            local blinkDevice = currentDevice
    						local blinkLevel = currentDevice.level
    						-- dz.utils.dumpTable(blinkDevice)
    						-- dz.utils.dumpTable(attributes)
    						logWrite("Device " .. blinkDevice.name .. " will blink")
    						if blinkDevice.state == "Off" then 
    							blinkDevice.switchOn()
    							blinkDevice.switchOff().afterSec(0.5)
    						else
    							blinkDevice.switchOff()
    							blinkDevice.switchOn().afterSec(0.5)
    						end
    						
    			        elseif action == 'dimmer' then 
    						local dimDevice = currentDevice
    						local dimLevel = dimDevice.level
    						local delay = 0
                            logWrite(dimDevice.name .. " direction is " .. direction)
    						if direction == "stop" then 
    						    dimDevice.cancelQueuedCommands()
    						    logWrite('Stop dimming of ' .. dimDevice.name .. ' at ' .. dimLevel ..'%')
    						elseif direction == 'down' then
    							repeat
    								delay = delay + 0.1
    								if direction == "down" then dimLevel = dimLevel - 1	else dimLevel = dimLevel + 1 end
    								logWrite('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%, after ' .. delay .. ' seconds')
    								dimDevice.dimTo(dimLevel).afterSec(delay)
    							until dimLevel <= 0
    					    elseif direction == 'up' then
                                repeat
                                    delay = delay + 0.1
                                    dimLevel = dimLevel + 1
                                    logWrite('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%, after ' .. delay .. ' seconds')
                                    dimDevice.dimTo(dimLevel).afterSec(delay)
                                until dimLevel >= 100
    						end
                        elseif action == 'toggle' then
                            -- dz.utils.dumpTable(currentDevice)
                            -- dz.utils.dumpTable(attributes)
                            local toggleDevice = currentDevice
                            if attributes["group"] then
                                toggleDevice.toggleGroup()
                            else 
                                toggleDevice.toggleSwitch()
                            end
                        end
                    end
                end
            end
        end
        
        local action = 'blink'
        local direction = 'up'
        
        if item.state == 'Click' and item.name == 'IKEA Remote Left' then 
            selectedGroupNumber = selectedGroupNumber - 1 
            if selectedGroupNumber == 0 then selectedGroupNumber = maxGroup end
            dz.notify("Aktuell grupp",control[selectedGroupNumber],dz.PRIORITY_NORMAL,dz.NSS_HTTP)
        elseif item.state == 'Click' and item.name == 'IKEA Remote Right' then 
            selectedGroupNumber = selectedGroupNumber + 1 
            if selectedGroupNumber > maxGroup then selectedGroupNumber = 1 end
            dz.notify("Aktuell grupp",control[selectedGroupNumber],dz.PRIORITY_NORMAL,dz.NSS_HTTP)
        elseif item.name == 'IKEA Remote' then
            action = 'toggle'
        elseif item.state == 'Hold' and item.name == "IKEA Remote Up"  then
            action = 'dimmer'
        elseif item.state == 'Hold' and item.name == 'IKEA Remote Down' then
            action = 'dimmer' 
            direction = 'down'
        elseif item.state == 'Release' and item.name == 'IKEA Remote Down' or item.name == 'IKEA Remote Up' then
            action = 'dimmer'
            direction = 'stop'
        elseif item.name == 'IKEA Remote Groups' then 
            selectedGroupNumber = math.floor( item.level/10 )
            logWrite("00 Group selected with IKEA Remote Groups" .. selectedGroupNumber)
        else
            logWrite('Unknown action requested; ignored', dz.LOG_INFO )
            return
        end
        
        if item.state == 'Click' or item.state == 'Release' then 
            logWrite('Turning off ' .. item.name)
            dz.devices(item.name).switchOff().silent()
        end
        
        dz.data.currentGroup = selectedGroupNumber
        doAction(action, direction) 
        
        
    end
}

Re: Dubble press

Posted: Thursday 16 July 2020 9:46
by Varazir
I found this https://www.domoticz.com/forum/viewtopic.php?t=25625
and wrote this, I'm at work now so I can test it but anything it that doesn't work I should do in another way ?

Code: Select all

-- doubleTap
local switchName =  "IKEA - Remote Kök"            -- Name  of your triggerSwitch 
local lightName =   "IKEA - Taklampa Kök"               -- Name of the lamp
local walplugName = "IKEA - Uttag Kök"
local timeout = 4
local delay = 0

return {
    on   =      { devices   =   { switchName }},
    
    logging =   { 
                    level           = domoticz.LOG_DEBUG, 
                    marker          = "IKEAKök" 
                },          
    
    data    =  { 
                    lastState =         { initial = "Off"   },
                    walpluglastState =  { initial = "Off"   },
                    lightlastState =    { initial = "Off"   }
                },

    execute = function(dz, item)
        
        dz.data.walpluglastState = dz.devices(walplugName).state
        dz.data.lightlastState = dz.devices(lightlastState).state
        
        local dimDevice = dz.devices(lightName)
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        if item.state ==  "On" and dz.data.walpluglastState == "Off" and item.lastUpdate.secondsAgo < timeout then
            logWrite('Turning on ' .. walplugName )
            dz.devices(walplugName).switchOn()
            dz.devices(lightName).cancelQueuedCommands()
        elseif item.state ==  "On" then
            logWrite('Turning on ' .. lightName .. ' in ' .. timeout .. ' sec' )
            dz.devices(lightName).switchOn().afterSec(timeout)
        end

        if item.state == "Off" and dz.data.walpluglastState == "ON"  and item.lastUpdate.secondsAgo < timeout  then
            logWrite('Turning off ' .. walplugName )
            dz.devices(walplugName).switchOff()
            dz.devices(lightName).cancelQueuedCommands()
        elseif item.state == "Off" then
            logWrite('Turning of ' .. lightName .. ' in ' .. timeout .. ' sec' )
            dz.devices(lightName).switchOff().afterSec(timeout)
        end
        
        if item.state == "Stop" then
            dimDevice.cancelQueuedCommands()
    		logWrite('Stop dimming of ' .. dimDevice.name .. ' at ' .. dimLevel ..'%')
    	end
    	
        if item.state == "Up" then
    		local dimLevel = dimDevice.level
            repeat
                delay = delay + 0.1
                dimLevel = dimLevel + 1
                logWrite('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%, after ' .. delay .. ' seconds')
                dimDevice.dimTo(dimLevel).afterSec(delay)
            until dimLevel >= 100
        end
    
        if item.state == "Down" then
    		local dimLevel = dimDevice.level
    		repeat
    			delay = delay + 0.1
    		    dimLevel = dimLevel - 1
    			logWrite('Set ' .. dimDevice.name .. ' to dimLevel '.. dimLevel .. '%, after ' .. delay .. ' seconds')
    			dimDevice.dimTo(dimLevel).afterSec(delay)
    		until dimLevel <= 0
        end
    end
}

Re: Dubble press

Posted: Friday 17 July 2020 12:40
by Varazir
I need a way to check witch device that are on or off.

Re: Dubble press

Posted: Friday 17 July 2020 14:17
by Varazir
Updated the script