HTTPRespons and json

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

Moderator: leecollings

Post Reply
Maxx
Posts: 61
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

HTTPRespons and json

Post by Maxx »

Hello All,

I am experimenting with HTTPResponse and json and I have a question about accessing the data in a json table.

This is the call I do:

Code: Select all

			url = 'http://192.168.1.231:8080/json.htm?type=devices&rid=230E',
				method = 'GET',
				callback = 'trigger', -- see httpResponses above.
then I access the json with:

Code: Select all

	if (item.isHTTPResponse) then

			if (item.statusCode == 200) then
				if (item.isJSON) then

					local someValue = item.json.Sunrise 
This gives me the Sunrise time perfectly and I can put in a dummy text device.

Now the question:

If the json looks like:

Code: Select all

"Sunrise" : "08:15",
   "Sunset" : "17:34",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 100,
         "CustomImage" : 0,
         "Data" : "17.5 C, 55 %",
How do I get the variable "Data" in the dummy text device?

I have tried all kinds of combinations:

Code: Select all

local someValue = item.json.result.Data  - Doesn't work returns "nil"
local someValue = item.json.result:Data  - Doesn't work returns "nil"
local someValue = item.json.["result.Data"]  - Doesn't work returns "nil"

I hope someone can help me
User avatar
Marci
Posts: 532
Joined: Friday 22 January 2016 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Wakefield, West Yorkshire UK
Contact:

Re: HTTPRespons and json

Post by Marci »

Code: Select all

local someValue = item.json.result[0]['Data'];
Result is an array containing an object. As it's the only object in the array, it sits at the first index, and a JavaScript array's first index position is 0.
Extended Domoticz homebridge-plugin for latest Homebridge - adds temp/humidity/pressure sensors, power consumption sensors, DarkSkies virtual weather station support, YouLess Meter support, general % usage support & switch/lamp status checking!
Maxx
Posts: 61
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: HTTPRespons and json

Post by Maxx »

Thank you for your quick response, I've tried this but it gives me the following error:

Code: Select all

 2018-02-04 12:55:01.218 dzVents: Error (2.4.1): An error occured when calling event handler http test
2018-02-04 12:55:01.218 dzVents: Error (2.4.1): ...domoticz/scripts/dzVents/generated_scripts/http test.lua:25: attempt to index field 'result' (a nil value)
2018-02-04 12:55:01.218 dzVents: Info: ------ Finished http test
User avatar
Marci
Posts: 532
Joined: Friday 22 January 2016 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Wakefield, West Yorkshire UK
Contact:

Re: HTTPRespons and json

Post by Marci »

Sorry, my fault. LUA not JavaScript. In LUA array first index is 1 not 0. Change result[0] to result [1]
Extended Domoticz homebridge-plugin for latest Homebridge - adds temp/humidity/pressure sensors, power consumption sensors, DarkSkies virtual weather station support, YouLess Meter support, general % usage support & switch/lamp status checking!
Maxx
Posts: 61
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: HTTPRespons and json

Post by Maxx »

perfect, it works.
Thanks
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: HTTPRespons and json

Post by dannybloe »

My question is though: why have this script in the first place? I'd say that dzVents already gives you all this data. No need to get it from Domoticz from the outside-in. Or is there something missing in dzVents?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Maxx
Posts: 61
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: HTTPRespons and json

Post by Maxx »

Hello Danny,

It was just a test to see how it works, the next step for me is to do a json call to Hue to directly to check the status of the lights. (see my other message). The domoticz status is not updated when the light is offline.

thanks
elmortero
Posts: 248
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: HTTPRespons and json

Post by elmortero »

dannybloe wrote: Thursday 08 February 2018 7:52 My question is though: why have this script in the first place? I'd say that dzVents already gives you all this data. No need to get it from Domoticz from the outside-in. Or is there something missing in dzVents?
Well, now that you mention it:
Maybe I am overlooking something, but is it possible to query the domoticz settings (in particular longitude and latitude)?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: HTTPRespons and json

Post by dannybloe »

Of course but you have to make sure that 127.0.0.1 (or whatever url you use) is listed to access the API without a password (settings of Domoticz).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest