Door to lock only if between 11:00 pm and 7:00 am

Hello All…

I have 2 Kwikset locks - a regular zwave on my garage door and a zwave deadbolt on the front door. I keep the front locked at all times. I get notifications when someone unlocks the door and if they don’t arm it, I have it autolock after 1 minute. That works fine.

For the garage, I want to leave the door unlocked during the day and have it autolock at 11:00 pm, which it does just fine. What I don’t know how to do is that between the times of 11:00 pm and 7:00 am, is if the door is unlocked, to autolock the lock after 1 minute of being unlocked. The lock is device #5, node id is 23.

Any help/direction is appreciated.

Gene

Hello gestep,

Create a scene that runs when the door is unlocked and has a command to lock the door after 1 minute. Add this Luup code:

local from_hour = 23
local to_hour = 7

local now = os.date ("*t")

if ((now.hour >= to_hour) and (now.hour < from_hour)) then
    return false
end

return true

Worked perfectly the first time :slight_smile:

Thanks for the speedy AND correct response!

Gene

I’ve got the following function I use (easier for me to remember). I load it in “Startup Lua” (and I have the DAD plugin installed for sunset/sunrise to work properly). It requires military time (string) or “sunset”/“sunrise” and will return true if the current time is between the specified times.

A (major) caveat is that it doesn’t presently handle the clock going over midnight (I didn’t have the time to make that work properly, so I work around it by using two “between()” calls [ if between() or between() ], one from the pre-midnight time until “23:59:59” and then one from 00:00:01 until the “end-time”. I realize it leaves me vulnerable for 2 seconds around midnight, but I don’t see that as being worth the effort to fix it.

-- Absolute requirement: Use the HH:MM:SS time pattern and Military time (i.e "07:00:00" for 7am
-- and "19:00:00" for 7pm). The time values _have_ to be strings as well!
-- The function also supports "sunset" and "sunrise" as valid start/end times
-- PS: This ain't pretty as far as coding goes, but it works :)
--
function between(startTime, endTime)
	-- Variables
	local startHour, startMin, startSec
	local endHour, endMin, endSec
	local endTimeInSeconds
	local startTimeInSeconds
	local sunrise
	local sunset
	local Ssr
	local Sss
	local Esr
	local Ess
	local t = os.date('*t')
	
	-- Pattern match for HH:MM:ss
   	local matchPattern = "(%d+):(%d+):(%d+)"
	--
    -- In case this is time, split out the Hour, Minutes & Seconds
    --	
    endHour,endMin,endSec = endTime:match(matchPattern)
    startHour,startMin,startSec = startTime:match(matchPattern)
	--
	-- Identify whether startTime or endTime are "sunset" or "sunrise" (special processing if either are true)
	--
	luup.log("For between() - startTime: " .. startTime .. ", and endTime: " .. endTime)

    --
	if ( (startTime:find("sunrise") ~= nil) or (endTime:find("sunrise") ~= nil) )
	then
		-- Grab today's sunrise time
		sunrise = tonumber(luup.variable_get('urn:upnp-ap15e-com:serviceId:DAD1', 'Sunrise', devID_DAD_Device) or 0)
		--
		luup.log("Indicated Sunrise")
		if ( startTime:find("sunrise") ~= nil)
		then
			Ssr = os.date('*t', sunrise)
			startHour = Ssr.hour
			startMin = Ssr.min
			startSec = Ssr.sec
		end
		--
		if ( endTime:find("sunrise")  ~= nil)
		then
			Esr = os.date('*t', sunrise)
			endHour = Esr.hour
			endMin = Esr.min
			endSec = Esr.sec
		end	
	end
	--
	if ( (startTime:find("sunset")  ~= nil) or (endTime:find("sunset") ~= nil) )
	then
		-- Grab today's sunset time
		sunset = tonumber(luup.variable_get('urn:upnp-ap15e-com:serviceId:DAD1', 'Sunset', devID_DAD_Device) or 0)
		--
		luup.log("Indicated Sunset")
		if (startTime:find("sunset") ~= nil) 
		then
			Sss = os.date('*t', sunset)
			startHour = Sss.hour
			startMin = Sss.min
			startSec = Sss.sec
		end
		--
		if (endTime:find("sunset") ~= nil)
		then
			Ess = os.date('*t', sunset)
			endHour = Ess.hour
			endMin = Ess.min
			endSec = Ess.sec			
		end
	end
    --
	if (startHour ~= nil)
	then
		startTimeInSeconds =  (startHour * 3600) + (startMin * 60) + startSec
		currentSecond = (t.hour * 3600) + (t.min * 60) + t.sec
	end
	if (endHour ~= nil)
	then
		endTimeInSeconds = (endHour * 3600) + (endMin * 60) + endSec
		-- Grab the current time
		currentSecond = (t.hour * 3600) + (t.min * 60) + t.sec
	end
	--
	--
	luup.log("Start time in seconds: " .. startTimeInSeconds)
	luup.log("End time in seconds: " .. endTimeInSeconds)
	luup.log("Current time in seconds: " .. currentSecond)
	-- Check if current time is between the two values - startTime & endTime
	-- If it is, return true.
    if ((currentSecond >= startTimeInSeconds) and (currentSecond <= endTimeInSeconds))
    then
		luup.log("Current time is between " .. startTime .. " and " .. endTime)
		return true
    end
    -- It's not, so return false
    return false
end