convert time/date

Moderator: leecollings

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

convert time/date

Post by manjh »

I want to use the lastupdate value to check if devices are updated for a set period.
What I get back from lastupdate is (for instance):

Code: Select all

2024-11-23 20:20:02
Is there a simple way (in LUA) to convert this into a a value that I can store, and compare in future values?
Hans
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: convert time/date

Post by FlyingDomotic »

If you're using "classical" LUA, then time elapsed since device's last update in seconds could be computed with:

Code: Select all

function lastSeen(device)
  timestamp = otherdevices_lastupdate[device] or device
  y, m, d, H, M, S = timestamp:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
  difference = os.difftime(os.time(), os.time{year=y, month=m, day=d, hour=H, min=M, sec=S})
  return difference
end
Called by "seconds = lastSeen("My device")" will load number of seconds since "My device" last update.
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: convert time/date

Post by manjh »

thanks for your help!
Hans
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest