Weird calculation of time with os.date?

Moderator: leecollings

Post Reply
Olgmo
Posts: 23
Joined: Tuesday 28 June 2016 12:24
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Norway
Contact:

Weird calculation of time with os.date?

Post by Olgmo »

Hi all,

I try to convert elapsed time from second into hours and minutes with os.date("%I:%M",diff)) but I dont understand why this is not working..
Command:
print ("UNIT Run time." .. diff .. " sec - " .. os.date("%H:%M",diff));

Log:
LUA: UNIT Run time. 306 sec - 01:05

WHY is 306 seconds display as 1 hour 5 minutes when it is just 5 minutes?

How to do this correctly?
zicht
Posts: 272
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: Weird calculation of time with os.date?

Post by zicht »

Hi

Try os.time :)

I do the same in different way, it has to do with lua and not with domoticz :)
os.clock is very accurate with up to 16 digits behind the comma.

Code: Select all

z1 = os.clock()

code were you want to check how long it runs

print("*******  "..os.clock() - z1) 
If you want to check on a device how long ago it was seen :

Code: Select all

function timedifference (s)   --- tijdverschil bepalen sinds laatste schakeling in sec
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
To call the function you can use anywhere in you script:

Code: Select all

difference2 = timedifference(otherdevices_lastupdate['Beweging Woonkamer'])
Both work like a charm and accurate.

Note : if you use the function for devicechanged_lastupdate{} you will run into an error on the day field, so always use otherdevices_lastupdate{} !
Note2 : if your code is running longer than 10 sec it will be abandoned to keep domoticz responsive
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest