OpenLuup : Loading Global Modules
-
It’s been a while since I looked at openLuup as it had been running nicely and quietly in the background doing some basic tasks. With my VeraPlus looking like it’s finally succumbing to old age, I want to shift a number of the global module I have over to openLuup.
To do this, I have added the files (example would be
xxpushover.lua
to the cmh-ludl folder and the following to the startuprequire “xxpushover”
The xxpushover.lua file itself starts with the following..
module("xxpushover", package.seeall)
And I always have a line in these files to allow me to check it’s been read in the start up related logs, which in this case it is..
The challenge I’m having is that when I try to call any of the functions within the module, it returns the following error..
"[string "ALTUI - LuaRunHandler"]:1: attempt to index global 'xxpushover' (a nil value)”
I’m no doubt missing something obvious, can anyone help me find out what it is ? Many thanks
-
The module call is, for good technical reasons, set to a no-op in openLuup.
It’s also an outmoded/deprecated pattern in newer Lua versions.
You should, instead, ensure that the module itself ends with a return statement, with a table including the required module contents.
There’s several ways to accomplish this. In openLuup itself I’ve generally done something like this:
— my module local function f1() whatever end local function f2() whatever end return { f1 = f1, f2 = f2, }
-
Thanks @akbooer,
OK to confirm, my current
xxpushover.lua
module has mainly global functions e.g.function push_specific(apptoken, title, device, message, priority, url, urltitle) local status = https.request ( "https://api.pushover.net/1/messages.json" , "token="..apptoken.."&user="..usertoken.."&device="..device.."&message="..message.."&title="..title.."&priority="..priority.."×tamp="..os.time().."&url="..url.."&url_title="..urltitle) luup.log(" <<<< - xxpushover module loaded - >>>>> ")
I should change these to local functions, and add the return table to the end..
local function push_specific(apptoken, title, device, message, priority, url, urltitle) local status = https.request ( "https://api.pushover.net/1/messages.json" , "token="..apptoken.."&user="..usertoken.."&device="..device.."&message="..message.."&title="..title.."&priority="..priority.."×tamp="..os.time().."&url="..url.."&url_title="..urltitle) return { push_file = push_file, push_basic = push_basic, push_link = push_link, push_html = push_html, push_mono = push_mono, push_specific = push_specific, } luup.log(" <<<< - xxpushover module loaded - >>>>> ")