Page 1 of 1

LUA optimize scripts

Posted: Friday 20 November 2015 9:39
by thorbj
Hi, I have 12 Lua scripts on my system that is used to do different tasks throughout the day.
I have a feeling that this can be done in a cleaner way, maybe not relying on that many scripts? Also I am certain that these scripts can be greatly improved.

- script_device_movement.lua - detects movement and turns on a dummy-switch called Movement Livingroom.
- script_device_homeon.lua - detects if Movement Livingroom and/or dummy-switches for me and my wife's cellphones ar switched on, then turns on a dummy-switch called Home.
- script_time_homeoff.lua - switches off the dummy-switch Home if Movement Livingroom and cellphones are off.
- script_device_noonehome.lua - executes a scene that turns off lights if Home and a dummy-switch for my Chromecast is off.
- script_device_lux_lvingroomoff.lua turns off all lights if lux is below a given value.
- script_device_override.lua turns on all lights if a dummy-switch called Override is turned on. (I have also added a condition in all scripts that commands for turning off lights only runs if the Override-switch is off.
- script_device_watchestv.lua checks the time and switches scene when watching TV if the time is between 8am and 8pm, or otherwise. It also checks lux values to keep lights off if its not necessary to turn them on.
- script_device_livingroomon.lua turns on day/evening scenes depending on the time of day when motion is triggered and lux is below a given value. Also if media device like Chromecast is switched off.
- script_device_livingroomon2.lua same as above, the only difference is that it is not checking if motion is detected, only of the motion-switch is in an 'On' state.
- script_time_bevegelseoff.lua turns off Movement Livingroom if no motion is detected the last 30 minutes.
- script_time_lightdaynight.lua switches scenes depending on the time of day and if the TV is on or not.
- script_time_nightmode.lua turns off all lights if no movement and TV/Chromecast is off between 11PM and 6 AM.

I have attached the scrips as a zip if anyone want to take a look and maybe come with improvements.

Also this post may help others get a better grip of all the possibilities with lua in Domoticz.

Thanks!

Re: LUA optimize scripts

Posted: Friday 20 November 2015 17:33
by Egregius
All those different scripts is one of the reason's I didn't whant to get into lua. I handle everything with one single php script. I believe it's much easier that way to get a grip on everything that is happening and must happen in the system.

Re: LUA optimize scripts

Posted: Friday 20 November 2015 19:24
by thorbj
So it's possible to acquire all this via a single PHP script? Is there a wiki or how-to for this?

Re: LUA optimize scripts

Posted: Friday 20 November 2015 19:34
by bizziebis
You can also put everything in one LUA script, you don't need PHP for that. But if you have an error in one part of the script, nothing in that script will work. So debugging can be messy sometimes.

Re: LUA optimize scripts

Posted: Monday 23 November 2015 8:28
by thorbj
bizziebis wrote:You can also put everything in one LUA script, you don't need PHP for that. But if you have an error in one part of the script, nothing in that script will work. So debugging can be messy sometimes.
OK, so you're saying that if I want to use LUA, witch may be better after all to have control of all the different events, this would be the best practice? Or is there a better way to do it with LUA?

Re: LUA optimize scripts

Posted: Monday 23 November 2015 9:28
by ThinkPad
Why change a 'winning team'. If it is working fine, and your CPU-usage stays low, just stick with your current method.

One big script may seem better, but i don't agree with that. Like bizziebis says, one error (small typo for example) will break your whole script and thus; your whole automation. Far too prone for failures for me.
Separate Lua scripts are also much easier to change if you want different functionality. With a big script it could take quite some more time, because you maybe have to change it in multiple places.
And separate Lua scripts are also easier to share with someone, if they just need a small part of your functionality. I have put my scripts on Bitbucket for example.

TL;DR: Just stick with your current situation.

Re: LUA optimize scripts

Posted: Monday 23 November 2015 18:04
by Egregius
thorbj wrote:So it's possible to acquire all this via a single PHP script? Is there a wiki or how-to for this?
Yes, it's possible :D
Not really, only some stuff on my Github. See link below.

Re: LUA optimize scripts

Posted: Wednesday 25 November 2015 9:01
by thorbj
Than you @Egregius and @ThinkPad, I think I've got the answers I needed :)

Re: LUA optimize scripts

Posted: Thursday 24 December 2015 22:36
by Siewert308SW
Sorry for digging this old thread up ;-)
But overhere i described on how i optimized my scripts and managed to decrease my CPU load...
http://www.domoticz.com/forum/viewtopic ... =20#p65545

Re: LUA optimize scripts

Posted: Wednesday 17 February 2016 14:25
by dannybloe