Page 1 of 1

Problem using Time()

Posted: Monday 12 October 2026 13:04
by ricorico94
Hi,

I'm using the version 2024.4 on raspberry pi. I admit it's an old version now, but so far it was working.
I had a script using the Time functions properly working for several years already and now I face weird behavior on handling the time.
I wonder if something happened on my domoticz because I now receive weird behavior on functions using Time. I made a test on this simple DZvent script:

Code: Select all

return {
	on = {
		timer = {
			'every minute'				-- causes the script to be called every minute
			}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, timer)
		domoticz.log('Timer event was triggered by my testscript' .. timer.trigger, domoticz.LOG_INFO)
		local Time = require('Time')
        local now = Time() -- current time
		domoticz.log("test usage Time: now="..now.getISO())
		domoticz.log("test usage Time: deltatemps="..now.dateToTimestamp('2026-10-12 12:45:00','yyyy-mm-dd hh:MM:ss'))
	end
}


I'd expect that the log shows increasing number of seconds, and approx hundreds to thousands seconds but no more.
Now my log shows:
2026-10-12 12:56:00.332 Status: dzVents: Info: template: ------ Start internal script: Test usage Time():, trigger: "every minute"
2026-10-12 12:56:00.332 Status: dzVents: Info: template: Timer event was triggered by my testscriptevery minute
2026-10-12 12:56:00.332 Status: dzVents: Info: template: test usage Time: now=2026-10-12T10:56:00Z
2026-10-12 12:56:00.332 Status: dzVents: Info: template: test usage Time: deltatemps=1791801900
2026-10-12 12:56:00.332 Status: dzVents: Info: template: ------ Finished Test usage Time()

and next update shows:
2026-10-12 12:57:00.299 Status: dzVents: Info: template: ------ Start internal script: Test usage Time():, trigger: "every minute"
2026-10-12 12:57:00.299 Status: dzVents: Info: template: Timer event was triggered by my testscriptevery minute
2026-10-12 12:57:00.300 Status: dzVents: Info: template: test usage Time: now=2026-10-12T10:57:00Z
2026-10-12 12:57:00.300 Status: dzVents: Info: template: test usage Time: deltatemps=1791801900
2026-10-12 12:57:00.300 Status: dzVents: Info: template: ------ Finished Test usage Time()

As you can see, the value of "now" is evolving, but the value of dateToTimeStamp remains equal to 1791801900.. and that value is clearly skyhigh to what I understand it should be (12:45 to 12:56 should be 11 minutes*60s=660 seconds.. and 720s at 12:57.

Am I misunderstanding something ?

br,
Rico

Re: Problem using Time()

Posted: Monday 12 October 2026 13:19
by jvdz
...but you hardcoded the date/time string in that function so guess it works as designed ;-)

Re: Problem using Time()

Posted: Monday 12 October 2026 14:22
by ricorico94
Indeed, sorry for my stupid mistake. I was understanding that the function was calculating a gap.. It is instead simply translating a text format into seconds. Thank you for having answered to me.
I was confused by the reading of the defintion in wiki: "Returns Number representing time since epoch in seconds". And I thought "since" was referring to the main date/time of the object instead of simply translating into seconds since origin of time.
My mistake..

Re: Problem using Time()

Posted: Monday 12 October 2026 15:27
by ricorico94
hi,
Still trying to investigate why my script doe not work anymore..
As a test script, I use:

Code: Select all

return {
	on = {
		timer = {
			'every minute'				-- causes the script to be called every minute
			
		}
	},
	data = {
	   lastReset = {initial= ''}

	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(dz, timer)
		dz.log('Timer event was triggered by my testscript' .. timer.trigger, dz.LOG_INFO)
		local Time = require('Time')

        if dz.data.lastReset == nil then 

            dz.log('lastReset is NIL ', dz.LOG_INFO)
			dz.data.lastReset=Time()
            dz.log('lastReset is set at: '..dz.data.lastReset.getISO(), dz.LOG_INFO)
			dz.log('lastmeasure is '..dz.data.lastmeasure,dz.LOG_INFO)
            
		else
			
			dz.log('lastReset is not NIL and = '..dz.data.lastReset.getISO(), dz.LOG_INFO)
			dz.log('lastmeasure is '..dz.data.lastmeasure,dz.LOG_INFO)
			

        end
	end
}
But once saved, I see an error message in the log:
2026-10-12 15:23:00.113 Status: dzVents: Info: template: ------ Start internal script: Test usage Time():, trigger: "every minute"
2026-10-12 15:23:00.113 Status: dzVents: Info: template: Timer event was triggered by my testscriptevery minute
2026-10-12 15:23:00.114 Status: dzVents: Info: template: ------ Finished Test usage Time()
2026-10-12 15:23:00.113 Error: dzVents: Error: (3.1.8) template: An error occurred when calling event handler Test usage Time()
2026-10-12 15:23:00.114 Error: dzVents: Error: (3.1.8) template: .../scripts/dzVents/generated_scripts/Test usage Time().lua:29: attempt to call a nil value (field 'getISO')
(row 29 is first log row after the "else" ("dz.log('lastReset is not NIL " etc.)

Why do I get that error ? In my script, I test whether or not the dz.data.lastReset variable is NIL .. Or do I do it the wrong way ?

Re: Problem using Time()

Posted: Monday 12 October 2026 16:44
by jvdz
You are initializing the field lastReset to '' and test for nil, hence the error as lastReset doesn't contain a valid time()!

try this version of the if:

Code: Select all

      if dz.data.lastReset == nil or dz.data.lastReset == '' then
			dz.log('lastReset is NIL ', dz.LOG_INFO)
			dz.data.lastReset=Time()
			dz.log('lastReset is set at: '..dz.data.lastReset.getISO(), dz.LOG_INFO)
			dz.log('lastmeasure is '..dz.data.lastmeasure,dz.LOG_INFO)
		else
			dz.log('lastReset is not NIL and = '..dz.data.lastReset.getISO(), dz.LOG_INFO)
			dz.log('lastmeasure is '..dz.data.lastmeasure,dz.LOG_INFO)
      end

Re: Problem using Time()

Posted: Monday 12 October 2026 17:38
by ricorico94
Thank you Jvdz.
Indeed this clears the error on first run. One question: could initialize as NIL directly ?
(by the way, I cleared the rows logging the "lastmeasure" because it's data from another script which couldn't be known by this script)

But now I get an error on 2s run:
2026-10-12 17:26:00.285 Status: dzVents: Info: template: ------ Start internal script: Test usage Time():, trigger: "every minute"
2026-10-12 17:26:00.285 Status: dzVents: Info: template: Timer event was triggered by my testscriptevery minute
2026-10-12 17:26:00.285 Status: dzVents: Info: template: lastReset is NIL
2026-10-12 17:26:00.286 Status: dzVents: Info: template: lastReset is set at: 2026-10-12T15:26:00Z
2026-10-12 17:26:00.288 Status: dzVents: Info: template: ------ Finished Test usage Time()
2026-10-12 17:27:00.239 Status: dzVents: Info: template: ------ Start internal script: Test usage Time():, trigger: "every minute"
2026-10-12 17:27:00.240 Status: dzVents: Info: template: Timer event was triggered by my testscriptevery minute
2026-10-12 17:27:00.240 Status: dzVents: Info: template: ------ Finished Test usage Time()
2026-10-12 17:27:00.329 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2026-10-12 17:27:00.240 Error: dzVents: Error: (3.1.8) template: An error occurred when calling event handler Test usage Time()
2026-10-12 17:27:00.240 Error: dzVents: Error: (3.1.8) template: .../scripts/dzVents/generated_scripts/Test usage Time().lua:29: attempt to call a nil value (field 'getISO')
Row 29 is the row : dz.log('lastReset is not NIL and = '..dz.data.lastReset.getISO(), dz.LOG_INFO)
I don't understand this since it's precisely in the branch where lastReset shouldn't be NIL.

(the script after your advice is now:

Code: Select all

return {
	on = {
		timer = {
			'every minute'				-- causes the script to be called every minute
				}
	},
	data = {
	   lastReset = {initial= ''}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(dz, timer)
		dz.log('Timer event was triggered by my testscript' .. timer.trigger, dz.LOG_INFO)
		local Time = require('Time')

        if dz.data.lastReset == nil or dz.data.lastReset == '' then
            	dz.log('lastReset is NIL ', dz.LOG_INFO)
	   	dz.data.lastReset=Time()
            	dz.log('lastReset is set at: '..dz.data.lastReset.getISO(), dz.LOG_INFO)
	else
		dz.log('lastReset is not NIL and = '..dz.data.lastReset.getISO(), dz.LOG_INFO)
	end
	end
}

Re: Problem using Time()

Posted: Monday 12 October 2026 18:39
by jvdz
Ok, tested this in my dev setup and the issue is that you are saving a time table in stead of a simple date-time string.
I have added a test for that to avoid any errors with your current saved time table to re-init the persistent variable.
try this version.

Code: Select all

return {
	on = {
		timer = {
			'every minute' -- causes the script to be called every minute
		}
	},
	data = {
		lastReset = { initial = nil }
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(dz, timer)
		dz.log('Timer event was triggered by my testscript' .. timer.trigger, dz.LOG_INFO)
		local Time = require('Time')

		if dz.data.lastReset == nil or type(dz.data.lastReset) == "table" then
			dz.data.lastReset = dz.time.rawDateTime
			dz.log('initialize lastReset to: ' .. dz.data.lastReset, dz.LOG_INFO)
		else
			dz.log('lastReset=' .. dz.data.lastReset .. ', which is ' .. Time(dz.data.lastReset).minutesAgo .. ' minutes ago.', dz.LOG_INFO)
		end
	end
}

Re: Problem using Time()

Posted: Tuesday 13 October 2026 11:59
by habahabahaba
And what it should do? Your script... ?
I mean May be there is another way