dzVents helper functions 'problem'

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

Moderator: leecollings

Post Reply
User avatar
heggink
Posts: 989
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

dzVents helper functions 'problem'

Post 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)
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
User avatar
heggink
Posts: 989
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: dzVents helper functions 'problem'

Post by heggink »

Talking to Danny, add domoticz as an argument to the helper and it works. Confirmed!
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: dzVents helper functions 'problem'

Post 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!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents helper functions 'problem'

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest