What is the best way to store a persistent Date and Time?
Code: Select all
local Time = require('Time')
return {
on = {
devices = {
DEVICE_TEST_EVENT
},
},
data = {
persistentTimeObject = { initial = Time()}, -- domoticz.time does not work here use Time() which Needs local Time = require('Time')
persistentDateTime = { initial = Time().rawDateTime } -- string
},
execute = function(domoticz, item)
if (item.isDevice) then
--domoticz.data.persistentTimeObject = Time()
--domoticz.data.persistentDateTime = Time().rawDateTime
domoticz.log('domoticz.data.persistentTimeObject.rawDateTime: ' .. tostring(domoticz.data.persistentTimeObject.rawDateTime))
domoticz.log('domoticz.data.persistentTimeObject.minutesAgo: ' .. tostring(domoticz.data.persistentTimeObject.minutesAgo) .. ' <-- WRONG')
domoticz.log('domoticz.data.persistentTimeObject.isToday: ' .. tostring(domoticz.data.persistentTimeObject.isToday) .. ' <-- WRONG')
domoticz.log('Time(domoticz.data.persistentTimeObject.rawDateTime).minutesAgo: ' .. tostring(Time(domoticz.data.persistentTimeObject.rawDateTime).minutesAgo))
domoticz.log('domoticz.time.makeTime(domoticz.data.persistentTimeObject).minutesAgo: ' .. tostring(domoticz.time.makeTime(domoticz.data.persistentTimeObject).minutesAgo))
domoticz.log('domoticz.time.compare(domoticz.data.persistentTimeObject).minutes: ' .. tostring(domoticz.time.compare(domoticz.data.persistentTimeObject).minutes))
domoticz.log('domoticz.data.persistentDateTime: ' .. tostring(domoticz.data.persistentDateTime))
domoticz.log('Time(domoticz.data.persistentDateTime).rawDateTime: ' .. tostring(Time(domoticz.data.persistentDateTime).rawDateTime))
domoticz.log('Time(domoticz.data.persistentDateTime).isToday: ' .. tostring(Time(domoticz.data.persistentDateTime).isToday))
domoticz.log('Time(domoticz.data.persistentDateTime).minutesAgo: ' .. tostring(Time(domoticz.data.persistentDateTime).minutesAgo))
domoticz.log('domoticz.time.makeTime(domoticz.data.persistentDateTime).rawDateTime: ' .. tostring(domoticz.time.makeTime(domoticz.data.persistentDateTime).rawDateTime))
domoticz.log('domoticz.time.makeTime(domoticz.data.persistentDateTime).isToday: ' .. tostring(domoticz.time.makeTime(domoticz.data.persistentDateTime).isToday))
domoticz.log('domoticz.time.makeTime(domoticz.data.persistentDateTime).minutesAgo: ' .. tostring(domoticz.time.makeTime(domoticz.data.persistentDateTime).minutesAgo))
end
end
}
Code: Select all
2024-04-28 14:46:05.307 Status: dzVents: Info: domoticz.data.persistentTimeObject.rawDateTime: 2024-04-27 14:43:26
2024-04-28 14:46:05.307 Status: dzVents: Info: domoticz.data.persistentTimeObject.minutesAgo: 0 <-- WRONG
2024-04-28 14:46:05.307 Status: dzVents: Info: domoticz.data.persistentTimeObject.isToday: true <-- WRONG
2024-04-28 14:46:05.308 Status: dzVents: Info: Time(domoticz.data.persistentTimeObject.rawDateTime).minutesAgo: 1442
2024-04-28 14:46:05.308 Status: dzVents: Info: domoticz.time.makeTime(domoticz.data.persistentTimeObject).minutesAgo: 1442
2024-04-28 14:46:05.308 Status: dzVents: Info: domoticz.time.compare(domoticz.data.persistentTimeObject).minutes: 1442
2024-04-28 14:46:05.308 Status: dzVents: Info: domoticz.data.persistentDateTime: 2024-04-27 14:43:26
2024-04-28 14:46:05.308 Status: dzVents: Info: Time(domoticz.data.persistentDateTime).rawDateTime: 2024-04-27 14:43:26
2024-04-28 14:46:05.308 Status: dzVents: Info: Time(domoticz.data.persistentDateTime).isToday: false
2024-04-28 14:46:05.308 Status: dzVents: Info: Time(domoticz.data.persistentDateTime).minutesAgo: 1442
2024-04-28 14:46:05.308 Status: dzVents: Info: domoticz.time.makeTime(domoticz.data.persistentDateTime).rawDateTime: 2024-04-27 14:43:26
2024-04-28 14:46:05.308 Status: dzVents: Info: domoticz.time.makeTime(domoticz.data.persistentDateTime).isToday: false
2024-04-28 14:46:05.309 Status: dzVents: Info: domoticz.time.makeTime(domoticz.data.persistentDateTime).minutesAgo: 1442
If stored as an Object domoticz.data.persistentTimeObject = Time() (or domoticz.data.persistentTimeObject =domoticz.time) it stores the entire time object in the __data file which is a big waste of space and resources (but then at least you can use it as a Time Object). However as noted in this topic all the relative fields of the time object are out of date and useless. (I.E. .minutesAgo. .isToday etc.). Seems like when a persistent time object is read into memory, when the script is run, the relative time object fields should be updated (in memory).
The only alternative I can see to storing a time object is to store the date and time as a string (I suppose you could do a timestamp as well).
domoticz.data.persistentDateTime = Time().rawDateTime (or domoticz.data.persistentDateTime = domoticz.time.rawDateTime)
If you store it as a string then you need to convert this string into an object to do anything with it. domoticz.time.makeTime(domoticz.data.persistentDateTime).isToday or (Time(domoticz.data.persistentDateTime).isToday).
Is there any way to update a time object besides converting it to a string and back to an object - I.E. Time(domoticz.data.persistentTimeObject.rawDateTime).isToday or domoticz.time.makeTime(domoticz.data.persistentTimeObject).isToday.
To get .minutesAgo using the compare function - I.E. domoticz.time.compare(domoticz.data.persistentTimeObject).minutes) - .minutesAgo is much more readable so - domoticz.time.makeTime(domoticz.data.persistentTimeObject).minutesAgo
Also what is the difference between Time() and domoticz.time.makeTime() (Besides the fact that Time() can only accept a String and domoticz.time.makeTime() will take a string or a time Object.)?
If nothing else maybe these examples will help other people dealing with time objects and date/time strings.