Page 1 of 1
Parse variable from JSON?
Posted: Tuesday 18 July 2017 13:00
by EdddieN
Hello there,
I have this JSON reply from a HTTP call:
Code: Select all
{"Label":["12/07/2017","13/07/2017","14/07/2017","15/07/2017","16/07/2017","17/07/2017","18/07/2017"],"Data":["860","877","877","860","860","860","860"]}
I'm only interested on the last data value, the last "860" number that changes over time. It is an ultrasonic oil tank meter so I want to add the remaining litres to Domoticz but I have no idea how to do this. I have experimented a bit with LUA in the past but parsing data from a HTTP call is a complete different thing for me.
The call I do (on browser) is:
Code: Select all
http://website.com/Report/GetChartData?SerialNo=SerialID&DeviceNo=0&NumericPeriod=0
Any pointer to help me get started would be fantastic!

Re: Parse variable from JSON?
Posted: Tuesday 18 July 2017 13:20
by Egregius
Easy in PHP, no clue in Lua
Code: Select all
<?php
$data=json_decode(file_get_contents('http://website.com/Report/GetChartData?SerialNo=SerialID&DeviceNo=0&NumericPeriod=0
'),true);
echo $data['Data'][6];
Re: Parse variable from JSON?
Posted: Tuesday 18 July 2017 14:18
by mivo
Hi,
there should be JSON.lua library in scripts/lua Domoticz subfolder. Documented here or in script itself:
https://www.domoticz.com/wiki/Lua_-_json.lua
Re: Parse variable from JSON?
Posted: Tuesday 18 July 2017 15:35
by EdddieN
aha! I missed that completely within the wiki. Thanks for the link

Re: Parse variable from JSON?
Posted: Wednesday 19 July 2017 19:56
by EdddieN
I must admit, it looks so simple in PHP... I need to look into it.
Anyway, this is how I managed at the end. Since I'm a LUA (and PHP) ignorant, feel free to improve on it. It seems to work for me:
Code: Select all
local t1 = os.date("%H") -- get hour time digit
commandArray = {}
if t1%23 == 0 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://website.com/Report/Getdata..."'))
local Stringjson = config:read('*all')
config:close()
local jsonData = json:decode(Stringjson)
oil = jsonData.Data[6]
print (Stringjson) -- debug json
print (oil) -- parsed json value
commandArray['UpdateDevice'] = 465 .. "|0|" .. oil -- update sensor value
end
return commandArray
Re: Parse variable from JSON?
Posted: Thursday 20 July 2017 13:43
by EdddieN
Ooopps my mistake. The script will run for 1 hours at 23:00, so it needs a something like:
Code: Select all
local t2 = os.date("%M") -- get minute time digit
..
..
if t1%23 == 0 and t2%01 == 00 then
..
..
Re: Parse variable from JSON?
Posted: Monday 24 July 2017 22:44
by Ittiz
I believe there is now a JSONdecode fuction for LUA in the latest beta version of Domoticz, no library needed.
Re: Parse variable from JSON?
Posted: Saturday 29 July 2017 20:56
by EdddieN
Still on stable version...
Quick thing, I added a new device for calculating the level of the tank, something like this:
Code: Select all
Oilpercentage = (oil * 100) / 1400
print (Oilpercentage)
commandArray['UpdateDevice'] = 465 .. "|0|" .. oil -- update sensor value
commandArray['UpdateDevice'] = 472 .. "|0|" .. Oilpercentage -- update sensor value
and now, only the last one "Oilpercentage" updates, the other one does not. Any idea why?
Re: Parse variable from JSON?
Posted: Sunday 30 July 2017 9:44
by mivo
Yes, because last assignment of commandArray wins. Multiple commands should be passed in LUA table like this:
Code: Select all
commandArray[1]={['UpdateDevice']='465|0|' .. oil}
commandArray[2]={['UpdateDevice']='472|0|' .. oilPercentage}
Re: Parse variable from JSON?
Posted: Sunday 30 July 2017 12:43
by EdddieN
mivo wrote:Yes, because last assignment of commandArray wins. Multiple commands should be passed in LUA table like this:
thanks, that did the trick!
Re: Parse variable from JSON?
Posted: Wednesday 11 April 2018 20:52
by EdddieN
This has been working well for a long while, however it always reports the third number from the end. This is the reply:
{"Label":["05/04/2018","06/04/2018","07/04/2018","09/04/2018","10/04/2018","11/04/2018"],"Data":["407","407","376","360","360","344"]}
and my code:
Code: Select all
local t1 = os.date("%H") -- get hour time digit
local t2 = os.date("%M") -- get minute time digit
commandArray = {}
if t1 == "22" and t2 == "30" then -- only poll at 11pm every day
json = (loadfile "/home/pi/domosense/scripts/lua/JSON.lua")() -- For Linux
-- API call
local config=assert(io.popen('curl "http://website.com/Report/GetData?SerialNo=xxxxx"'))
local Stringjson = config:read('*all')
config:close()
local jsonData = json:decode(Stringjson)
oil = jsonData.Data[6]
-- print (Stringjson) -- debug json
print (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
Instead of returning 344, it returns 360. Any ideas where am I going wrong?
Re: Parse variable from JSON?
Posted: Tuesday 08 May 2018 22:57
by EdddieN
Is this still working on the latest beta V3.9390 :
https://www.domoticz.com/wiki/Lua_-_json.lua
After an update it seems to have stopped for me.