Page 1 of 1
Why would LUA be really slow?
Posted: Friday 07 September 2018 14:24
by jefft
I have a really simple LUA script to toggle the state of a light when a switch is pushed, but when I trigger the event, there's a huge delay (8~10 seconds typically!) before the target device changes state. I'm using LUA to avoid the silliness with Blockly and its fall-through "elseif" clauses, but when I tried one half of this logic ("if switch on/off and light off then light on") in Blockly, the target light changed state instantly.
Why would doing this through LUA be so slow? I'm really puzzled. Anyone have any ideas of what I can check / try, please?
Code: Select all
commandArray = {}
-- hall second switch channel to toggle state of landing lights (catch both state changes in hall switch as it's a toggle)
if (devicechanged['Hall switch landing'] == 'On' or devicechanged['Hall switch landing'] == 'Off') then
print ("Hall-switch-landing LUA event fired");
if (otherdevices['Landing lights'] == 'Off') then
commandArray['Landing lights']='On'
else
commandArray['Landing lights']='Off'
end
end
return commandArray
System info:
Version: 4.9701
Build Hash: b47a877f
Compile Date: 2018-06-23 14:27:56
dzVents Version: 2.4.6
Python Version: 3.6.5 (default, Apr 1 2018, 05:46:30) [GCC 7.3.0]
Domoticz is running on an Ubuntu 18.04 server VM with 2vCPU, 2GB RAM, on a VMWare ESXi host.
Z-wave is Aeotec z-stick Gen 5.
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 17:38
by jvdz
Does the log entry also take that long or just the switch of the light?
What is shown for that in the domoticz log?
Jos
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 18:06
by jefft
Good thought, Jos.
A log entry shows up immediately for the switch action and right after it (<0.5s.) for "Domoticz is sending a command", then the light switches some time after that, quite variable - just had it take 2s and 10s on two consecutive tries. On and off seem to be either quicker or slower together. The light's control module is z-wave, so it could be that the z-wave network was having a hard time earlier, perhaps, and has now settled a bit. I'll force a network heal to make sure.
I also spotted a surprising error in the log after the last test:
Code: Select all
2018-09-07 15:51:48.529 Status: LUA: Hall-switch-landing LUA event fired
2018-09-07 15:51:48.529 (Z-Wave (Aeotec Z-Stick Gen5)) Light/Switch (Hall switch landing)
2018-09-07 15:51:48.530 Status: EventSystem: Script event triggered: Hall switch2 landing lights
2018-09-07 15:51:48.559 OpenZWave: Domoticz has send a Switch command! NodeID: 23 (0x17)
2018-09-07 15:51:48.560 (Z-Wave (Aeotec Z-Stick Gen5)) Light/Switch (Landing lights)
2018-09-07 15:51:55.015 (Z-Wave (Aeotec Z-Stick Gen5)) Usage (Unknown)
2018-09-07 15:51:55.015 (Z-Wave (Aeotec Z-Stick Gen5)) General/kWh (kWh Meter)
[b]2018-09-07 15:52:00.331 Error: EventSystem: in Hall switch2 landing lights: [string "--..."]:29: attempt to index global 'devicechanged' (a nil value)[/b]
(line 29 is actually the first "if" in the code; I cut off the header comments when I posted it); I've stripped out a few irrelevant log lines caused by other, non-z-wave devices reporting in.
So, it looks like the command is sent (@48.559), received by the light and acknowledged with a status report (@48.560) all within milliseconds, then 7 seconds later the light reports a change in power usage (@55.015) which seems to cause the event to be run again, creating the error (@00.331). That empty devicechanged array is strange... why would any event code be called if no device has changed?
The light didn't actually switch on until around the same time as the power report was sent. I guess that part has to be some sort of z-wave or module configuration problem, given that the light module clearly acknowledged the command very quickly.
Any ideas on the error?
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 18:19
by jefft
Oh, update...
I left the log scrolling in real time while I was configuring some other devices and I just spotted the same error coming up repeatedly - the event is definitely being triggered by the power meter reports coming in from z-wave devices (not just this device, I think, looks like any device), but then an empty 'devicechanged' array is being passed to the event code and crashing it.
Sounds like something that needs to be coded around by checking what caused the event to run, or possibly a bug in that there ought to be something in the array to say that the power meter changed...?
I guess for this event, I can check the dimensions of the array and quit if it's empty. Time to brush up on my LUA coding!
That raises a different question, though - how would one use a change in power usage to trigger an event, if that were the intention?
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 18:21
by heggink
Why not try dzvents?
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 18:30
by jefft
heggink wrote: ↑Friday 07 September 2018 18:21
Why not try dzvents?
Another language to learn (I get the basics of LUA having used Vera for a few years before seeing the light) and it looks more complex. Is it likely not to have the issues with empty device arrays etc?
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 18:47
by jvdz
Let me guess: You have coded this in the internal editor and haven't made it a "Device" script?
Check the dropdown for All and change that to Devices.
Jos
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 18:58
by jefft
jvdz wrote: ↑Friday 07 September 2018 18:47
Let me guess: You have coded this in the internal editor and haven't made it a "Device" script?
Check the dropdown for All and change that to Devices.
Jos
Ah!

Can I remove the "if devicechanged == nil" check now, then?
Re: Why would LUA be really slow?
Posted: Friday 07 September 2018 19:27
by heggink
jefft wrote:heggink wrote: ↑Friday 07 September 2018 18:21
Why not try dzvents?
Another language to learn (I get the basics of LUA having used Vera for a few years before seeing the light) and it looks more complex. Is it likely not to have the issues with empty device arrays etc?
Dzvents is just lua but then specifically adapted for domoticz. For one, had you used that instead of standard lua then this issue would not have occurred at all. I switched completely from blockly to dzvents. It's really simple to adopt.
Re: Why would LUA be really slow?
Posted: Sunday 09 September 2018 10:24
by jvdz
jefft wrote: ↑Friday 07 September 2018 18:58
Can I remove the "if devicechanged == nil" check now, then?
Sure as that table is always provided with a Device Event, but won't be there for the other types of events.
Jos