Running Lua Code ? And watching device properties?
-
toggledbitsreplied to cw-kid on Feb 21, 2021, 6:18 PM last edited by toggledbits Feb 21, 2021, 1:46 PM
@cw-kid said in Running Lua Code ? And watching device properties?:
If MSR could build device properties or watch variables as PLEG does I think you'd be making it super easy for any user to pick up and run with MSR.
I'm really not getting what you're on about here. In your example earlier in the reply, monitoring the dimming level of a device is just this simple in MSR or Vera Reactor:
You literally select the device and the attribute and you're done, with all the different operators to choose from as well. There's no need for an expression at all. The only reason we have an expression for your ealier example/request is that we need to copy the attribute value from one device to another. But if we were just going to turn on another light in response to a light turning on, no expression needed.
-
@therealdb said in Running Lua Code ? And watching device properties?:
if you define a library and register it at startup, you could do that too. I’m currently using this approach, and I like it because it’s inside a file and not a textbox.
Can you give me some easy to follow instructions on how to do this please?
I like the idea of having my lua code in different files stored on the Vera hub and then being able to have that Lua code run by sending a http request from my MSR rule etc.
I would like to reduce the number of Vera scenes I currently have also.
-
@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
-
cw-kidreplied to therealdb on Feb 24, 2021, 3:18 PM last edited by cw-kid Feb 24, 2021, 12:11 PM
@therealdb Great thanks
First question which folder do you store the VeraScenes.lua file on the Vera file system ?
I put it in here:
/etc/cmh-ludl
EDIT
You need to upload the file via the Vera web GUI in
Apps - Develop Apps - Luup files
area not via some other means like WinSCP.
-
@cw-kid said in Running Lua Code ? And watching device properties?:
First question which folder do you store the VeraScenes.lua file on the Vera file system ?
or just use "Upload files" from Apps.
Mandatory part is to import into Lua Startup, under Apps, Develop Apps, Edit Startup Lua.
VeraScenes = require("VeraScenes")
I'll correct the readme soon.
-
@therealdb said in Running Lua Code ? And watching device properties?:
Mandatory part is to import into Lua Startup, under Apps, Develop Apps, Edit Startup Lua.
I did that.
-
Its working now if I upload it via Vera GUI and not WinSCP
Thanks
-
btw, your code seems to need some re-working to avoid warnings. try this:
function TTSTest() luup.log('VeraScenes.TTSTest') local mode = tonumber(luup.attr_get("Mode", 0)) if (mode == 1) then luup.inet.wget("http://192.168.0.4:1880/scenetrigger?message=This is a Test&ip=192.168.0.16") end end
-
@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
Hi
Any update on when this might happen ?
Thank you.
-
I have been running Lua within my Vera reactor pretty successfully. In some cases the reason was to have some conditional logic on the activity side. I found that this enabled me to keep the condition side a bit simpler and clean. I was also running lua to send Global Cache IR codes to my older Tv's. I now am trying the CG plugin that Patrick sorted out for me. The small issue with that is I need to convert all my Global Cache codes to pronto hex. I only mention so others know it does not support CG codes. There is also some lua that I use to turn off my computer. In preliminary testing the big issue I see with lua in Vera scenes is stability. I tried moving some of my lua into scenes and invoking the scenes thru MSR and everything just became real unstable with Luup restarts. I understand that this may be issues with my Vera, etc... . I also had lua in Vera reactor activities that preceded delays or other devices commands and by moving the lua into a Vera scene will that mess with the timing? Will there be a lag on the Vera side in invoking the scene while MSR continues with the reaction? The bottom line for me is I will try to make this work and eliminates much of the lua that I can. Having some legacy devices with IR contol does not help the situation I still have a lot of work to do so I can't say if everything will work out. It will be a matter of functionality and stability for me otherwise I will just stick with Vera reactor until I can't anymore. I will say that I do wish there was lua support in MSR.
-
@tarkus said in Running Lua Code ? And watching device properties?:
I tried moving some of my lua into scenes and invoking the scenes thru MSR
Is that working then and running the Vera scene?
I can't even seem to get that far on MSR build 0.1.21062 my Vera scene isn't being run when the reaction happens.
-
@cw-kid said in Running Lua Code ? And watching device properties?:
I can't even seem to get that far on MSR build 0.1.21062 my Vera scene isn't being run when the reaction happens
How are you running the scene?
-
Yeah, scenes run but the Vera goes haywire.
-
-
If that's the case, it probably goes haywire when the scene are run from the Vera UI as well, since MSR and the Vera UI use the same mechanism to start the scene. In fact, pretty much everything MSR does with Vera is enabled by the same mechanisms that the Vera UI (and Imperihome, etc.) use. There's nothing MSR can do to mess up those scenes. It just asks that they run.
I would start at the Vera UI scenes tab for troubleshooting. Lua code on Vera can be very particular, as you know. Long delays will cause deadlocks and reloads. If you're opening sockets or making HTTP requests without appropriate timeouts and error handling, you're going to get into trouble. If you open sockets or files and are not taking steps to close them properly in all conditions, even when errors occur, you're going to exhaust limited resource handles and cause a reload or even a reboot. You really need to understand and know what you're doing, and unfortunately, there's just a lot of bad Lua copy-pasta on the forums.
-
@toggledbits said in Running Lua Code ? And watching device properties?:
How are you running the scene?
I opened a PR with screen shot.
-
Well least you can run the Vera scenes I cannot at the moment.
I see these in the MSR log:
VeraController:ERR [VeraController:performOnEntity] action request failed
VeraController:CRIT Error: Request failed: 401 Error -
I understand and I will try to test keeping MSR out of the equation. Having said that the same lua in Vera reactor did not cause any issues. What i will do is take a segmented approach and not try to finish the complete MSR project at once. If I hit trouble i will try to remedy as well as I can. I am not a coder, not great at understanding code syntax. I am good at the logical part or reactor just not lua and coding in general. I have no idea whether my code is up to par or not. It was created by copying and pasting peoples samples from the forums with some modification that I picked up from online tutorials etc. All i know is is seems to work in Vera Reactor. My Vera reactor setup is quite complex and there is a lot going on. Is running a scene outside of reactor a true test absent of the other things reactor is doing concurrent to running a scene via Vera reactor or MSR? I will try with whatever skills I can muster up but when it comes down to it and speaking of Vera Reactor and my conditional logic needs if it isn't broken dont' fix it or at least dont go crazy trying to.
-
@tarkus said in Running Lua Code ? And watching device properties?:
In preliminary testing the big issue I see with lua in Vera scenes is stability. I tried moving some of my lua into scenes and invoking the scenes thru MSR and everything just became real unstable with Luup restarts.
I think you already discovered the problem. Running the lua through scenes is probably not a good idea. Have you attempted what was suggested by @therealdb in this thread defining a library at startup and calling your lua through that? IMO this is the correct way to do it.