Lua code check (beginner).... has frozen my vera twice

Well. The other day I was writing some lua (new to Lua, some coding background).
Half way through I saved it, without thinking, and my vera got the dreaded unit is busy, than lua engine is taking longer than expected.
I was working through it with techsupport (slooooowly) and it magically started working.
I ripped out the code and combed through it. Found some mistakes and thought I fixed it.
Saved it and now stuck with my vera completely inoperable. Maybe tomorrow it will come back…
In the mean time I figured I would do a sanity check…


local function checkWorkTime()
     local pStart = "08:00"   -- Start of time period
     local pEnd = "18:00"     -- End of time period
     local allow = true       -- true runs scene during period, false blocks it
     local hS, mS = string.match(pStart,"(%d+)%:(%d+)")
     local mStart = (hS * 60) + mS
     local hE, mE = string.match(pEnd,"(%d+)%:(%d+)")
     local mEnd = (hE * 60) + mE
     local tNow = os.date("*t")
     local mNow = (tNow.hour * 60) + tNow.min
     if mEnd >= mStart then
          return (((mNow >= mStart) and (mNow <= mEnd)) == allow)
     else 
          return (((mNow >= mStart) or (mNow <= mEnd)) == allow)
     end
end

local function checkWorkWeek()
     local dFirst = 2         -- Start day of period (1-7) Sunday = 1 
     local dLast = 5          -- End day of period (1-7) Sunday = 1
     local allow = true       -- true runs scene during period, false blocks it
     local tNow = os.date("*t")
     local dNow = tNow.wday
     if dLast >= dFirst then
          return (((dNow >= dFirst) and (dNow <= dLast)) == allow)
     else 
          return (((dNow >= dFirst) or (dNow <= dLast)) == allow)
     end
end

local function checkAway()
  local dID = 10          -- Device ID of your MultiSwitch
  local button = 2	-- MultiSwitch button number (1-8)
  local allow = true     -- true runs scene if switch is on, false blocks it
  local status = luup.variable_get("urn:dcineco-com:serviceId:MSwitch1","Status"..button,dID)
  return ((status == "1") == allow)
end

if checkWorkWeek() and checkWorkTime() then
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "12"}, 0) -- cool76
elseif checkAway() then
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "12"}, 0) -- cool76
else
  luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "11"}, 0)  -- cool72
end

Yall see anything wrong?

I cannot see anything wrong with your code.

Well thats good and bad.
Glad to know I am not crazy…

The bad being when I enter this code into the luup section of a scene, it hangs my vera lite for 2 days…
Guess I will keep working with support to figure this out.

I don’t claim to be a lua expert ? I’ve only just started! ? but I took a look at your code, in the spirit of helping to learn more.

Shouldn’t os.date(“*t”) actually have two arguments, the second indicating the specific moment for which you wish the table?

(This is based on a cursory reading of http://www.lua.org/pil/22.1.html ).

[quote=“Oryzarius, post:4, topic:182238”]I don’t claim to be a lua expert ? I’ve only just started! ? but I took a look at your code, in the spirit of helping to learn more.

Shouldn’t os.date(“*t”) actually have two arguments, the second indicating the specific moment for which you wish the table?

(This is based on a cursory reading of Programming in Lua : 22.1 ).[/quote]

If you don’t provide a second argument, os.date(“*t”) returns the current date & time.

[quote=“joer., post:3, topic:182238”]Well thats good and bad.
Glad to know I am not crazy…

The bad being when I enter this code into the luup section of a scene, it hangs my vera lite for 2 days…
Guess I will keep working with support to figure this out.[/quote]

I would suspect a memory issue rather than a problem with your code.

It can depend on the trigger that runs the code. For example, there’s a bug in UI6 that locks Vera up if you put LUUP in a scene that is run on an interval schedule, like once an hour.

Oh man that is exactly it. It is a trigger schedule that runs every 30 minutes. Is this bug documented somewhere? A fix for it? I will bring this up with Vera support.
My thought process is to run my HVAC schedule based on a 30 minute trigger system and logic built in around times, sensors, virtual switches for the time.

Oh man that is exactly it. It is a trigger schedule that runs every 30 minutes. Is this bug documented somewhere? A fix for it? I will bring this up with Vera support.
My thought process is to run my HVAC schedule based on a 30 minute trigger system and logic built in around times, sensors, virtual switches for the time.[/quote]

Give some serious thought to using PLEG for this. It is much easier to implement multiple-factor timing logic in PLEG and it is less likely to lock-up your system.

I used the countdown timer plug-in instead. When they fix the interval schedule I’ll switch back to that.

PLEG is certainly another option.

Support said they would notify the developers, so hopefully it’ll be fixed in UI7.