@toggledbits said in Running Lua Code ? And watching device properties?:
It's not a limitation. It's the way Lua works, combined with one way Luup is broken.
The problem is that you are declaring your delay callback/handler function in a module. It is therefore not a global function, it is a module function -- it is scoped within the module, not globally.
To access functions inside a module, you have to use modulename.functionname(arguments), as you know, and the same is true for the delay handler/callback.
But the way Vera implemented luup.call_delay(), it takes a function name as a string, rather than a function reference. The function, therefore, has to be global. And it's not global, it's in a module.
One way to solve this problem (which all plugins experience, too, by the way) is to make a global alias. That means in your startup Lua, you do something like this:
mymodule = require( "mymodule" ) -- this line loads your module
-- Create a global alias for my delay handler
mymodule_delayhandler = mymodule.delayhandler
Then, in your module, instead of doing luup.call_delay( 'delayhandler', 5 ) you have to do luup.call_delay( 'mymodule_delayhandler', 5 ). This makes the delay use your global alias instead of the function name directly (which it can't).
Just trying this "Global Alias" for function delays, for the first time, but I can't get it working. I must be doing something wrong.
In the top of my VeraScenes.lua file I have added this code:
module("VeraScenes", package.seeall) -- TODO: personalize it
mymodule = require( "mymodule" ) -- this line loads your module
-- Create a global alias for my delay handler
mymodule_delayhandler = mymodule.delayhandler
--Away Mode Scene
function mymodule_delayhandler()
local status = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 419) --Status of Safe to Arm virtual switch
if status == "0" then --Safe to Arm
luup.inet.wget("http://SEND-TTS-MESSAGE1")
else --Not Safe to Arm
luup.inet.wget("http://SEND-TTS-MESSAGE2")
end
end
luup.call_delay( 'mymodule_delayhandler', 30 )
The test code checks the status of a virtual switch and sends a different HTTP request / TTS announcement etc.
I wanted a 30 second delay before anything happens at all.
When I call this function from a MSR rule by using
VeraScenes.mymodule_delayhandler()
I can hear the TTS announcement right away, so my 30 second delay is not happening.
What am I doing wrong?
@Tarkus Did you get this working?
Thanks