We have moved at community.getvera.com

Author Topic: Lua programing help needed  (Read 12668 times)

Offline JimMac

  • Sr. Member
  • ****
  • Posts: 309
  • Karma: +2/-1
Lua programing help needed
« 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?




Offline Boyo

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Re: Lua programing help needed
« Reply #1 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.
Vera UI4 1.1.1183; Schalge Link Door Handle; HA18WD 3-Way/Single-Pole Switch x2; WDHA-12R - Wireless Gateway Module; Trane Thermostat; Retired: WDTC-20 - Z-Wave Enabled Thermostat

Offline LibraSun

  • Hero Member
  • *****
  • Posts: 574
  • Karma: +2/-0
Re: Lua programing help needed
« Reply #2 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."
Vera Model I running UI4 (Firmware 1.1.1338), died in 2015
Vera Plus running UI7 (Firmware 1.7.2935)

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Lua programing help needed
« Reply #3 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).

Offline JimMac

  • Sr. Member
  • ****
  • Posts: 309
  • Karma: +2/-1
Re: Lua programing help needed
« Reply #4 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.


Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Lua programing help needed
« Reply #5 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

Offline JimMac

  • Sr. Member
  • ****
  • Posts: 309
  • Karma: +2/-1
Re: Lua programing help needed
« Reply #6 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.

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Lua programing help needed
« Reply #7 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
 













Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Lua programing help needed
« Reply #8 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

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Lua programing help needed
« Reply #9 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
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Lua programing help needed
« Reply #10 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.

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Lua programing help needed
« Reply #11 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
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline JimMac

  • Sr. Member
  • ****
  • Posts: 309
  • Karma: +2/-1
Re: Lua programing help needed
« Reply #12 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