Error on 'nil' value that should not be 'nil'...

Moderator: leecollings

Post Reply
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Error on 'nil' value that should not be 'nil'...

Post by leby »

I have a lua script that I moved to internal editor, have changed to "lua" and "device".

I have three variables, HallwayPir, OutsidePir and PirDiff. I use them to see when we are leaving or coming home (small negativ / positiv number)

Code: Select all

commandArray = {}
	z = tonumber(uservariables['HallwayPir'])
	w = os.time()
	x = w - z
if (devicechanged['Outside Veranda West Pir'] == 'On') then
	commandArray['Variable:PirDiff'] = tostring(x)
	commandArray['Variable:OutsidePir'] = tostring(w)
end
return commandArray
Needless to say, It does not work. I get

Code: Select all

Error: EventSystem: in device_outsidepir: [string "commandArray = {} ..."]:4: attempt to perform arithmetic on global 'z' (a nil value)
But the uservariable "HallwayPir" is not nil and I would expect a value.
Any one that can point me to my mistake?
/lennart
User avatar
cyberclwn
Posts: 103
Joined: Thursday 20 August 2015 22:53
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Error on 'nil' value that should not be 'nil'...

Post by cyberclwn »

Hey,

First try debug some yourself. Try print out the variables in the logging with

Code: Select all

print ("Status z: "..z)
But i think "uservariables['HallwayPir']" doesn't contain a number.
If it needs to be a date, you probably need "uservariables_lastupdate['HallwayPir']"

But try figure it out with debugging and print the things you are comparing
3xPi 2B (Domoticz "live", Domoticz "sandbox", PhotoFrame)
RFXCom433(E), KaKu, Oregon Scientific, Keyes 2-relay, Logitech Media Server, MiLight, Smartwares heating controller(2x), IR Send/Receive, Keyes PIR, XH-M131 DuskSensor, DHT22/11
User avatar
jvdz
Posts: 2332
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Error on 'nil' value that should not be 'nil'...

Post by jvdz »

To calculate a time difference in LUA you should use os.difftime(t1, t2).
This is an example function to calculate the difference with the current time:

Code: Select all

-- Calculate the time difference in seconds between Now and supplied time. in HH:MM:SS
function timedifferencenow(s)
--~ 	print(" s:" .. s .. "   e:" .. e )
	s = s .. ":00" -- add seconds in case only hh:mm is supplied
	timenow = os.date("*t")
	year = timenow.year
	month = timenow.month
	day = timenow.day
	hour = string.sub(s, 1, 2)
	minutes = string.sub(s, 4, 5)
	seconds = string.sub(s, 7, 8)
	t1 = os.time()
	t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
	difference = os.difftime(t1, t2)
	return difference
end

print(" Time difference with 11:00 in seconds: " .. timedifferencenow("11:00"))
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Error on 'nil' value that should not be 'nil'...

Post by leby »

Thx both,
@ cyberclwn, the HallwayPir is a number, and if I have understood lua it does not matter that it is stored as string, lua will interpret it as.
@ jvdz its not the difference calculation that is the problem really but I can perhaps combine your answers, or I simply copy the smart lua script in the wiki.
/lennart
User avatar
jvdz
Posts: 2332
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Error on 'nil' value that should not be 'nil'...

Post by jvdz »

So what exactly is the content of HallwayPir when you get the nil error?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Error on 'nil' value that should not be 'nil'...

Post by leby »

To illustrate I created a new time script

Code: Select all

commandArray = {}

	z = uservariables['OutsidePir']
	h = uservariables['HallwayPir']
	w = os.time()
	x = w-z
	print('z '..z)
	print('w '..w)
	print('x '..x)
	print('h '..h)

return commandArray
and the the result was the same, nil value in the log well just for the sake of it I changed the name of the variable.
From HallwayPir to DownstairPir. so no the script looks like this

Code: Select all

commandArray = {}

	z = uservariables['OutsidePir']
	h = uservariables['DownstairPir']
	w = os.time()
	x = w-z
	print('z '..z)
	print('w '..w)
	print('x '..x)
	print('h '..h)

return commandArray
In the log I expected to see the same error due to nil value, to my surprise the print was now

Code: Select all

2016-08-14 08:24:00.508 LUA: z 1470969772
2016-08-14 08:24:00.508 LUA: w 1471155840
2016-08-14 08:24:00.508 LUA: x 186068
2016-08-14 08:24:00.508 LUA: h 1470969772
So the problem i solved but I don't understand why?
Regarding my calculation I have understood that the time numbers are so many seconds that have passed since 1 January 1970. So for me it works well to use this to calculate different events around.

Thx jos for inputs!
/lennart
mrf68

Re: Error on 'nil' value that should not be 'nil'...

Post by mrf68 »

I think that the problem could have been that a variable had the same name as a device. I had a similar problem when I created a dummy thermostat and gave it the same name as a thermometer.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest