Failed optimisation... And my Lack of Lua skills
Posted: Thursday 26 July 2018 19:21
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:
and this
Both appear syntactically correct, although Im not sure which is the right way... However they both fail with
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.
Any pointers to what I'm doing wrong, or if its even possible?
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")
}
Code: Select all
return domoticz.helpers.motionDetectedFunction(domoticz, 9, 22, 120, "Kitchen")
Code: Select all
...icz/scripts/dzVents/generated_scripts/Kitchen Motion.lua:4: attempt to index field 'helpers' (a nil value).
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?