Page 1 of 1
LUA, include file
Posted: Tuesday 02 February 2016 12:49
by gertlind1
I have a bunch of functions() that i use in my LUA scripts.
Is it possible to have those in a separate file instead of copy them to all my LUA scripts?
I have recently started to use the the built in LUA editor if that matters.
Re: LUA, include file
Posted: Saturday 06 February 2016 20:57
by jmleglise
I don't know for built in editor. But with file, you can do like this :
json = (loadfile "/home/pi/domoticz/scripts/lua/json.lua")() -- For Linux
json = (loadfile "D:\\Domoticz\\scripts\\lua\\json.lua")() -- For Windows
See example here :
https://www.domoticz.com/wiki/Lua_-_json.lua
Re: LUA, include file
Posted: Saturday 06 February 2016 23:23
by nayr
I dont think the lua editor actually saves files on the filesystem, so your lua include is going to have to be maintained externally..
for me I created a folder /usr/local/share/lua/5.2 and linked a file called libs.lua into that folder.
see:
https://github.com/nayrnet/domoticz-scr ... a/libs.lua
then here is an example usage in a script:
https://github.com/nayrnet/domoticz-scr ... ntdoor.lua
Re: LUA, include file
Posted: Sunday 07 February 2016 9:25
by gertlind1
Thanks guys.
i will play around with it,
Re: LUA, include file
Posted: Saturday 12 March 2016 11:53
by gertlind1
Of course I can't get it to work.
Created the /usr/local/share/lua/lua5.2/
Created a file there named libs.lua
Code: Select all
local libs = {};
function GroupState(sArray)
-- v1.0. 2016-01-18
-- Checks the state of a group of swithces.
-- USAGE : GroupState({'Switch1','Switch2','Switch3','Switch4','Switch5',more switches})
-- RETURNS : 'On' if all switches are on.
-- : 'Off' if all switches are off.
-- : 'Mixed" if one or more, but not all switches are on.
--
local iState = 0
local iCount = 0
local sState = ''
for i,light in pairs(sArray) do
if (otherdevices[light] == 'On') then
iState = iState + 1
end
iCount = iCount + 1
end
if(iState == 0) then sState = "Off" end
if(iState > 0) then sState = 'Mixed' end
if(iState == iCount) then sState = 'On' end
-- print("iCount : " .. iCount)
-- print("iState : " .. iState)
-- print("sState : " .. sState)
print "LIBS LUA"
return sState
end
return libs
made the soft link in my /domoticz/scripts/lua/
Code: Select all
libs.lua -> /usr/local/share/lua/5.2/
calling it from here
Code: Select all
commandArray = {}
libs = require("libs")
aLivingroom = {'Hörnbord','Soffbord','Blomma','Fönster','Bokhylla'}
sCounterName = 'Rörelse räknare'
if (devicechanged['Rörelse'] == 'On' and libs.GroupState(aLivingroom) == 'Off') then
time = os.date("*t")
if (SunSet() < 60 and time.hour <= 20) then
commandArray['Group:Alla Vardagsrum']='On'
end
if(time.hour >= 21) then
commandArray['Hörnbord']='On FOR 1'
end
else
difference = TimeDifference('Rörelse')
myTime = SecondsToClock(tostring(difference))
commandArray['UpdateDevice'] = '10|0|'..myTime..' sedan senaste rörelsen.'
end
return commandArray
Getting this error:
Code: Select all
Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_motion.lua: /home/pi/domoticz/scripts/lua/script_device_motion.lua:75: attempt to call field 'GroupState' (a nil value)
Re: LUA, include file
Posted: Saturday 12 March 2016 11:59
by gertlind1
Forget it.
I cannot read or my glasses are dirty.
missed this
function lGroupState(sArray)
Did not add the "libs."GroupsState(sArray)
Works fine now.
Will move all my functions to libs.lua.
Tanks for the tip and the instruction.
Re: LUA, include file
Posted: Saturday 12 March 2016 14:47
by Dnpwwo
Scripts created through the script editor are stored in the database but the only difference is how they are loaded by Domoticz prior to being handed to the Lua engine. Functionally external file inclusion should work the same way regardless of where the script is stored.
Re: LUA, include file
Posted: Saturday 12 March 2016 16:12
by gertlind1
Dnpwwo wrote:Scripts created through the script editor are stored in the database but the only difference is how they are loaded by Domoticz prior to being handed to the Lua engine. Functionally external file inclusion should work the same way regardless of where the script is stored.
Yes, works fine.
Added a lua script in the Domoticz internal editor
Code: Select all
libs = require("libs")
commandArray = {}
if (otherdevices['Rörelse'] == 'On') then
print ('Time to Sunset : ' .. libs.SecondsToClock(tostring(libs.SunSet())))
end
return commandArray
Works as a charm.
Code: Select all
2016-03-12 16:09:23.602 LUA: Time to Sunset : 00:01:33
Would it be possible to have the "functions file" libs.lua in the internal editor also?
That would be great.
Maybe if libs.lua is put in domoticz/scripts/lua
and the soft link that points to lib.lua is in the /usr/local/share/lua/lua5.2/
??
Re: LUA, include file
Posted: Saturday 12 March 2016 21:05
by dannybloe
<shameless-plug>
Or you use this:
dzVents
</shameless-plug>