require function in dzvents  [Solved]

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

Moderator: leecollings

Post Reply
Maxx
Posts: 61
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

require function in dzvents

Post by Maxx »

I am trying to build my own "function library" and I found some examples but I have not succeeded in implemting them.

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
}
and this is the function:

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
end
it gives the following log:

Code: 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) 
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.)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: require function in dzvents  [Solved]

Post by waaren »

Maxx wrote: 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.
The last line of the function file should be

Code: Select all

return self
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Maxx
Posts: 61
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: require function in dzvents [SOLVED]

Post by Maxx »

Hi Waaren,

Thank you, it works
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest