Page 1 of 1

I need help getting a garage door close using a selector

Posted: Thursday 18 July 2019 23:29
by rlschulz
What I am trying to do is watch for the garage door tilt sensor to turn on, once it does it will look at the selector in domoticz and see how long it needs to stay open. Then will wait the required time and close the door. If the selector is off it ignores the timer commands and stays open. Can someone point out my mistakes or tell me if this is possible.

Code: Select all

-- DZVents script to control garage door open time
local selector = 'Garage Door Close Timer'  -- virtual timer switch
local switch =  'Garage Door Opener'        -- garage door opener    
local variable = 'Garage Timer Minutes Set' -- variable from virtual switch 
local tilt = 'Garage Door Tilt Sensor'      -- tilt sensor to determine open or closed
return {
    on = 
    {  
            devices =   
            {   
                selector, -- Selector switch ( set timer )   
             }, 
            variables = 
            {   
                variable
            },
    },
    
    logging =   
    {   
        level   = domoticz.LOG_DEBUG,
    },
    
    execute = function(domoticz, item)
        selector = domoticz.devices(selector)
        switch = domoticz.devices(switch)
        variable = domoticz.variables(variable)
        
        local function stop(reason)
            domoticz.log("Closing garage door triggered by " .. reason,domoticz.LOG_DEBUG)
            variable.cancelQueuedCommands()
            variable.set(0).silent()
            switch.switchOff().silent()
            selector.switchSelector(0).silent()
        end
    if tilt.levelName == 'On' then
            if item.isVariable then
            if item.value > 0 then
                item.cancelQueuedCommands()
                item.set(0).afterMin(item.value) -- This will trigger script again after the set minutes
                domoticz.log("Garage door will be open for " .. item.value .. " minutes")
                switch.switchOn().silent()            else
                stop("variable =< =")  -- var 0 ==> close garage door 
            end
        elseif item == selector then
            if item.level == 0 then stop("selector set to 0") 
            elseif item.level < 70 then variable.set(item.level)
            elseif item.level == 70 then variable.set(120)
            elseif item.level == 80 then variable.set(240)
        end
            item.switchSelector(0).silent()
        elseif not(item.active) then
            stop("manually (or timer) closed control switch") -- selector or switch set to Off    
        end
    end
end
}

Re: I need help getting a garage door close using a selector

Posted: Friday 19 July 2019 0:38
by waaren
rlschulz wrote: Thursday 18 July 2019 23:29 What I am trying to do is watch for the garage door tilt sensor to turn on, once it does it will look at the selector in domoticz and see how long it needs to stay open. Then will wait the required time and close the door. If the selector is off it ignores the timer commands and stays open. Can someone point out my mistakes or tell me if this is possible.
If you enter the wait time in seconds as levelNames in the selector the script could look like

Code: Select all

-- control garage door open time
return 
{
    on = { devices = { 'Garage Door Tilt Sensor'}}, 

    logging = { level = domoticz.LOG_DEBUG, marker = 'Garage door' },

    execute = function(dz, item)
        local closeTimeSelector = dz.devices('Garage Door Close Timer')
        local garagedoorSwitch = domoticz.devices('Garage Door Opener')
        local closeTime = -1
        
        if closeTimeSelector.state ~= 'Off' then 
            closeTime = tonumber(closeTimeSelector.levelName)
        end

        if item.active and ( closeTime > -1 ) then
            garagedoorSwitch.switchOff().checkFirst().afterSec(closeTime)
        end
    end
}

Re: I need help getting a garage door close using a selector

Posted: Friday 19 July 2019 1:18
by rlschulz
Your code is so much cleaner than my mess!
I am getting this error. Thank you for all your help.

2019-07-18 17:06:20.979 (Z-Wave) Light/Switch (Garage Door Tilt Sensor)
2019-07-18 17:06:21.026 Status: dzVents: Info: Garage door: ------ Start internal script: Garage Door Timed Close: Device: "Garage Door Tilt Sensor (Z-Wave)", Index: 589
2019-07-18 17:06:21.026 Status: dzVents: Debug: Garage door: Processing device-adapter for Garage Door Close Timer: Switch device adapter
2019-07-18 17:06:21.026 Status: dzVents: Error (2.4.19): Garage door: An error occured when calling event handler Garage Door Timed Close
2019-07-18 17:06:21.026 Status: dzVents: Error (2.4.19): Garage door: ...ts/dzVents/generated_scripts/Garage Door Timed Close.lua:10: attempt to index global 'domoticz' (a nil value)
2019-07-18 17:06:21.026 Status: dzVents: Info: Garage door: ------ Finished Garage Door Timed Close

Re: I need help getting a garage door close using a selector

Posted: Friday 19 July 2019 8:14
by waaren
Sorry, my mistake. I could only check syntax and domoticz / dzVents did not complain about this. Hope I understood your requirements and config though. Updated version below

Code: Select all

-- control garage door open time
return 
{
    on = { devices = { 'Garage Door Tilt Sensor'}}, 

    logging = { level = domoticz.LOG_DEBUG, marker = 'Garage door' },

    execute = function(dz, item)
        local closeTimeSelector = dz.devices('Garage Door Close Timer')
        local garagedoorSwitch = dz.devices('Garage Door Opener')
        local closeTime = -1
        
        if closeTimeSelector.state ~= 'Off' then 
            closeTime = tonumber(closeTimeSelector.levelName)
        end

        if item.active and ( closeTime > -1 ) then
            garagedoorSwitch.switchOff().checkFirst().afterSec(closeTime)
        end
    end
}

Re: I need help getting a garage door close using a selector  [Solved]

Posted: Friday 19 July 2019 17:38
by rlschulz
That worked perfectly. Thank you!

Re: I need help getting a garage door close using a selector

Posted: Tuesday 30 July 2019 20:22
by rlschulz
I ran into a different issue. If you close the garage door manually, the tilt sensor goes to off, but since it created a timer it will open the door again. Then it will wait the minutes set on the selector and close the door again. Is there a way if you close the door manually that it can cancel the timer?

Re: I need help getting a garage door close using a selector

Posted: Tuesday 30 July 2019 21:04
by waaren
rlschulz wrote: Tuesday 30 July 2019 20:22 I ran into a different issue. If you close the garage door manually, the tilt sensor goes to off, but since it created a timer it will open the door again. Then it will wait the minutes set on the selector and close the door again. Is there a way if you close the door manually that it can cancel the timer?
If there is a way that domoticz can recognize the fact that you do some manual action then the script can be amended for it. Can you think of a way to set a virtual switch or userVariable if you do this manual action ?

Re: I need help getting a garage door close using a selector

Posted: Wednesday 31 July 2019 0:11
by rlschulz
Other than the tilt sensor changes to off when it is closes I can't. It doesn't use the z-wave pieces as it can be done with the car remote.