Running Lua Code ? And watching device properties?
-
@cw-kid said in Running Lua Code ? And watching device properties?:
I am good at the moment however, using the method therealdb showed me. Its working currently.
I think you're missing what I'm saying. You don't need change your library structure or not do what @therealdb has suggested. It's a good, clean way to go. But if you do this from MSR right now as a Request action (that's what I assume you are doing) to run the function:
http://ip-address:3480/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunLua&Code=VeraScenes.turnLightsOn%28nil%29
You can (21063 and after) just do this, assuming you've loaded the library in startup Lua:
And if you haven't loaded it in startup Lua, you can just bring it in with the code, like this:
I thought you meant we could actually put the LUA code itself in to a MSR action "Set Reaction" and some how you were going to pass that to Vera.
But I see what you meant now, so we still need to edit the VeraScenes.lua file and add our functions / LUA code etc.
I can confirm the Play button works and the code is executed on my Vera Plus
So instead of sending a HTTP Request to Vera to initiate the function we can now just specify the function in the MSR rule actions.
-
I thought you meant we could actually put the LUA code itself in to a MSR action "Set Reaction" and some how you were going to pass that to Vera.
But I see what you meant now, so we still need to edit the VeraScenes.lua file and add our functions / LUA code etc.
I can confirm the Play button works and the code is executed on my Vera Plus
So instead of sending a HTTP Request to Vera to initiate the function we can now just specify the function in the MSR rule actions.
@cw-kid said in Running Lua Code ? And watching device properties?:
I thought you meant we could actually put the LUA code itself in to a MSR action "Set Reaction" and some how you were going to pass that to Vera.
You can, and that's exactly what's happening. Your call to your preloaded function is Lua code. You can also put this in and it will run on the Vera:
function later() luup.log("I'm running five seconds later!", 2) end luup.log("I'm running now!") luup.call_delay( "later", 5 )
You can put in any Lua you want. You can open a socket, send data, and close it. You can reload Luup (intentionally). You can write a file. No limits.
The code structure proposed by @therealdb makes good sense and is just a better way to encapsulate those functions than to bury them in Reactor rules. It will make your maintenance easier to structure your code that way.
-
This is how it will go:
That said, this won't work today, because I haven't yet connected the expressions to the action parameters (the
{master_level}
value in the dimming action won't work yet). That's coming, but not yet. You can, however, do exactly this in Vera Reactor to see how it works. It will work the same way in MSR.Edit... let me explain how it works (on both Vera Reactor and MSR):
The expression keeps the current value of the master dimmer. So on my system,
vera>device_105
is named "Virtual Dimmer 18". The trigger condition looks for any change in the dimming level of that dimmer. When it changes, the set reaction sets the current value (from the expressionmaster_level
) on the slave dimmer "Virtual Dimmer 20". Bob's your uncle.@toggledbits said in Running Lua Code ? And watching device properties?:
That said, this won't work today, because I haven't yet connected the expressions to the action parameters (the {master_level} value in the dimming action won't work yet)
Hi
Is this related to PR 0000005 ? "Set Variable action not yet implemented" Which is now Closed.
Thanks
-
@cw-kid here we go: https://github.com/dbochicchio/vera/tree/master/Library
I've added a sample to RunLua as well, so this should serve well our future us when in need of it
@therealdb said in Running Lua Code ? And watching device properties?:
@cw-kid here we go: https://github.com/dbochicchio/vera/tree/master/Library
I've added a sample to RunLua as well, so this should serve well our future us when in need of it
Does this work with lua that is using functions? The following runs as standalone code but will not run from the library. I tested another block of lua without functions and that works in the library.
module("VeraScenes", package.seeall) -- TODO: personalize it -- Function / Samsung_tv_power_on function matrixtest() luup.log('VeraScenes.matrixtest') local socket = require("socket") function tcpclose(tcp) tcp:close() end function Delay1() --[[HDMI Matrix B1--]] tcp = assert(socket.connect("192.168.255.204", 4998)) tcp:send("sendir,1:3,1,38000,1,69,341,171,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,64,22,64,22,64,22,64,22,64,22,64,22,64,22,64,22,21,22,21,22,64,22,21,22,64,22,21,22,21,22,21,22,64,22,64,22,21,22,64,22,21,22,64,22,64,22,64,22,1493,341,85,22,3669" .. "\r\n") luup.call_delay("tcpclose", 1050, tcp) end luup.call_delay("Delay1", 1) function Delay2() --[[HDMI Matrix B2--]] local socket = require("socket") tcp = assert(socket.connect("192.168.255.204", 4998)) tcp:send("sendir,1:3,1,38000,1,69,343,171,21,21,22,21,22,21,22,21,21,21,22,21,22,21,21,21,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,21,22,63,22,21,21,21,22,21,22,21,21,22,21,21,22,63,22,21,22,63,22,63,22,63,22,1528,342,85,22,1528" .. "\r\n") luup.call_delay("tcpclose", 1050, tcp) end luup.call_delay("Delay2", 10) end
-
@therealdb said in Running Lua Code ? And watching device properties?:
@cw-kid here we go: https://github.com/dbochicchio/vera/tree/master/Library
I've added a sample to RunLua as well, so this should serve well our future us when in need of it
Does this work with lua that is using functions? The following runs as standalone code but will not run from the library. I tested another block of lua without functions and that works in the library.
module("VeraScenes", package.seeall) -- TODO: personalize it -- Function / Samsung_tv_power_on function matrixtest() luup.log('VeraScenes.matrixtest') local socket = require("socket") function tcpclose(tcp) tcp:close() end function Delay1() --[[HDMI Matrix B1--]] tcp = assert(socket.connect("192.168.255.204", 4998)) tcp:send("sendir,1:3,1,38000,1,69,341,171,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,64,22,64,22,64,22,64,22,64,22,64,22,64,22,64,22,21,22,21,22,64,22,21,22,64,22,21,22,21,22,21,22,64,22,64,22,21,22,64,22,21,22,64,22,64,22,64,22,1493,341,85,22,3669" .. "\r\n") luup.call_delay("tcpclose", 1050, tcp) end luup.call_delay("Delay1", 1) function Delay2() --[[HDMI Matrix B2--]] local socket = require("socket") tcp = assert(socket.connect("192.168.255.204", 4998)) tcp:send("sendir,1:3,1,38000,1,69,343,171,21,21,22,21,22,21,22,21,21,21,22,21,22,21,21,21,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,21,22,63,22,21,21,21,22,21,22,21,21,22,21,21,22,63,22,21,22,63,22,63,22,63,22,1528,342,85,22,1528" .. "\r\n") luup.call_delay("tcpclose", 1050, tcp) end luup.call_delay("Delay2", 10) end
@tarkus I have also seen the same issue / limitation.
Seems if your LUA code has functions within it already, you can't use that code in the VeraScenes.lua file as it doesn't run the code.
I've had to run from MSR some Vera scenes with that LUA code instead for now.
Perhaps we are both doing it incorrectly which is quite possible.
-
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 doluup.call_delay( 'mymodule_delayhandler', 5 )
. This makes the delay use your global alias instead of the function name directly (which it can't). -
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 doluup.call_delay( 'mymodule_delayhandler', 5 )
. This makes the delay use your global alias instead of the function name directly (which it can't).@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.delayhandlerThen, 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
-
T toggledbits locked this topic on