Page 1 of 1

Error: EventSystem: in *event: [string " ..."]:18: attempt to compare number with nil

Posted: Sunday 24 September 2017 11:21
by Evelen
Hi.
I try to make my first LUA script to warn me if the temperature is bellow/ower a number
the script works perfect in my LUA editor ZeroBrane Studio (with temp as a variable), but when I try it in Domoticz I got this message:

Error: EventSystem: in Kjøleskap alarm: [string " ..."]:18: attempt to compare number with nil

Code: Select all


--- Configuration
local temp = 'Kjøleskap'
local upper_temp_limit = 15
local lower_temp_limit = 2
local upper_temp_limit_warning_msg = "Øvre temperaturgrense " .. upper_temp_limit .. "ºC er nådd!"
local lower_temp_limit_warning_msg = "Nedre temperaturgrense " .. upper_temp_limit .. "ºC er nådd!"

--commandArray = {}

temp = tonumber(temp)

if (warning_sent == nil) then
    warning_sent = false
end

if (upper_temp_limit < temp and warning_sent ~= true) then
  print(upper_temp_limit_warning_msg)
  warning_sent = true
  
elseif (lower_temp_limit > temp and warning_sent ~= true) then
  print(lower_temp_limit_warning_msg)
  warning_sent = true
  
elseif (upper_temp_limit > temp and lower_temp_limit < temp) then
    print("Temp ok")
  warning_sent = false

end

--return commandArray

Anyone se what's wrong? also, do I need the CommandArray thing? (gives the same result with and without)

Re: Error: EventSystem: in *event: [string " ..."]:18: attempt to compare number with nil

Posted: Sunday 24 September 2017 11:40
by zicht
Just a quick view :

Is it possible that your temp is a string set in the beginning of a script to a name ?

Code: Select all

local temp = 'Kjøleskap'
So in the comparisation you are compairing a number with a string in the if-then ?

Code: Select all

if (upper_temp_limit < temp and warning_sent ~= true) then
maybe it should be local temp = otherdevices['Kjøleskap'] to get the value of 'Kjøleskap' but thats just an assumption.
(You need to compare numbers with numbers and strings with strings.)

Re: Error: EventSystem: in *event: [string " ..."]:18: attempt to compare number with nil

Posted: Sunday 24 September 2017 13:18
by Evelen
thanks :)
Also, another mistake was that the name of the device was wrong..

this gives me the value:

Code: Select all

local temp_device = "29 - Kjøleskap"
local temp = tonumber(otherdevices_svalues[temp_device])
print(temp)