Problem using Time()

Please use template to report bugs and problems. Post here your questions when not sure where else to post
Only for bugs in the Domoticz application! other problems go in different subforums!

Moderators: leecollings, remb0

Forum rules
Before posting here, make sure you are on the latest Beta or Stable version.
If you have problems related to the web gui, clear your browser cache + appcache first.

Use the following template when posting here:

Version: xxxx
Platform: xxxx
Plugin/Hardware: xxxx
Description:
.....

If you are having problems with scripts/blockly, always post the script (in a spoiler or code tag) or screenshots of your blockly

If you are replying, please do not quote images/code from the first post

Please mark your topic as Solved when the problem is solved.
Post Reply
ricorico94
Posts: 99
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Problem using Time()

Post 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
User avatar
jvdz
Posts: 2479
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Problem using Time()

Post by jvdz »

...but you hardcoded the date/time string in that function so guess it works as designed ;-)
ricorico94
Posts: 99
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem using Time()

Post 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..
ricorico94
Posts: 99
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem using Time()

Post 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 ?
User avatar
jvdz
Posts: 2479
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Problem using Time()

Post 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
ricorico94
Posts: 99
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem using Time()

Post 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
}
User avatar
jvdz
Posts: 2479
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Problem using Time()

Post 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
}
User avatar
habahabahaba
Posts: 284
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Problem using Time()

Post by habahabahaba »

And what it should do? Your script... ?
I mean May be there is another way
Post Reply