I'm creating a LUA script file that includes all the common routine that can be used in the event scripting in lua without defining them all the times
The file script is named LibUtil.LUA and at the moment include Time difference (as per wiki) and a conversion from CSV to table
the file is called in each script as:
Code: Select all
libutil = loadfile "/home/pi/domoticz/scripts/lua/LIBUTIL.lua"()
Code: Select all
libutil:timeDiff(s)
here's mine:
Code: Select all
function timeDiff(dName,dType)
t1 = os.time()
if dType == 'v' then
updTime = uservariables_lastupdate[dName]
elseif dType == 'd' then
updTime = otherdevices_lastupdate[dName]
end
year = string.sub(updTime, 1, 4)
month = string.sub(updTime, 6, 7)
day = string.sub(updTime, 9, 10)
hour = string.sub(updTime, 12, 13)
minutes = string.sub(updTime, 15, 16)
seconds = string.sub(updTime, 18, 19)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
tDiff = os.difftime(t1,t2)
return tDiff
end
in this way I can test both cases with a single function
now I will defice and call this way:
Code: Select all
libutil = loadfile "/home/pi/domoticz/scripts/lua/LIBUTIL.lua"()
varName = 'MyVariable'
devName = 'MyDevice'
commandArray = {}
secsDevice = libutil:timeDiff(devName,'d')
secsVariable = libutil:timeDiff(varName,'v')
print ( "Time difference for Device '..devName..': '..secsDevice..' - for Variable '..varName..': '..secsVariable)
return commandArray
Hope this could help someone
ciao
M