LUA script to Lock Kwikset lock after 30 minutes

With the new site layout, It makes it kind of confusing as to where to post e.g. Locks vs Scene Scripting ;D ok I’ll stop winching…

I’m trying to be able to;

  1. Check the state of the locks (locked vs unlocked
  2. If its lock end
  3. If its unlocked lock the lock
  4. Check it after 30 minutes of lock state change

I thought that this would work but it simply locks the lock everytime, can someone give me pointers, thanxs in advance Mike

function checkdoorstatus()
– Find the status of lock # 26
local Lock1State=luup.variable_get(“urn:micasaverde-com:serviceId:DoorLock1”,“Status”,26)
– Exit the scene if the lock is locked.
if (Lock1State == “1”) then
return false
end

luup.call_timer(“checkdoorstatus”, 1, “30m”, “”, “”)
end

So if I understand correctly, you want a scene that checks a lock’s status every 30 minutes. If it’s unlocked, lock it.

  1. Create a scene to lock the door.
  2. Add a timer and set it to run every 30 minutes.
  3. Add this Lua code:
local lockState = luup.variable_get ("urn:micasaverde-com:serviceId:DoorLock1", "Status", 26)

-- If it's locked, don't run the scene.
if (lockState == "1") then
    return false
end

-- The lock is unlocked, run the scene.
return true

Almost… I only want to check 30 minutes after the door was opened not every 30 minutes and if the door is still open then issue a lock command.

I think I’m almost there…Looking at a Scene;

  1. Set up a event that executes when lock is opened
  2. LUA script that verifies that lock is still open after 30 minutes if locked do nothing and if open lock the lock (still need help here)
  3. Set Command tab to lock the lock in question

I think the code is almost there just not certain how to setup a 30 minute timer before executing the command tab. I’m assuming that the LUA script runs from top to bottom, that would suggest that I place the timer code at the top and after it expires then check lock status and return false/true depending on the lock status?.

thanxs in advance Mike

Cant you use the timer option that is available in a scene?
See screenshot

Seems to me its best to use functionality thats already available rather than designing new.
Just write your code that if lock is unlocked, lock it (you already did that) and have the timer interval set to 30 minutes.

[quote=“Michael_N_Blackwell, post:3, topic:168588”]Almost… I only want to check 30 minutes after the door was opened not every 30 minutes and if the door is still open then issue a lock command.

I think I’m almost there…Looking at a Scene;

  1. Set up a event that executes when lock is opened
  2. LUA script that verifies that lock is still open after 30 minutes if locked do nothing and if open lock the lock (still need help here)
  3. Set Command tab to lock the lock in question

I think the code is almost there just not certain how to setup a 30 minute timer before executing the command tab. I’m assuming that the LUA script runs from top to bottom, that would suggest that I place the timer code at the top and after it expires then check lock status and return false/true depending on the lock status?.

thanxs in advance Mike[/quote]

I guess I can, for some reason, I was thinking that the scene would execute every 30 minutes rather than upon event driven…kind of slow on my part ???. Although as I trying to learn LUA scripting, my premise of inserting the timer at the top should work? (yes/no) Thanxs for the insight

Update I: looked at the LUA logic flow and it has the timer logic and Event logic at the same entry level which suggests an “OR” situation. I believe I’m back to adding a 30 minute time into my LUA, any suggestions? I’ve also confirm the same with having both an event and timer, it executes properly but the timer has no apparent effect because the event happen first. Mike

Update II: This is my latest LUA script but I’m still unable to get the script to execute the timer properly I must be dense… :cry:

function checkdoorstatus()
luup.call_timer(“checkdoorstatus”, 1, “1800”, “”, “”)

local lockState = luup.variable_get (“urn:micasaverde-com:serviceId:DoorLock1”, “Status”, 26)

– If it’s locked, don’t run the scene.
if (lockState == “1”) then
return false
end

– The lock is unlocked, run the scene.
return true
end

@Michael_N_Blackwell

If you want to use it as a learning project, there is documentation on luup timers:
http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_call_timer

Also you might find thiss thread interesting then?
http://forum.mios.com/index.php?topic=6014.0

Best,

Henk

@henk, thanxs for the links… wasn’t able to get the timer to work for my situation, however I did get my code to work as follows:

  1. Create a scene
  2. Setup in the Control tab to lock your particular lock
  3. Set up in the Event tab that triggers on Opened lock event
  4. Add the following LUA Code, change the sleep time (1 second =1000) to your particular delay (I used 10 minutes) and change the device ID 27 to your device:

luup.sleep (600000)

– Get Lock status.
local lockState = luup.variable_get (“urn:micasaverde-com:serviceId:DoorLock1”, “Status”, 27)

– If it’s locked, don’t run the scene.
if (lockState == “1”) then
return false
end

– The lock is unlocked, run the scene that locks the lock.
return true

Hope this LUA script can help someone else 8)

I’m “Thick as Brick” ;D I’ve always believed in the KISS principle… well at least I learnt some LUA coding from this experience.

I’ve just figured out what “unchanged” in the command tab of a scene is for, oh well. Here’s my update to getting a scene to re-lock one’s Door after someone has opened it.

  1. Create a scene
  2. Go to the Control tab;
    • Select the lock in question
    • Select unchanged option
    • Select the duration you want to wait before locking your door “after X minutes”
    • Select the option to “Lock” the door
  3. Go to the Event tab;
    -Select the lock in question
    -Select device trigger “A door is Opened or Closed”
    -Select mode “opened”
  4. Exit the scene
  5. Select the Save (top and right of VERA panel)

That’s it no LUA needed