LUA optimize scripts

Moderator: leecollings

Post Reply
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

LUA optimize scripts

Post 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!
Attachments
lua_scripts.zip
(8.39 KiB) Downloaded 237 times
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA optimize scripts

Post 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.
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: LUA optimize scripts

Post by thorbj »

So it's possible to acquire all this via a single PHP script? Is there a wiki or how-to for this?
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
User avatar
bizziebis
Posts: 182
Joined: Saturday 19 October 2013 14:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8805
Location: The Netherlands
Contact:

Re: LUA optimize scripts

Post 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.
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: LUA optimize scripts

Post 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?
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: LUA optimize scripts

Post 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.
I am not active on this forum anymore.
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA optimize scripts

Post 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.
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: LUA optimize scripts

Post by thorbj »

Than you @Egregius and @ThinkPad, I think I've got the answers I needed :)
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
User avatar
Siewert308SW
Posts: 294
Joined: Monday 29 December 2014 15:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: LUA optimize scripts

Post 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
Setup:
- DS923+ Docker
Domoticz Latest Stable
Mosquitto / ZwaveJSUI @ Aeotec Z-Stick Gen5+ / PiHole Unbound
P1 USB Gas/Power, HW watermeter
- A lot of Zwave switches, contacts, plugs, smoke/Co2 ect
- DiY 7.5kWh Solar Storage @ GitHuB
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: LUA optimize scripts

Post by dannybloe »

Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest