Lua programing help needed

I am trying to learn to code with Lua and need some help. I am trying to add code to a scene that will execute only if the time is between 16:00 hrs and 21:15 hrs. So far I can’t find any examples of how to do this. Here is what I have so far:

if os.time()>16:00
and os.time()<21:15
else
Return False
end

Since the os.time function returns a string I don’t think is can be directly checked against the time value “<16:00” or can it?
Is there a better way of doing this?

If it returns it as a string, is there a function to convert it into a number?
Don’t know lua, so I can’t be of much help.

My understanding of Lua tells me it enjoys “type coercion”…

From http://www.lua.org/manual/5.1/manual.html:

"2.2.1 - Coercion

Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules. Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format."

print(os.date(‘*t’,os.time())[“hour”])
print(os.date(‘*t’,os.time())[“min”])
print(os.date(‘*t’,os.time())[“sec”])

print the current hour, minute, and second (data type is number) (see Lua 5.1 Reference Manual).

I looked at os.date but the more I read the more os.time() fit the requirements besides the same question applies to the value returned by os.date(*t). If os.time returns a value not a string does it recognize it as time (60 minutes, 24 hours) and not a numeric value? I tried the code as a Lua scene but it did not work.

current_hour = os.date(‘*t’,os.time())[“hour”]
current_min = os.date(‘*t’,os.time())[“min”]

if (current_hour > 15) and ( (current_hour < 21) or ( (current_hour == 21) and (current_min <= 15) ) )
then
else
return false
end

or:

current_second = os.date(‘*t’,os.time())[“hour”] * 3600 + os.date(‘*t’,os.time())[“min”] * 60

min_time_in_seconds = 16 * 3600 + 0 * 60
max_time_in_seconds = 21 * 3600 + 15 * 60

if (current_second > min_time_in_seconds) and (current_second < max_time_in_seconds)
then
else
return false
end

This makes sense. I was assuming that Lua had more advanced functions and conversions. Your option 2 would make changing the time values a little easier. I’ll try it out this weekend. Thanks for the help.

Ap15e, looking at wiki and this older post, if my time range is 16:00 to 05:30 is there something different that I need because my end time is beyoud 00:00 time (2nd example)? thanxs Mike

current_hour = os.date(‘*t’,os.time())[“hour”]
current_min = os.date(‘*t’,os.time())[“min”]

if (current_hour > 15) and ( (current_hour < 21) or ( (current_hour == 21) and (current_min <= 15) ) )
then
else
return false
end

or:

current_second = os.date(‘*t’,os.time())[“hour”] * 3600 + os.date(‘*t’,os.time())[“min”] * 60

min_time_in_seconds = 16 * 3600 + 0 * 60
max_time_in_seconds = 21 * 3600 + 15 * 60

if (current_second > min_time_in_seconds) and (current_second < max_time_in_seconds)
then
else
return false
end

Michael, I wrote the Lua code that does what you want. You can find it here:
[url=http://wiki.micasaverde.com/index.php/Scripts_for_scenes#Scene_that_runs_only_in_a_user_set_time_interval]http://wiki.micasaverde.com/index.php/Scripts_for_scenes#Scene_that_runs_only_in_a_user_set_time_interval[/url]

mcvflorin, many thanxs, it’s hard to keep track via the various threads as to who originate the original input. I’m relatively new to Lua programming and need some time to digest the new code so that I understand the logic used. Refering the overview flow really helps, Mike

Most of the code is pretty self-explaining, I added some comments to explain the logic behind where I believe it’s harder to understand.

Looked at your commented code, really helps, many thanxs from the Lua challenged. ???

Side note, noticed on a another thread that “not at home” random code seems to be unanswered, would have thought as a home automation solution that it would have been one of the first projects addressed. Mike

I posted some sample codes I use in that thread…
http://forum.micasaverde.com/index.php?topic=2826.15