Page 1 of 1

Error after updating

Posted: Tuesday 29 June 2021 13:02
by JimmyH1969
Hi all,

i've been using a script for a long time, but after updating Domoticz it throws an error:

Script part:

Code: Select all

	tempfilename = "UnifiController.tmp" -- Temp JSON output file
	local f = io.popen("stat -c %Y " .. tempfilename)
	local last_modified = f:read()
	if (os.difftime (os.time(), last_modified) > 30)
	then
		-- All kind of actions are executed here.
		-- Execute url
		-- Execute url
		read_login = os.execute(url_login)
		read_open = os.execute(url_open)
		read_logout = os.execute(url_logout)
	end
The error i get is on this line: if (os.difftime (os.time(), last_modified) > 30)
Error: [string " ..."]:14: bad argument #2 to 'difftime' (number expected, got nil)

Any ideas? I know it's probably because of a change in LUA, but i cant find it.

Tnx in advance!

Re: Error after updating

Posted: Tuesday 29 June 2021 15:31
by waltervl
If I read the error correct the second argument of difftime (=last_modified) is empty.
So your line

Code: Select all

 local last_modified = f:read()
is not working correctly.

Perhaps the file "UnifiController.tmp" is on a different place? Other access rights?

Re: Error after updating

Posted: Wednesday 30 June 2021 9:43
by JimmyH1969
Tnx for the reply.

The file is at that location and with the same rights, but i do agree it has something to do with that.

Re: Error after updating

Posted: Wednesday 30 June 2021 10:34
by psubiaco
If the file contains a valid number, try to use tonumber() function to convert its content to a number:

local last_modified = tonumber(f:read())

Re: Error after updating

Posted: Wednesday 30 June 2021 12:07
by JimmyH1969
Mhh, to test things i created a smaal script that should just get the last modified date of a file, like:

Code: Select all

commandArray = {}

	tempfilename = 'UnifiController.tmp' -- Temp JSON output file
	local f = io.popen("stat -c %Y " ..tempfilename)
	local last_modified = f:read()
	--local last_modified = tonumber(f:read())
	
	print("io.popen-1: " ..os.date("%c", last_modified))

return commandArray
The return is always the execution time of the script: LUA: io.popen-1: Wed Jun 30 12:07:00 2021
Even if i specify another file with a much older date and even if i specify a non existing file.
This is on Windows btw...

Any ideas?

Re: Error after updating

Posted: Wednesday 30 June 2021 12:36
by psubiaco
I've checked with LUA 5.3:
it works if you remove the local keyword:
f = io.popen("stat -c %Y " ..tempfilename)
last_modified = f:read()

Re: Error after updating

Posted: Thursday 01 July 2021 10:01
by JimmyH1969
No difference for me...