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')
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