Page 1 of 1

time since changed

Posted: Monday 02 May 2016 21:18
by manjh
Is it possible to retrieve (in LUA) what the elapsed time is since a device last changed?
I can see the information in the log of the device, but would like to use it in a LUA script...

Already found:
otherdevices_lastupdate['device'])
But this returns date and time, and I need to have the minutes since last change.
What is the smartest way to calculate that in LUA?

Re: time since changed

Posted: Tuesday 03 May 2016 11:58
by bobkersten
See the code below. The timeOffset function gives you the offset in seconds.

Code: Select all

function timeOffset( sDate_ )
	local t1 = os.time()
	local t2 = parseDate( sDate_ )
	return os.difftime( t1, t2 )
end

function timeDelta( sDate1_, sDate2_ )
	local t1 = parseDate( sDate1_ )
	local t2 = parseDate( sDate2_ )
	return math.abs( os.difftime( t1, t2 ) )
end


function parseDate( sDate_ )
	local sYear = string.sub( sDate_, 1, 4 )
	local sMonth = string.sub( sDate_, 6, 7 )
	local sDay = string.sub( sDate_, 9, 10 )
	local sHour = string.sub( sDate_, 12, 13 )
	local sMinutes = string.sub( sDate_, 15, 16 )
	local sSeconds = string.sub( sDate_, 18, 19 )
	return os.time{ year=sYear, month=sMonth, day=sDay, hour=sHour, min=sMinutes, sec=sSeconds }
end

Re: time since changed

Posted: Tuesday 03 May 2016 22:00
by manjh
Bob: exactly what I need. Thanks.
I have included the code in the script where I want to use the function. It automatically brought a question: is it possible to build a library of LUA functions in Domoticz, so the code can be used from any script?

Re: time since changed

Posted: Tuesday 03 May 2016 22:48
by jvdz
Yes you can. I've a while set of general functions stored in a file named general_functions.lua and have added this line at the top of my scripts:

Code: Select all

dofile("/home/pi/domoticz/scripts/lua/general_functions.lua")
Jos

Re: time since changed

Posted: Wednesday 04 May 2016 11:25
by manjh
jvdz wrote:Yes you can. I've a while set of general functions stored in a file named general_functions.lua and have added this line at the top of my scripts:

Code: Select all

dofile("/home/pi/domoticz/scripts/lua/general_functions.lua")
Jos
Excellent,except I use a Win10 based server, not a Pi.
Where are the scripts store on Win10?
I looked in the program files directory and found Domoticz, and a bit deeper a "scripts" folder. But that does not show me all the LUA scripts that I have created. Am I missing something?