require function in dzvents
Posted: Sunday 13 October 2019 20:49
I am trying to build my own "function library" and I found some examples but I have not succeeded in implemting them.
following sample :
and this is the function:
it gives the following log:
Loading the function is succesfull but instead of executing the function it doesn't see fn as a reference to the function but as a boolean.
Is there anybody that has tried this before ( I took the example of the buienradar script, it seems to work in this script bu I cannot find the difference.)
following sample :
Code: Select all
return {
on = {
timer = {'every minute'},
},
logging = { level = domoticz.LOG_DEBUG,
marker = "Object test" },
execute = function(dz, triggeredItem)
local function Functions()
fn = require("Functions")
end
if pcall(Functions) then
dz.log("Functions loaded",dz.LOG_DEBUG)
else
local status, err = pcall(Functions)
dz.log("\n\n ******** Error ********* \n\n " .. err .. "\n\n" , dz.LOG_ERROR) -- No Functions -->> we quit
dz.log("Required functions (file) unusable or not present", dz.LOG_ERROR) -- No usersettting file So we quit
dz.log("Vereiste functies kunnen niet worden geladen. Verwerking stopt.", dz.LOG_ERROR) -- No usersettting file So we quit
return
end
local set = 20
local diff = 18
local output = fn.PID(set, diff)
dz.log('PID output : ' .. output)
end
}Code: Select all
local self = { }
function round(n)
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end
function self.PID(setpoint, avg_Temp)
local Ki = 10
local Kp = 13,6
local Kd = 1000
local dt = 60
local error = setpoint - avg_Temp
local output = (Kp*error+22)
return output
endCode: Select all
2019-10-13 20:44:00.891 Status: dzVents: Info: Object test: ------ Start internal script: test:, trigger: every minute
2019-10-13 20:44:00.891 Status: dzVents: Debug: Object test: Functions loaded
2019-10-13 20:44:00.892 Status: dzVents: Info: Object test: ------ Finished test
2019-10-13 20:44:00.891 Error: dzVents: Error: (2.4.29) Object test: An error occurred when calling event handler test
2019-10-13 20:44:00.891 Error: dzVents: Error: (2.4.29) Object test: ...e/pi/domoticz/scripts/dzVents/generated_scripts/test.lua:37: attempt to index global 'fn' (a boolean value) Is there anybody that has tried this before ( I took the example of the buienradar script, it seems to work in this script bu I cannot find the difference.)