Page 1 of 1

minutesAgo on persistant data always returns 0

Posted: Wednesday 09 October 2019 23:37
by antoine
Hi,

I cannot find the problem with this dzVentz scene. The value of minutesAgo is always 0.

Code: Select all

local Time = require('Time')

return {
	on = {
	        scenes = { 
            		'Huiskamer*',
       		 },
        
		-- timer riggers
		timer = {
			-- timer triggers.. if one matches with the current time then the script is executed
			'15 minutes after sunset',
			'15 minutes before sunrise',
		},
	},

	data = {
       		lastTime = { initial = Time() },
    	},

	-- custom logging level for this script
 	logging = {
        	level = domoticz.LOG_DEBUG,
        	marker = "Huiskamer"
    	},

	execute = function(domoticz, triggeredItem, info)
	    domoticz.log('Scene last call at ' .. domoticz.data.lastTime.raw, domoticz.LOG_DEBUG)
	    
	    local minutesAgo = domoticz.data.lastTime.minutesAgo
	    domoticz.log('minutesAgo = ' .. minutesAgo, domoticz.LOG_DEBUG)
	end
}

Re: minutesAgo on persistant data always returns 0

Posted: Thursday 10 October 2019 8:55
by waaren
antoine wrote: Wednesday 09 October 2019 23:37 I cannot find the problem with this dzVentz scene. The value of minutesAgo is always 0.
I only see in your script that you initializes the persistent data. At that time the minutesAgo attribute is written as 0. It will not change.
Maybe if you describe what you want the script to show, I can help.

Re: minutesAgo on persistant data always returns 0

Posted: Thursday 10 October 2019 9:57
by boum
The OP may want to get time difference and update lastTime on some conditions, so that would be:

Code: Select all

local minutesAgo = domoticz.time.compare(domoticz.data.lastTime).minutes
(I guess it is not clear that the Time.*ago properties are not dynamic and are only updated when the Time object is created)

Re: minutesAgo on persistant data always returns 0  [Solved]

Posted: Thursday 10 October 2019 17:23
by antoine
It was indeed not clear that the minutesAgo attribute is only written during setting the persistent data.
I've updated the script with the proposal of boum, and now it working.
Thanks.

Re: minutesAgo on persistant data always returns 0

Posted: Wednesday 25 March 2020 16:48
by kofec
Thanks it also help me

Re: minutesAgo on persistant data always returns 0

Posted: Sunday 28 April 2024 22:16
by blueberryPie
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.