Failed optimisation... And my Lack of Lua skills

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Andyf66
Posts: 44
Joined: Wednesday 13 June 2018 12:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Failed optimisation... And my Lack of Lua skills

Post by Andyf66 »

I'm gradually optimising my early dzvents code... I have a bunch of device event scripts that handle motion detected in various places and turn on/off various lights with various delays.

I came to the conclusion that 99% of the code is identical apart from the parameters, so I've attempted to optimise the duplication away. What I'm trying to do is make the top level device event script a single call to a helper function, that returns the entire event block. I've tried both this:

Code: Select all

return {
    domoticz.helpers.motionDetectedsFunction(domoticz, 9, 22, 120, "Kitchen")
}

and this

Code: Select all

return domoticz.helpers.motionDetectedFunction(domoticz, 9, 22, 120, "Kitchen")
Both appear syntactically correct, although Im not sure which is the right way... However they both fail with

Code: Select all

...icz/scripts/dzVents/generated_scripts/Kitchen Motion.lua:4: attempt to index field 'helpers' (a nil value). 
Is domoticz.helpers out of scope in some way here?

The shared code looks something like this, but the error above appears to indicate that it fails long before my shared code is called.

Code: Select all

        motionDetectedFunction  = function(domoticz, dayStartHour, dayEndHour, duration, sceneRoot)
        
            return {
    
                on = {
                    devices = {
                        sceneRoot .. ' Motion'
                    } 
                },
  
                execute = function(domoticz, item)
                    domoticz.helpers.motionActionFunction(domoticz, dayStartHour, dayEndHour, duration, sceneRoot) 
                end
            }
            
        end, 

Any pointers to what I'm doing wrong, or if its even possible?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Failed optimisation... And my Lack of Lua skills

Post by waaren »

Andyf66 wrote: Thursday 26 July 2018 19:21 ....
Any pointers to what I'm doing wrong, or if its even possible?
Don't know if this helps but

Code: Select all

return {
	active = function(domoticz)
			  return domoticz.startTime.secondsAgo < 180
			end,
this is an example of how to create a function that will be executed before the on section. It is designed to build your own "on = " section.by
setting the active = to true or false. Because you parse the domoticz object to it, I expect that you could also link a helpers functions to it.
Two remarks:

- make sure the function return false or true depending if you want the script to continue after the execution of the function
- this function will be called on every device change and every minute by the main event system.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Andyf66
Posts: 44
Joined: Wednesday 13 June 2018 12:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Failed optimisation... And my Lack of Lua skills

Post by Andyf66 »

Hmmm. Being called every minute doesn't seem too efficient. Maybe I'm over optimising here...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest