I have a URL that when I call it, it returns this:
The Data numbers can be up to 6 max, those represent the number of days from the date before.{"Label":["28/09/2018","29/09/2018"],"Data":["1195","1181"]}
Until now I have used this LUA script which seems a bit obsolete and sometimes does not work very well, for example it only returns a value if I have 6 values on the Data reply:
Code: Select all
local t1 = os.date("%H") -- get hour time digit
local t2 = os.date("%M") -- get minute time digit
commandArray = {}
if t1 == "20" and t2 == "32" then -- only poll at 11pm every day
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
-- API call
local config=assert(io.popen('curl "http://urladdress.com/Report/GetChartData?SerialNo=1234&DeviceNo=0&NumericPeriod=0"'))
local Stringjson = config:read('*all')
config:close()
local jsonData = json:decode(Stringjson)
local oil = jsonData.Data[6]
-- print ("JSON DATA" ..Stringjson) -- debug json
print ("Litres " ..tostring(oil)) -- parsed json value
-- commandArray['UpdateDevice'] = 465 .. "|0|" .. oil -- update sensor value
Oilpercentage = (oil * 100) / 1250
-- print (Oilpercentage)
-- commandArray['UpdateDevice'] = 472 .. "|0|" .. Oilpercentage -- update sensor value
commandArray[1]={['UpdateDevice']='465|0|' .. oil}
commandArray[2]={['UpdateDevice']='472|0|' .. Oilpercentage}
end
return commandArray