Page 1 of 1

HTTPRespons and json

Posted: Sunday 04 February 2018 12:27
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

Re: HTTPRespons and json

Posted: Sunday 04 February 2018 12:50
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.

Re: HTTPRespons and json

Posted: Sunday 04 February 2018 12:56
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

Re: HTTPRespons and json

Posted: Sunday 04 February 2018 13:08
by Marci
Sorry, my fault. LUA not JavaScript. In LUA array first index is 1 not 0. Change result[0] to result [1]

Re: HTTPRespons and json

Posted: Sunday 04 February 2018 13:11
by Maxx
perfect, it works.
Thanks

Re: HTTPRespons and json

Posted: Thursday 08 February 2018 7:52
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?

Re: HTTPRespons and json

Posted: Friday 09 February 2018 17:18
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

Re: HTTPRespons and json

Posted: Friday 09 February 2018 17:35
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)?

Re: HTTPRespons and json

Posted: Friday 09 February 2018 17:40
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).