Garage Door Left Open Script

I have setup a scene that is triggered when my Garage door is opened. When this scene runs, I have a Lua script that basically has a delayed function that runs to check to see if the garage door was left open. If it is still open, then it runs another scene that sends out a notification.

The script is pretty simple and after reading the excellent examples by RexBeckett, I think it is setup correctly. The problem though is it seems like my “checkGarageDoorStatus()” function is never called by luup.call_delay(). If I do not use luup.call_delay() and just call the function immediately, it successfully runs the notification scene - so it has to be something wrong with how I am using luup.call_delay(). I know if Vera reboots then the delay actions are lost, but even when I set the delay to 5 seconds, my notification scene is never ran.

I tried to look at the log files to see what was going on, but when I try to view the file via the url:
http:///cgi-bin/cmh/log.sh?Device=LuaUPnP
I get a “ERR_CONNECTION_REFUSED”.

-- Scene scheduled to run whenever the Garage Door is opened

-- Device Id
local deviceId = 1

-- Check Garage Door Status the specified wait time (in seconds)
luup.call_delay("checkGarageDoorStatus", 3600, deviceId)

function checkGarageDoorStatus(deviceId)

  -- Get current Garage Door Status, possible values "0: closed" and "1: open"
  local currentStatus = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", deviceId)

  -- If Door is closed do nothing and stop the scene
  if (currentStatus == "1") then

    -- Call scene that sends out a notification
    luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = 11}, 0)

    -- Check again in specified wait time (in seconds)
    luup.call_delay("checkGarageDoorStatus", 1800, deviceId)
  else
    return false
  end

end

Thanks!

I am just starting to get the hang of this myself! Hope the following helps!

I think your function name has a problem. Try:

function checkGarageDoorStatus()
–your code–
end

and call your function with:
luup.call_delay(“checkGarageDoorStatus”,2) --Where the 2 is two seconds.

Thanks for the help MarkAgain!

So moving my checkGarageDoorStatus() function to be before the luup.call_delay() method and not passing the device Id as a parameter to the function made this script work as expected.

Looking at this samples provided here : http://forum.micasaverde.com/index.php/topic,18679.msg154756.html#msg154756, it seems like they are not 100% accurate then?

Specifically the following sample which is basically what I originally had?

local dID = 99
luup.call_delay("delayDim",2,dID)

function delayDim(dev)
     local devno = tonumber(dev)
     local lls = tonumber((luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelStatus", devno)))
     local newlls = lls - 10
     if newlls < 0 then newlls = 0 end
     luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = newlls}, devno)
     if newlls > 0 then luup.call_delay("delayDim",2,dev) end
end

I am not familiar with passing a parameter. In your example I don’t see where your variable dev is getting a value. If dev is nil then devno will be nil as well. The device ID will be nil. I am guessing that the example you were looking at has a typo. I think that it could be dID instead of dev.

You could try assigning a value to dev.
local dev = 99

or replacing dev with dID.

I use RexBecket’s wonderful test tool. Saves a LOT of time!
http://forum.micasaverde.com/index.php/topic,24018.0.html

If I recall correctly, parameters passed by call_delay are passed as strings, rather than integers.

Therefore suggest that you convert the input parameter to a number before trying to use it:

devno=tonumber(dev)

BTW - tonumber() will still work if “dev” is already a number, so it does no harm to always use it.

Ahh - good catch aa6vh! I will give this a try and report back.

After running this script for a couple weeks, I have noticed a flaw in my logic/design. Right now the way this script works, when the garage door is opened, it starts a delay that checks in an hour if the garage door is left open - which works great, in the event that the door was left open. However where it fails is if the garage door was opened, then closed, but then an hour later I reopen the garage door - it will send out a notification.

I understand why this is happening and at first thought I am thinking to create another scene that is triggered when the door is closed that “cancels” the delay but not sure if this is possible. Doing some searching in the forum, I think if I just stop the scene via lua code, this will also stop the delay correct? Some post advise against stopping a scene via code though.

Any suggestions or other ideas on how to approach this?

Thanks!

Unfortunately, you cannot “cancel” a delay. You have to check when the delay completes to see if the code needs to actually run.

What I do for situations like yours, is set up a delay loop and a countdown timer global variable. I set the countdown timer variable to the number of times I want to run the loop, then fire up the loop for 1 minute intervals. Each time the delay completes, it decrements the variable, and either restarts the delay, or if the timer is zero, completes the task.

Now I can abort the loop by simply setting the variable to -1.

Big note: Delays, global variables, and the like get lost when Vera reboots or LUA reloads. So if your garage is open, and Vera reboots, you might not get notified like you expect. In my case, I wanted to relock my front door. Coded so that when a reboot occurs, Vera on restart will go ahead and just relock the door anyway. Restarts are rare, so this was acceptable in my situation.

Hello,
I manage the supervision of the opened doors with this plugin :
http://forum.micasaverde.com/index.php/topic,34489.0.html