Page 1 of 1

Failed optimisation... And my Lack of Lua skills

Posted: Thursday 26 July 2018 19:21
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?

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

Posted: Thursday 26 July 2018 20:17
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.

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

Posted: Friday 27 July 2018 12:05
by Andyf66
Hmmm. Being called every minute doesn't seem too efficient. Maybe I'm over optimising here...