Page 1 of 1

Lua script to extract data from json

Posted: Sunday 10 December 2017 21:59
by samhh83
Hello everybody

i'm looking around on the forum and the rest of the internet but i can't seem to find the right anwser. At home we have Enphase solar panels& gateway. I can get the production and consumption from the gateway and into virtual kwh meters. Now am i trying to parse the data i got from the gateway about every individual panel into seperate meters so to check if they are working accordingly. I have attached a piece of the json. My question is how do i get the data from "lastReportWatts" into a variable?
commandArray = {}
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux

-- API call
local config=assert(io.popen('curl "http://address/solar.json"')) -- json data off all solar panels
local Stringjson = config:read('*all')
config:close()
local jsonData = json:decode(Stringjson)

Current_watt = jsonData.lastReportWatts

This trows an error : attempt to index global 'devicechanged' (a nil value)
Attecht is the json converted to txt.

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 22:09
by jvdz
samhh83 wrote: Sunday 10 December 2017 21:59 This trows an error : attempt to index global 'devicechanged' (a nil value)
The shown code won't throw that error as it doesn't contain the devicechanged table.
How are you running this LUA script? Internal editor? If so, you need to change the Event type to Device in stead of All.

Jos

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 22:21
by samhh83
Hi jvdz,
my mistake wrong error code. The wright one is "[string "commandArray = {} ..."]:16: attempt to concatenate global 'Inverter' (a nil value)"
I'm trying to get the "lastReportWatts" from every panel in separate meters. Normally it is something in the Line of:
local Variable = json:decode(raw json)
variable_to_sensor = Variable.index.data
But in this json there is no index to specify which array to get the data from.

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 22:34
by jvdz
Iam getting an different error as it seems the JSON file you attached isn't complete:
can't parse JSON at char 1179 of: [

Could you check the data in the file?

Jos

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 22:48
by samhh83
You are wright, I just shortened the file and forgot to remove a comma at the end.
Hereby a revised version.

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 22:56
by jvdz
Ok, so now it is indeed a valid JSON file, but it doesn't contain the field energy_today.
What data do you want the extract from the JSON?

Jos

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 23:01
by jvdz
Just as an example how to process this JSON: This lists all occurences of maxReportWatts

Code: Select all

local jsonData = json:decode(Stringjson)
tc = #jsonData
for i = 1, tc do
	print(jsonData[i].maxReportWatts)
end
Jos

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 23:04
by samhh83
again your wright sorry, it's late.... :oops: :oops: :oops: it has to be " jsonData.lastReportWatts"

Re: Lua script to extract data from json

Posted: Sunday 10 December 2017 23:09
by samhh83
Thanks that did the trick..