Page 1 of 1

dzVents helper functions 'problem'

Posted: Friday 21 July 2017 11:36
by heggink
Looking to dramatically simplify my alarm system scripts, I am struggling to get a helper fuction to work:
I have multiple ways to switch on/off the alarm (keypad, geofence, ..) so want to create a global helper function that does this.
When I call the helper function, it complains about attempting to access a global domoticz (nil value). I find that I cannot access domoticz data nor call domoticz devices from a helper function.
Am I doing something wrong or is that by design?

hetest.lua
return {
active = true,
on = {
devices = { 'hetest' }
},
execute = function(domoticz, dev)
local results = domoticz.helpers.AlarmOn(domoticz.security)
local results = domoticz.helpers.AlarmOff(domoticz.security)
end
}

global_data.lua
return {
helpers = {
AlarmOn = function(state)
print('In AlarmOn: ' .. state)
domoticz.devices('Security Panel').armAway()
end,

AlarmOff = function(state)
print('In AlarmOff: ' .. state)
domoticz.devices('Security Panel').disarm()
end
}
}

Error:
2017-07-21 11:27:16.732 Error: dzVents: Error: An error occured when calling event handler hetest
2017-07-21 11:27:16.732 Error: dzVents: Error: /home/pi/domoticz/scripts/dzVents/scripts/global_data.lua:15: attempt to index global 'domoticz' (a nil value)

Re: dzVents helper functions 'problem'

Posted: Friday 21 July 2017 16:18
by heggink
Talking to Danny, add domoticz as an argument to the helper and it works. Confirmed!

Re: dzVents helper functions 'problem'

Posted: Friday 21 July 2017 17:01
by BakSeeDaa
It should be convenient though if there was a way to access the domoticz object without adding it as an argument to all the helper functions...

Cheers!

Re: dzVents helper functions 'problem'

Posted: Tuesday 25 July 2017 11:25
by dannybloe
Well, I don't like to create too many globals. It is a bad practise programming-wise. Globals may have undesired so-called side-effects that may be hard to debug. That's why you should always create your Lua variables as local in your code-blocks. If not they may interfere with other scripts as the value isn't cleared when other dzVents scripts will be executed.