time since changed

Moderator: leecollings

Post Reply
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

time since changed

Post 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?
Hans
User avatar
bobkersten
Posts: 34
Joined: Tuesday 06 October 2015 9:17
Target OS: Linux
Domoticz version: beta
Location: Venray
Contact:

Re: time since changed

Post 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
Mac mini w/ ESXi running Ubuntu w/ Domoticz custombuild, SolarEdge 3500Wp, S0 Meter, P1 Smart Meter, RFXtrx433e, Aeotec Z-Stick Gen5
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: time since changed

Post 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?
Hans
User avatar
jvdz
Posts: 2445
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: time since changed

Post 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
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: time since changed

Post 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?
Hans
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest