The Vera Community forums have moved!

Advanced => Programming => General => Topic started by: JimMac on September 14, 2009, 11:52:37 am

Title: Lua programing help needed
Post by: JimMac on September 14, 2009, 11:52:37 am
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?



Title: Re: Lua programing help needed
Post by: Boyo on September 14, 2009, 04:29:55 pm
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.
Title: Re: Lua programing help needed
Post by: LibraSun on September 14, 2009, 05:27:37 pm
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."
Title: Re: Lua programing help needed
Post by: Ap15e on September 14, 2009, 06:00:16 pm
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 http://www.lua.org/manual/5.1/manual.html#5.8).
Title: Re: Lua programing help needed
Post by: JimMac on September 14, 2009, 08:56:29 pm
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.

Title: Re: Lua programing help needed
Post by: Ap15e on September 15, 2009, 05:15:24 am
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
Title: Re: Lua programing help needed
Post by: JimMac on September 15, 2009, 08:46:30 am
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.
Title: Re: Lua programing help needed
Post by: MNB on March 30, 2011, 02:26:03 pm
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
 













Title: Re: Lua programing help needed
Post by: mcvflorin on March 31, 2011, 04:10:31 am
Michael, I wrote the Lua code that does what you want. You can find it here:
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)
Title: Re: Lua programing help needed
Post by: MNB on March 31, 2011, 01:22:57 pm
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
Title: Re: Lua programing help needed
Post by: mcvflorin on April 01, 2011, 06:32:19 am
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.
Title: Re: Lua programing help needed
Post by: MNB on April 01, 2011, 11:46:01 am
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
Title: Re: Lua programing help needed
Post by: JimMac on April 01, 2011, 03:58:03 pm
I posted some sample codes I use in that thread...
http://forum.micasaverde.com/index.php?topic=2826.15