minutesAgo on persistant data always returns 0  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
antoine
Posts: 2
Joined: Sunday 10 December 2017 17:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

minutesAgo on persistant data always returns 0

Post 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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: minutesAgo on persistant data always returns 0

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: minutesAgo on persistant data always returns 0

Post 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)
antoine
Posts: 2
Joined: Sunday 10 December 2017 17:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

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

Post 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.
kofec
Posts: 53
Joined: Friday 16 September 2016 14:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: minutesAgo on persistant data always returns 0

Post by kofec »

Thanks it also help me
blueberryPie
Posts: 18
Joined: Sunday 19 November 2017 20:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: minutesAgo on persistant data always returns 0

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest