Parse variable from JSON? Topic is solved

Moderator: leecollings

Post Reply
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Parse variable from JSON?

Post 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! :D
11101101 - www.machinon.com
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Parse variable from JSON?

Post 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]; 
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Parse variable from JSON?

Post 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
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Parse variable from JSON?

Post by EdddieN »

aha! I missed that completely within the wiki. Thanks for the link :)
11101101 - www.machinon.com
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Parse variable from JSON?

Post 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

11101101 - www.machinon.com
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Parse variable from JSON?

Post 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
..
..
11101101 - www.machinon.com
Ittiz
Posts: 48
Joined: Tuesday 03 January 2017 0:37
Target OS: Linux
Domoticz version: 13939
Location: USA
Contact:

Re: Parse variable from JSON?

Post by Ittiz »

I believe there is now a JSONdecode fuction for LUA in the latest beta version of Domoticz, no library needed.
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Parse variable from JSON?

Post 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?
11101101 - www.machinon.com
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Parse variable from JSON?

Post 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}
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Parse variable from JSON?

Post 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!
11101101 - www.machinon.com
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Parse variable from JSON?

Post 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?
11101101 - www.machinon.com
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Parse variable from JSON?

Post 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.
11101101 - www.machinon.com
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest