Page 1 of 1
Error on 'nil' value that should not be 'nil'...
Posted: Thursday 11 August 2016 20:44
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?
Re: Error on 'nil' value that should not be 'nil'...
Posted: Friday 12 August 2016 22:48
by cyberclwn
Hey,
First try debug some yourself. Try print out the variables in the logging with
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
Re: Error on 'nil' value that should not be 'nil'...
Posted: Friday 12 August 2016 23:13
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
Re: Error on 'nil' value that should not be 'nil'...
Posted: Saturday 13 August 2016 8:07
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.
Re: Error on 'nil' value that should not be 'nil'...
Posted: Saturday 13 August 2016 10:52
by jvdz
So what exactly is the content of HallwayPir when you get the nil error?
Jos
Re: Error on 'nil' value that should not be 'nil'...
Posted: Sunday 14 August 2016 8:34
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!
Re: Error on 'nil' value that should not be 'nil'...
Posted: Sunday 14 August 2016 10:29
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.