Events & Scene Programming with Schlage Locks

i want to create a Scene that is triggered by an unlock event…

For example:

If garage door is locked… turn off lightx, lighty

If garage door is unlocked and it is later than 6:00pm turn on lightx, lighty

Any examples of how to do this… I’m a novice is this a l Luup programming project?

I don’t see how with the Schlage lock I can separate an lock from an unlock.

For the garage door locked scenario, create a scene, add an event, and select your door as your device. For event type, select “A door is opened or closed”, and under which mode, select “closed”. Name the event, and then under commands, select whatever lights you want to turn off and set them to OFF. Save the scene and test it.

For the garage door unlocked scenario, are you saying you want the light to come on if you unlock it from the inside, or when you unlock it by typing in the code? Also, for “and” statements, you need to be prepared to do write some luup code. You can use the same steps as above, but select “opened” for “which mode” in the event section. Additionally, you’de need to add a luup scene, drop in the code below, and click update. Save your scene and test it.

if (os.date(‘*t’,os.time())[“hour”] >= 18) then
return false
end

Haven’t tested this code - at the office now, but it should work. This should work between the hours of 6pm and midnight. To get more sophisticated, you can check one of the many posts/threads on the forums about sunset/sunrise - or see the lua code that I believe Javier created.
Also, it’s been a while since I’ve messed around with the “door is opened or closed” event type, but when I first got the lock, that event type actually functioned as door is unlocked or locked (from the inside)

I just have a few advices:

first, the second parameter in [tt]os.date()[/tt] defaults to the current time, so it’s not necessary to include a call to [tt]os.time()[/tt]. In other words:

os.date('*t', os.time())

is exactly the same as:

os.date('*t')

second: on table lookups, if the key if a simple string, with just letters, numbers and the ‘_’, and it doesn’t start by a number, it can be written as a ‘field’. so:

table["field"]

can be written as:

table.field

with this two tips, the code snippet is reduced to:

if (os.date('*t').hour >= 18) then
  return false
end

which I find a lot more readable.

If you want to test for a range, you can use a local variable, for example to prevent the scene to run between 8pm and 2am, you can write:

local time = os.date('*t')
if (time.hour >=20 or time.hour < 2) then
  return false
end

If you want to test for hours and minutes, it’s better to convert the full time to seconds. in the timeutils module i’ve written ([url=http://code.mios.com/svn_public/mios_genericutils/timeutils.lua]http://code.mios.com/svn_public/mios_genericutils/timeutils.lua[/url]), there are some useful functions, so you can write:

require "timeutils"
local time = timeutils.dayseconds()
if time >= timeutils.to_secs("08:30pm") and time <= timeutils.to_secs("09:15pm") then
  return false
end

Thanks Javier… never really worked with os.date… I’ve been more interested in daytime/nightime checks.
SK

Vera has some sense of sunset and sunrise. How would one query for the time of sunset or sunrise today (or on a given day)?

I’ve started to port this calculations to Lua; but it’s not as simple as it seems. What I’ve got so far is to calculate the Sun’s position at any given time (or ‘now’ by default), check http://code.mios.com/trac/mios_genericutils/wiki/TimeUtils

@Javier: Thanks for the pointer to your code. If this is still a work in progress, how does Vera figure out what time to use when one selects “At Sunset” in the GUI scene tool?

it does similar calculations, but it’s deep in the C++ code. I want to refactor it and port to Lua. Besides, it’s a little different when you want to do it for any given day and not necessarily just ‘next sunrise/sunset’ as the current code does.