Access multiple json.item objects

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
infoed
Posts: 7
Joined: Monday 21 October 2024 14:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Access multiple json.item objects

Post by infoed »

Hi,
I 'm looking for a way to process multiple HTTP resonses of different openurl results.

E.g :
Open 2 different graph API calls for 2 different devices (idx 1 and idx2)

url1 = dz.settings['Domoticz url'] .. '/json.htm?type=command&param=graph&sensor=counter&range=year&idx=' .. id1
dz.openURL({ url = url1, callback = scriptVar1 })
url2 = dz.settings['Domoticz url'] .. '/json.htm?type=command&param=graph&sensor=counter&range=month&idx=' .. id2
dz.openURL({ url = url2, callback = scriptVar2 })

When I have only one url call, I process the results like this:

-- main
if item.isHTTPResponse and item.isJSON then
updateTextSensor(processResult(item.json))
elseif item.isTimer then
callYearGraph(idx1)
else
dz.log(' Error while retrieving elektra data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data)
end

I know that the result of ONE openurl call is in item.json.

Can somebody give me an example of processing TWO url calls using TWO item.json tables with the different callback methods?

Thanks in advance, InfoEd
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Access multiple json.item objects

Post by waltervl »

You have to check the trigger eg

Code: Select all

if item.isHTTPResponse and item.isJSON then
  if item.trigger ==  'scriptVar1' then
  -- do your scrptvar 1 thing
  elseif item.trigger ==  'scriptVar2' then
  -- do your scrptvar 2 thing
  end
 updateTextSensor(processResult(item.json))
elseif item.isTimer then
   callYearGraph(idx1)
else
  dz.log(' Error while retrieving elektra data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data)
end
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest