Page 1 of 1
Error from LUA script
Posted: Sunday 26 June 2022 10:41
by MikeyMan
I have a script running that calculates the average outside temperature. This has been running for a long time without any issues now.
However, recently i'm starting to get errors:
Code: Select all
2022-06-25 00:45:00.740 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:32: attempt to index a nil value (local 'jsonData')
Haven't change the script, which looks as follows:
Code: Select all
local function DeviceUpdate(idx, value1)
local cmd = string.format("%d|0|%.2f", idx, value1)
--print(cmd)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
Debug = "YES" -- Debugging aanzetten ("YES") of uitzetten ("NO")
commandArray = {}
local m = os.date('%M')
if (m % 5 == 0) then -- script loopt iedere 5 minuten
-- Variabelen instellen
--local temperature_name = 'Gemiddelde Buitentemp'
local temperature_idx = 64 --otherdevices_idx[temperature_name]
--local uitlees_name = 'Ground Temperature (10 cm)'
local uitlees_idx = 50 --otherdevices_idx[uitlees_name]
--Temperaturen van afgelopen 24 uur opvragen
local sQuery = 'curl "http://10.0.0.30:8080/json.htm?type=graph&sensor=temp&idx='..tostring(uitlees_idx)..'&range=day"'
local handle=assert(io.popen(sQuery))
local raw = handle:read('*all')
handle:close()
--Ontvangen waarden verwerken
json = assert(loadfile '/home/pi/domoticz/scripts/lua/JSON.lua')()
local jsonData = json:decode(raw)
if Debug=='YES' then
print (jsonData.title)
print (jsonData.status)
print (jsonData.result[1].d)
print (jsonData.result[1].te)
end
--Gemiddelde temperatuur van afgelopen 24 uur bepalen
local sum = 0
local ave = 0
local elements = #jsonData.result
for i = 1, elements do
sum = sum + jsonData.result[i].te
end
ave = sum / elements
if Debug=='YES' then
print('Gemiddeld: '..tostring(ave))
end
--Gemiddelde temperatuur opslaan in virtuele temperatuur sensor
DeviceUpdate(temperature_idx,ave)
end
return commandArray
Anyone got any idea what is going wrong here?
Re: Error from LUA script
Posted: Sunday 26 June 2022 11:43
by psubiaco
What about replacing the line
with
Code: Select all
commandArray[temperature_name]=tostring(ave)
?
It's much simpler than calling DeviceUpdate()
Also, it's better you check the result of query to get the list of temperature values, and check that format is ok, values are float, ...
Re: Error from LUA script
Posted: Sunday 26 June 2022 12:04
by MikeyMan
psubiaco wrote: ↑Sunday 26 June 2022 11:43
What about replacing the line
with
Code: Select all
commandArray[temperature_name]=tostring(ave)
?
It's much simpler than calling DeviceUpdate()
Also, it's better you check the result of query to get the list of temperature values, and check that format is ok, values are float, ...
I believe you immediately! I'll try your suggestion and see what happens.
My coding skills are virtually non existent

Is the query logged somewhere?
Re: Error from LUA script
Posted: Monday 27 June 2022 7:33
by psubiaco
MikeyMan wrote: ↑Sunday 26 June 2022 12:04
Is the query logged somewhere?
Everything is logged by Domoticz, in database, syslog or in the file specified in Domoticz run script /etc/init.d/domoticz
Use the print() function to log anything else than errors.
Re: Error from LUA script
Posted: Monday 27 June 2022 9:29
by MikeyMan
Haha, domoticz.log in /var/log is 2 gb's. Lets see whats in there.
Edit; that's the same info as in the GUI.
However, did pinpoint that it wasn't working as correctly as i thought. Values did show up, however:
Code: Select all
2021-11-15 00:00:11.472 Error: EventSystem: Warning!, lua script Gemiddelde temperatuur has been running for more than 10 seconds
Thats a long time ago already.
First occurence of the current error was last year:
Code: Select all
2021-12-15 17:20:00.512 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1)
..."]:32: attempt to index a nil value (local 'jsonData')
And some variations of the error:
Code: Select all
2021-12-18 01:00:10.502 Error: EventSystem: Warning!, lua script /home/pi/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
Strangely the error has been gone for a while and then came back... Very strange.
Trying to run the curl i get the following:
Code: Select all
pi@raspberrypi:~ $ curl "http://10.0.0.30:8080/json.htm?type=graph&sensor=temp&idx=50&range=day"
-bash: curl: command not found
Re: Error from LUA script
Posted: Monday 27 June 2022 9:56
by MikeyMan
What the...
After installing curl it was fixed it seems. How did that get removed?
Slightly different error now:
Code: Select all
2022-06-27 10:00:00.497 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
2022-06-27 10:05:00.572 Status: LUA: Gemiddeld: 17.603718393654
2022-06-27 10:05:00.572 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
2022-06-27 10:10:01.305 Status: LUA: Gemiddeld: 17.606246901339
2022-06-27 10:10:01.306 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
Reverted to the earlier script, and now the error is gone.
Re: Error from LUA script
Posted: Monday 27 June 2022 10:54
by psubiaco
I already suggested you to forget buggy DeviceUpdate function and just use
commandArray[DEVICENAME]=tostring(VALUE)
to pass the result to domoticz.
Re: Error from LUA script
Posted: Monday 27 June 2022 10:57
by MikeyMan
psubiaco wrote: ↑Monday 27 June 2022 10:54
I already suggested you to forget buggy DeviceUpdate function and just use
commandArray[DEVICENAME]=tostring(VALUE)
to pass the result to domoticz.
Thats what i tried. That resulted in the following error:
Code: Select all
2022-06-27 10:00:00.497 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
2022-06-27 10:05:00.572 Status: LUA: Gemiddeld: 17.603718393654
2022-06-27 10:05:00.572 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
2022-06-27 10:10:01.305 Status: LUA: Gemiddeld: 17.606246901339
2022-06-27 10:10:01.306 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
Changing back to the earlier code did fix that.
Re: Error from LUA script
Posted: Monday 27 June 2022 11:01
by MikeyMan
psubiaco wrote: ↑Monday 27 June 2022 10:54
I already suggested you to forget buggy DeviceUpdate function and just use
commandArray[DEVICENAME]=tostring(VALUE)
to pass the result to domoticz.
Thats what i tried. That resulted in the following error:
Code: Select all
2022-06-27 10:00:00.497 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
2022-06-27 10:05:00.572 Status: LUA: Gemiddeld: 17.603718393654
2022-06-27 10:05:00.572 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
2022-06-27 10:10:01.305 Status: LUA: Gemiddeld: 17.606246901339
2022-06-27 10:10:01.306 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
Changing back to the earlier code did fix that.
Now trying to swap temperature_name for temperature_idx.
Re: Error from LUA script
Posted: Monday 27 June 2022 11:01
by psubiaco
As you see, the error is in the DeviceUpdate function, so you have to erase that function as it's not used anymore.
Re: Error from LUA script
Posted: Monday 27 June 2022 11:10
by MikeyMan
psubiaco wrote: ↑Monday 27 June 2022 11:01
As you see, the error is in the DeviceUpdate function, so you have to erase that function as it's not used anymore.
To recap, after installing curl, this was working without errors:
This was not working:
Code: Select all
commandArray[temperature_name]=tostring(ave)
with this error:
Code: Select all
2022-06-27 10:00:00.497 Error: EventSystem: in Gemiddelde temperatuur: [string "local function DeviceUpdate(idx, value1) ..."]:55: table index is nil
After uncommenting this line:
Code: Select all
local temperature_name = 'Gemiddelde Buitentemp'
I get no errors any more.
Re: Error from LUA script
Posted: Monday 27 June 2022 11:25
by psubiaco
Ok, if you prefer you can also use
commandArray['Gemiddelde Buitentemp']=tostring(ave)
so, in this case, you don't need to define the variable temperature_name='Gemiddelde Buitentemp' as you did.
Re: Error from LUA script
Posted: Tuesday 28 June 2022 12:05
by MikeyMan
psubiaco wrote: ↑Monday 27 June 2022 11:25
Ok, if you prefer you can also use
commandArray['Gemiddelde Buitentemp']=tostring(ave)
so, in this case, you don't need to define the variable temperature_name='Gemiddelde Buitentemp' as you did.
Somehow it does run then, but doesnt update the value.
Script is now:
Code: Select all
local function DeviceUpdate(idx, value1)
local cmd = string.format("%d|0|%.2f", idx, value1)
--print(cmd)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
Debug = "YES" -- Debugging aanzetten ("YES") of uitzetten ("NO")
commandArray = {}
local m = os.date('%M')
if (m % 5 == 0) then -- script loopt iedere 5 minuten
-- Variabelen instellen
--local temperature_name = 'Gemiddelde Buitentemp'
local temperature_idx = 64 --otherdevices_idx[temperature_name]
--local uitlees_name = 'Ground Temperature (10 cm)'
local uitlees_idx = 50 --otherdevices_idx[uitlees_name]
--Temperaturen van afgelopen 24 uur opvragen
local sQuery = 'curl "http://10.0.0.30:8080/json.htm?type=graph&sensor=temp&idx='..tostring(uitlees_idx)..'&range=day"'
local handle=assert(io.popen(sQuery))
local raw = handle:read('*all')
handle:close()
--Ontvangen waarden verwerken
json = assert(loadfile '/home/pi/domoticz/scripts/lua/JSON.lua')()
local jsonData = json:decode(raw)
if Debug=='YES' then
print (jsonData.title)
print (jsonData.status)
print (jsonData.result[1].d)
print (jsonData.result[1].te)
end
--Gemiddelde temperatuur van afgelopen 24 uur bepalen
local sum = 0
local ave = 0
local elements = #jsonData.result
for i = 1, elements do
sum = sum + jsonData.result[i].te
end
ave = sum / elements
if Debug=='YES' then
print('Gemiddeld: '..tostring(ave))
end
--Gemiddelde temperatuur opslaan in virtuele temperatuur sensor
--DeviceUpdate(temperature_idx,ave)
commandArray['Gemiddelde Buitentemp']=tostring(ave)
end
return commandArray
Re: Error from LUA script
Posted: Tuesday 28 June 2022 12:30
by psubiaco
Hi,
my mistake: commandArray in the simplest format can be used only with integer values.
In your case, you can use commandArray[1]={['UpdateDevice']=idx|0|value}:
Code: Select all
-- commandArray['Gemiddelde Buitentemp']=tostring(ave) -- works only if ave is integer!
commandArray[1]={['UpdateDevice']=temperature_idx..'|0|'..ave}
Re: Error from LUA script
Posted: Tuesday 28 June 2022 12:44
by MikeyMan
psubiaco wrote: ↑Tuesday 28 June 2022 12:30
Hi,
my mistake: commandArray in the simplest format can be used only with integer values.
In your case, you can use commandArray[1]={['UpdateDevice']=idx|0|value}:
Code: Select all
-- commandArray['Gemiddelde Buitentemp']=tostring(ave) -- works only if ave is integer!
commandArray[1]={['UpdateDevice']=temperature_idx..'|0|'..ave}
Thanks. I'll give that a try.
Strange it isn't throwing an error though?