Page 1 of 1
Noob: Can't get the constant values to work
Posted: Friday 01 March 2019 9:37
by Marque1968
I have a global_data via the gui:
- Spoiler: show
Code: Select all
-- global_data.lua
return {
helpers = {
findInArray = function(searchvalue, checkarray)
local foundvalue = false
for key,value in pairs(checkarray) do
if (value == searchvalue) then
foundvalue=true
end
end
return foundvalue
end,
findInDeviceArray = function(domoticz, searchvalue, checkarray)
local foundvalue = false
for key,value in pairs(checkarray) do
if (domoticz.devices(value).state == searchvalue) then
foundvalue=true
end
end
return foundvalue
end,
findInVariableArray = function(domoticz, searchvalue, checkarray)
local foundvalue = false
for key,value in pairs(checkarray) do
if (domoticz.variables(value).value == searchvalue) then
foundvalue=true
end
end
return foundvalue
end,
-- Device ID's
DID_radiation = 193,
DID_radiationdisplay = 149,
-- Variable ID's
VID_proxM = 1,
VID_proxN = 4,
-- Global variables
MY_CONSTANT = 100 -- doesn't have to be a function
}
}
The findIn... functions I have impemented in the system already, and work as expected, however not so for the constant variables. I have this script:
- Spoiler: show
Code: Select all
local idx_radiation = domoticz.helpers.DID_radiation
local idx_virtual = domoticz.helpers.DID_radiationdisplay
return {
on = {
devices = { idx_radiation },
},
execute = function(domoticz, triggeredItem, info)
domoticz.devices(idx_virtual).updateCounter(domoticz.utils.round(triggeredItem.radiation,0))
end
}
But the calling of these constant variables result in the following log entry:
Code: Select all
2019-03-01 09:12:42.878 Status: dzVents: Error (2.4.14): ...icz/scripts/dzVents/generated_scripts/test radiation.lua:1: attempt to index field 'helpers' (a nil value)
(as you can see, I started from the example on
https://www.domoticz.com/wiki/DzVents:_ ... _functions )
I also read there was a DATA section to the global_data.lua, but I do not want these values to be changed by functions in any other script (or even this one, for that matter).
Any input greatly appreciated.
Re: Noob: Can't get the constant values to work
Posted: Friday 01 March 2019 12:22
by waaren
Marque1968 wrote: Friday 01 March 2019 9:37
I have a global_data via the gui:
The findIn... functions I have impemented in the system already, and work as expected, however not so for the constant variables.
But the calling of these constant variables result in the following log entry:
Code: Select all
2019-03-01 09:12:42.878 Status: dzVents: Error (2.4.14): ...icz/scripts/dzVents/generated_scripts/test radiation.lua:1: attempt to index field 'helpers' (a nil value)
I also read there was a DATA section to the global_data.lua, but I do not want these values to be changed by functions in any other script (or even this one, for that matter).
Your global_data is correct but you try to access the domoticz object in your script in an area where it is not accessible.
To retrieve the value from domoticz.helpers.DID_radiationdisplay, you will have to move the assign statement inside the execute = function part
like:
Code: Select all
return {
on = {
devices = { idx_radiation },
timer = {"every minute"},
},
execute = function(domoticz, triggeredItem, info)
local idx_virtual = domoticz.helpers.DID_radiationdisplay
domoticz.log("idx_virtual: " .. idx_virtual)
end
}
The other thing is that the term CONSTANT used in global_data could led you believe that it cannot be changed in your script. Which is not the case as you can see from this example: (Let it execute at least twice to see what happens)
Code: Select all
return {
on = {
timer = {"every minute"},
},
execute = function(domoticz, triggeredItem, info)
local idx_virtual = domoticz.helpers.DID_radiationdisplay
domoticz.log("idx_virtual: " .. idx_virtual)
domoticz.helpers.DID_radiationdisplay = "Not a real constant"
local idx_virtual = domoticz.helpers.DID_radiationdisplay
domoticz.log("idx_virtual: " .. idx_virtual)
end
}
In fact Lua does not know a constant type as such. As explained
here there is a (complicated) way to work around that but I never found a good reason inside the domoticz area to use that approach.
Re: Noob: Can't get the constant values to work
Posted: Sunday 03 March 2019 11:37
by Marque1968
So what I want (and think I need), is not possible?
I want a list of devices and variables, accessible by name, so when a power plug fails (which happened to me last week) and gets replaced by another power plug, I want to changed the value in the list of constants so ALL scripts who reference that power plug in any way, now use the new device.
And if those constants can be changed in an other script is not a real issue for me, as I won't do that, AND if the system resets it would refer to the original value again, which is not the case with the data section.
Re: Noob: Can't get the constant values to work
Posted: Sunday 03 March 2019 12:39
by Marque1968
I think I found a work around
I now created a lua file in my home directory:
And added to the dzVents script:
Code: Select all
dofile("/home/pi/Documents/scripts/vartest.lua")
local idxs_of_switch = {var3} --{182}
return {
on = {
devices = idxs_of_switch,
},
execute = function(domoticz, triggeredItem, info)
if (triggeredItem.active) then
domoticz.helpers.zendTelegram(domoticz,"info\r\n Device on")
else
domoticz.helpers.zendTelegram(domoticz,"ALARM. Device is switched off !!!")
end
end
}
This does the trick:
HOWEVER
I am in a bit of doubt if this is fool proof. If the variable in the 'vartest' file is changed, how would domoticz know if it needs to trigger on a new value? Does it activate each and every script upon an event to check if the script needs to be executed? I would have thought it to be a set to a database trigger, but during my testing I could change the 'vartest' file without touching the dzvents script, and the triggers would react according to the new values. So I think I found myself a solution here...
Re: Noob: Can't get the constant values to work
Posted: Sunday 03 March 2019 21:19
by waaren
Marque1968 wrote: Sunday 03 March 2019 11:37
So what I want (and think I need), is not possible?
I want a list of devices and variables, accessible by name, so when a power plug fails (which happened to me last week) and gets replaced by another power plug, I want to changed the value in the list of constants so ALL scripts who reference that power plug in any way, now use the new device.
And if those constants can be changed in an other script is not a real issue for me, as I won't do that, AND if the system resets it would refer to the original value again, which is not the case with the data section.
If the fact that they can be changed in other scripts is not an issue for you, it is possible and in fact you already did it. Just define them in global_data and use them as in my example.
Re: Noob: Can't get the constant values to work
Posted: Monday 04 March 2019 7:48
by Marque1968
Ok, didn't think of that. I had a seperate file, which was not accessable from the UI, so I could not alter it from a remote location. Your solution lets me alter the work-installation from the comfort of my couch at home
