Lua script to extract data from json Topic is solved

Moderator: leecollings

Post Reply
samhh83
Posts: 6
Joined: Sunday 10 December 2017 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Lua script to extract data from json

Post 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.
Attachments
solar.txt
(1.21 KiB) Downloaded 402 times
Last edited by samhh83 on Sunday 10 December 2017 22:59, edited 2 times in total.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to extract data from json

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
samhh83
Posts: 6
Joined: Sunday 10 December 2017 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script to extract data from json

Post 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.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to extract data from json

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
samhh83
Posts: 6
Joined: Sunday 10 December 2017 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script to extract data from json

Post by samhh83 »

You are wright, I just shortened the file and forgot to remove a comma at the end.
Hereby a revised version.
Attachments
solar.txt
(1.21 KiB) Downloaded 432 times
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to extract data from json

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to extract data from json

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
samhh83
Posts: 6
Joined: Sunday 10 December 2017 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script to extract data from json

Post by samhh83 »

again your wright sorry, it's late.... :oops: :oops: :oops: it has to be " jsonData.lastReportWatts"
samhh83
Posts: 6
Joined: Sunday 10 December 2017 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua script to extract data from json

Post by samhh83 »

Thanks that did the trick..
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest