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.
Lua script to extract data from json Topic is solved
Moderator: leecollings
-
- Posts: 6
- Joined: Sunday 10 December 2017 15:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Lua script to extract data from json
- 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.
- 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
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
-
- 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
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.
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.
- 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
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
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
-
- 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
You are wright, I just shortened the file and forgot to remove a comma at the end.
Hereby a revised version.
Hereby a revised version.
- Attachments
-
- solar.txt
- (1.21 KiB) Downloaded 432 times
- 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
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
What data do you want the extract from the JSON?
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
- 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
Just as an example how to process this JSON: This lists all occurences of maxReportWatts
Jos
Code: Select all
local jsonData = json:decode(Stringjson)
tc = #jsonData
for i = 1, tc do
print(jsonData[i].maxReportWatts)
end
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
-
- 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
again your wright sorry, it's late.... it has to be " jsonData.lastReportWatts"
-
- 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
Thanks that did the trick..
Who is online
Users browsing this forum: No registered users and 1 guest