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

Re: Problem using Time()

Post by ricorico94 »

Hi Jvdz and habahabahaba ,

@habahabahaba : I try to understand why one my scripts doesn't work anymore with Time object. So I created this simple script to make some testings.

@Jvdz:
Your suggestion is working, thank you. At least I have a solution.
However, is it the way the Time object should work ? With your solution, the time is stored as a string, meaning that I can't use anymore all the functions described on wiki related to Time object.
I modified my script to introduce another persistent data, this time as a time object 'timeobject':
Spoiler: show

Code: Select all

return {
	on = {
		timer = {
			'every minute'				-- causes the script to be called every minute
			
		}
	},
	data = {
	   lastReset = {initial= nil},
        timeobject = {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.timeobject== nil then 
            dz.data.timeobject=Time('2026-10-13 12:00:00') 
            dz.log('timeobject was NIL and ha sbeen set again', dz.LOG_INFO)
        end
        if dz.data.lastReset == nil or type(dz.data.lastReset) == "table" then

            dz.log('lastReset is NIL ', dz.LOG_INFO)
			dz.data.lastReset = dz.time.rawDateTime
          --  dz.log('lastReset is set at: '..dz.data.lastReset.getISO(), dz.LOG_INFO)
			dz.log('initialize lastReset to: ' .. dz.data.lastReset, dz.LOG_INFO)
            
		else
			
		--	dz.log('lastReset is not NIL and = '..dz.data.lastReset.getISO(), dz.LOG_INFO)
			dz.log('lastReset=' .. dz.data.lastReset .. ', which is ' .. Time(dz.data.lastReset).minutesAgo .. ' minutes ago.', dz.LOG_INFO)
			

        end
    local now=Time()
    --local myTime=dz.time.toUTC(dz.data.lastReset)
    local myTime=Time(dz.data.lastReset)
    dz.log('comparison using time object: myTime is '..myTime.getISO()..' and comparing to lastReset '..myTime.compare(now).minutes.. 'minutes', dz.LOG_INFO)
    if dz.data.timeobject== nil then 
            dz.log('timeobject is NIL again', dz.LOG_INFO)
        else
         dz.log('timeobject is NOT nil again', dz.LOG_INFO)   
        dz.log('Timeobject is  '..dz.data.timeobject.rawDateTime..'and was set '..dz.data.timeobject.minutesAgo ..  'minutes ago.', dz.LOG_INFO)
        --dz.log('Comparison: now vs Timeobject  is  '..dz.data.timeobject.compare(now).minutes, dz.LOG_INFO)
    end
    end
}

	
The data timeobject looks persistent and properly stored as time object because I can use the dz.data.timeobject.rawDateTime and it shows correct value:
2026-10-15 20:20:00.347 Status: dzVents: Info: template: ------ Start internal script: Test usage Time():, trigger: "every minute"
2026-10-15 20:20:00.348 Status: dzVents: Info: template: Timer event was triggered by my testscriptevery minute
2026-10-15 20:20:00.348 Status: dzVents: Info: template: lastReset=2026-10-13 09:00:00, which is 3560 minutes ago.
2026-10-15 20:20:00.349 Status: dzVents: Info: template: comparison using time object: myTime is 2026-10-13T07:00:00Z and comparing to lastReset 3560minutes
2026-10-15 20:20:00.349 Status: dzVents: Info: template: timeobject is NOT nil again
2026-10-15 20:20:00.349 Status: dzVents: Info: template: Timeobject is 2026-10-13 12:00:00and was set 226minutes ago.
2026-10-15 20:20:00.351 Status: dzVents: Info: template: ------ Finished Test usage Time()
But what I don't understand:
- the "was set 226 minutes ago' never change.. Event with later refresh (like 1 minute later), the date/time is still ok, but the "minutes ago" remains stuck at 226 instead of being increasing by 1 at each refresh (every minute)..
2026-10-15 20:25:00.324 Status: dzVents: Info: template: timeobject is NOT nil again
2026-10-15 20:25:00.324 Status: dzVents: Info: template: Timeobject is 2026-10-13 12:00:00and was set 226minutes ago.
- if I use "dz.data.timeobject.getISO()' instead of " dz.data.timeobject.rawDateTime", I get an error whereas getISO is working a few rows above in statement "comparison using time object: myTime is '..myTime.getISO()..' "
2026-10-15 20:27:00.341 Error: dzVents: Error: (3.1.8) template: An error occurred when calling event handler Test usage Time()
2026-10-15 20:27:00.341 Error: dzVents: Error: (3.1.8) template: .../scripts/dzVents/generated_scripts/Test usage Time().lua:47: attempt to call a nil value (field 'getISO')
-if I un-comment the last row "--dz.log('Comparison: now vs Timeobject is '..dz.data.timeobject.compare(now).minutes, dz.LOG_INFO)", then I also get an error there..
2026-10-15 20:28:00.399 Status: dzVents: Info: template: timeobject is NOT nil again
2026-10-15 20:28:00.399 Status: dzVents: Info: template: Timeobject is 2026-10-13 12:00:00and was set 226minutes ago.
2026-10-15 20:28:00.399 Status: dzVents: Info: template: ------ Finished Test usage Time()
2026-10-15 20:28:00.399 Error: dzVents: Error: (3.1.8) template: An error occurred when calling event handler Test usage Time()
2026-10-15 20:28:00.399 Error: dzVents: Error: (3.1.8) template: .../scripts/dzVents/generated_scripts/Test usage Time().lua:48: attempt to call a nil value (field 'compare')
So I really get confused and Don't understand how I can use this timeobject as persistent data..
User avatar
jvdz
Posts: 2482
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 »

I don't understand why you want to store the whole object! What is the purpose?
So lets start with the real goal here as the simple short example i gave already shows you can simply show the time past since the update?
I normally prefer to save the timestamp as that is a number you can use to simply calculate any time difference too.
User avatar
habahabahaba
Posts: 285
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: Problem using Time()

Post by habahabahaba »

I think:

In dz.data you can store only simple data (like string, boolean values, digits)
and When you use dz.data.timeobject = Time('2026-10-13 12:00:00') - Time() - is a function with its own reasult - table? so you are trying to put Table in a table.

Just try
dz.data.timeobject = '2026-10-13 12:00:00' -- only a string object.

And I agree with jvdz - timestamp is more preferable
ricorico94
Posts: 105
Joined: Monday 26 October 2015 10:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem using Time()

Post by ricorico94 »

I'll rework using only simple persistent as per initial suggestion of Jvdz. Probably more robust anyway !

Thanks a lot for your help here !
Post Reply