Load custom.lua

What’s the best way to handle custom function and code. Is it to have a custom.lua somewhere and load it from the startup ?

It depends what you want the code to do:

If it’s initialisation code, or library functions that you want to be available to scene code, then requiring a library file in the startup Lua is the way to go.

If you want to add some additional HTTP functionality, then write a CGI using the WSAPI interface.

If you want a new plugin, then write a complete plugin.

Or write a scene and put Lua into its code box, or in a trigger.

I have a bunch of home made function I made and use them in scene… before openLuup, in my Vera, I put all that code in the “Edit Startup Lua” tab!

So on openLuup I can put all that in a file and call the file in the startup lua!

This is exactly the same as for Vera. You can put stuff in the Startup Lua, be it lots of code or just a require statement.

Need some help…

In startup I have:

myLua = require("L_MyLuaCode")

My file is named L_MyLuaCode.lua and inside I have:

[code]
module(“L_MyLuaCode”, package.seeall)

local PLUGIN_VERSION = ‘0.60’
function checkTime(time1,time2)
local pStart = time1 – Start of time period
local pEnd = time2 – 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[/code]

My problem is, I’m unable to call this function…
I try

status = checkTime(“21:00”,“08:00”)

and

status = myLua.checkTime(“21:00”,“08:00”)

and I always got [string “status = myLua.checkTime(“21:00”,“08:00”)…”]:1: attempt to index global ‘myLua’ (a nil value)

Works for me…

local myLua = require "L_MyLuaCode"
print(pretty(myLua))
print "-----------"
status = myLua.checkTime("21:00","08:00")
print (status)

giving…

{
  _M = _,
  _NAME = "L_MyLuaCode",
  _PACKAGE = "",
  checkTime = function: 0x0189cd20
}
-----------
false

Where did you put L_MyLuaCode.lua ?

I put it under /opt/cmh-ludl/

OK. I ran this in the Lua Test window. Can you try that?

I will keep this snippet as a reference :wink:

Using your snippet I got
error loading module ‘L_MyLuaCode’ from file ‘./L_MyLuaCode.lua’:
./L_MyLuaCode.lua:213: ‘)’ expected near ‘,’

That mean I have some error in my whole file that I can’t see anywhere!
I comment out the line and it’s working.

Using this snippet, I’m able to see my error!

Thanks for everything akbooer!

After I’m done with my testing of openLuup/ALTUI I will for sure, send a donation to you and ALTUI!

While here…

under openLuup, is it different to call some function as under Vera… for example:

[code]local dt = os.date("*t")
local month = dt.month
local day = dt.day
local summer
local VSID = 19
local ssmon = 5 – Start of summer (month)
local ssday = 10 – Start of summer (day)
local esmon = 9 – End of summer (month)
local esday = 15 – End of summer (day)

if ((month == ssmon) and (day >= ssday))
or ((month == esmon) and (day <= esday))
or ((month > ssmon) and (month < esmon)) then summer = 1 else summer = 0 end

luup.call_action(“urn:upnp-org:serviceId:VSwitch1”,“SetTarget”,{newTargetValue=summer},VSID)[/code]

What’s wrong with my luup.call under openLuup ?

Using your snippet to test my myLua function, it works. but without the local myLua = require “L_MyLuaCode”
and having myLua = require(“L_MyLuaCode”) in the startup file… doesn’t work!

No. It’s designed to be exactly the same… ony better!

[code] luup.call_action("urn:upnp-org:serviceId:VSwitch1","SetTarget",{newTargetValue=summer},VSID)[/code]

What’s wrong with my luup.call under openLuup ?

At first sight, nothing, although you might care to check the error return values that the action call makes.

At a guess, though, you are missing some of the VSwitch device or service files.

Incidentally, you might consider something like the following snippet to be simpler and a bit more readable…

local dt = os.date "*t"
local now = os.time()
local sos = os.time {day =10, month=5, year=dt.year}
local eos = os.time {day =15, month=9, year=dt.year}

local summer = sos < now and now < eos

[quote=“DesT, post:11, topic:193309”]Using your snippet to test my myLua function, it works. but without the local myLua = require “L_MyLuaCode”
and having myLua = require(“L_MyLuaCode”) in the startup file… doesn’t work![/quote]

No. In this case, the value of myLua will be true and you will probably get an error like

[string "..."]:2: attempt to index global 'myLuaCode' (a boolean value)

The solution is to write the module in a non-deprecated style, eschewing the use of module, and using a return value with the exported functions and variables, viz…

local PLUGIN_VERSION = '0.60'
function checkTime(time1,time2)
  local pStart = time1 -- Start of time period
  local pEnd = time2 -- 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

return {
  checkTime = checkTime,
  -- other exports could go here
}

@akbooer,

Ok but in your example, how I’m calling my function checkTime after ?

Exactly the same way as before…

myLua.checkTime(...)

[string “status = myLua.checkTime(“21:00”,“08:00”)…”]:1: attempt to index global ‘myLua’ (a nil value)

STARTUP file:

- Any other startup processing may be inserted here... luup.log "loading custom code" myLua = require "L_MyLuaCode"

L_MyLuaCode.lua file (see attachment)

ok got it :wink: