Page 1 of 1

universal Time Difference Script

Posted: Friday 25 November 2016 17:18
by emme
Ciao,

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"() 
than you can call different routines like

Code: Select all

libutil:timeDiff(s)
the time difference function has been changed a little to be considered... universal..
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
the function has 2 arguments: device name and device type: V for a variable, D for a device
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
...in case you add the function in each script it could even not need that, but if you willing to create a library.. it could be useful


Hope this could help someone
ciao
M