LUA json API what is wrong ? Help.

Moderator: leecollings

Post Reply
janojj
Posts: 4
Joined: Sunday 14 May 2017 19:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7450
Location: Poland
Contact:

LUA json API what is wrong ? Help.

Post by janojj »

Hello
I'm just starting with LUA I have a problem with the API and script.
Address to API http://powietrze.malopolska.pl/_powietr ... a&ci_id=23

My script that will not process data

Code: Select all

       json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux

       local config=assert(io.popen('curl http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=23'))
       local location = config:read('*all')
       config:close()
       local jsonLocation = json:decode(location)

    d1 = jsonLocation.dane.actual.details.0.par_desc
d2 = jsonLocation.dane.actual.details.0.g_nazwa
d3 = jsonLocation.dane.actual.details.1.par_desc
d4 = jsonLocation.dane.actual.details.1.g_nazwa
d5 = jsonLocation.dane.actual.details.2.par_desc
d6 = jsonLocation.dane.actual.details.2.g_nazwa
   print('Lat: '..d1)

Wants to download data

d1 = jsonLocation.dane.actual.details.0.par_desc
d2 = jsonLocation.dane.actual.details.0.g_nazwa
d3 = jsonLocation.dane.actual.details.1.par_desc
d4 = jsonLocation.dane.actual.details.1.g_nazwa
d5 = jsonLocation.dane.actual.details.2.par_desc
d6 = jsonLocation.dane.actual.details.2.g_nazwa

I have such a mistake
Malformed number near '.0.par_desc'

When he wants to download other data
d1 = jsonLocation.dane.city.ci_name
lua: test.lua:9: attempt to index field 'dane' (a nil value)
stack traceback:
test.lua:9: in main chunk
[C]: ?

Please help the new one.
zicht
Posts: 272
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: LUA json API what is wrong ? Help.

Post by zicht »

--
Last edited by zicht on Tuesday 13 June 2017 13:07, edited 1 time in total.
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
zicht
Posts: 272
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: LUA json API what is wrong ? Help.

Post by zicht »

I have no clue of what you try to do at all..
Last edited by zicht on Tuesday 13 June 2017 19:43, edited 1 time in total.
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA json API what is wrong ? Help.

Post by jvdz »

Shouldn't that be:

Code: Select all

d1 = jsonLocation.dane.actual.details[0].par_desc
etc..
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
janojj
Posts: 4
Joined: Sunday 14 May 2017 19:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7450
Location: Poland
Contact:

Re: LUA json API what is wrong ? Help.

Post by janojj »

Hello so this is my start in lua. Try

d1 = jsonLocation.dane.forecast.dzisiaj.details[0].fo_nazwa

Unfortunately it does not work

lua: 1.lua:8: attempt to index field 'dane' (a nil value)
stack traceback:
1.lua:8: in main chunk
[C]: ?

what is wrong?

please help.
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA json API what is wrong ? Help.

Post by jvdz »

Actually several things are wrong. A simple debug statement revealed that the curl returned: {"exception":"Nieprawid\u0142owy parametr"}

So I did some testing and made the required modifications to your script:

Code: Select all

-- json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
local json = require "JSON" -- for raspberry
local config = assert(io.popen('curl "http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=23"'))
local location = config:read('*all')
config:close()
--~ print(location)
local jsonLocation = json:decode(location)
d1 = jsonLocation.dane.actual[1].details[1].par_desc
d2 = jsonLocation.dane.actual[1].details[1].g_nazwa
d3 = jsonLocation.dane.actual[1].details[2].par_desc
d4 = jsonLocation.dane.actual[1].details[2].g_nazwa
d5 = jsonLocation.dane.actual[1].details[3].par_desc
d6 = jsonLocation.dane.actual[1].details[3].g_nazwa
print('Lat: '..d1)
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
janojj
Posts: 4
Joined: Sunday 14 May 2017 19:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7450
Location: Poland
Contact:

Re: LUA json API what is wrong ? Help.

Post by janojj »

Hello
I tested

Code: Select all

       json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux

       local config=assert(io.popen('curl http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=21'))
       local location = config:read('*all')
       config:close()
       local jsonLocation = json:decode(location)

d1 = jsonLocation.dane.forecast.dzisiaj[0].details[0].par_name
   print('Lat:'..d1)
it still does not work. What am I doing wrong
Thanks in advance for your help
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA json API what is wrong ? Help.

Post by jvdz »

Did the code I gave work for you?
Why do you use [0] in stead of [1] like I did? The decoded array starts with [1] I think.
EDIT: I see you use a different url and guess you need this key:
d1 = jsonLocation.dane.forecast.dzisiaj.details[1].par_name

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
janojj
Posts: 4
Joined: Sunday 14 May 2017 19:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7450
Location: Poland
Contact:

Re: LUA json API what is wrong ? Help.

Post by janojj »

Thank you very much for help. The script works great. :lol:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest