HTTPRespons and json and Hue

Easy to use, 100% Lua-based event scripting framework.
Post Reply
Maxx
Posts: 18
Joined: Saturday 27 January 2018 21:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

HTTPRespons and json and Hue

Post by Maxx » Sunday 04 February 2018 16:03

Hello All,

Taking it one small step further..

I am trying to get the Hue light status directly fom the Hue bridge. (Status in Domoticz is not updated when the light is offline)

I use the following script. (based on the dzvents template)

Code: Select all

return {
	on = {
		timer = {
			'every minute' -- just an example to trigger the request
		},
		httpResponses = {
			'trigger' -- must match with the callback passed to the openURL command
		}
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://192.168.1.105/api/[i]username[/i]/lights/4',
				method = 'GET',
				callback = 'trigger', -- see httpResponses above.
			})
			domoticz.log('Timer event')
		end

		if (item.isHTTPResponse) then

			if (item.statusCode == 200) then
				if (item.isJSON) then
                    domoticz.log('JSON was detected')
                    local someValue = item.json.name

					-- update some device in Domoticz
					domoticz.devices('Tekst').updateText(someValue)
				end
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end
		end

	end
}
When I run the script I do not get error mesages nor any of the logmessages I put in the script. I suspect the HTTPResponse is not ok.
The URL in the browser gives the following result:

Code: Select all

{
  "state": {
    "on": false,
    "bri": 51,
    "alert": "none",
    "mode": "homeautomation",
    "reachable": true
  },
  "swupdate": {
    "state": "noupdates",
    "lastinstall": "2017-11-14T19:35:13"
  },
  "type": "Dimmable light",
  "name": "Veranda 2",
  "modelid": "LWB010",
  "manufacturername": "Philips",
  "capabilities": {
    "streaming": {
      "renderer": false,
      "proxy": false
    }
  },
  "uniqueid": "00:17:88:01:02:b0:10:91-0b",
  "swversion": "1.29.0_r21169",
  "swconfigid": "C40F3F07",
  "productid": "Philips-LWB010-1-A19DLv3"
}
In the Wiki there is a dzvents script "Simple_script_to_update_Philips_Hue_status" but I think with the new dzvents version I should be to use the HTTPResponse and openURL option.


Thanks in advance

Maxx
Posts: 18
Joined: Saturday 27 January 2018 21:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HTTPRespons and json and Hue

Post by Maxx » Sunday 04 February 2018 20:25

Update:

Item is returning with HTTPResonse = 200 so that is ok.


In the errorlog the following error occurs:

Code: Select all

(2.4.1): ...omoticz/scripts/dzVents/generated_scripts/Hue status.lua:28: attempt to index field 'json' (a nil value)
When I check json of Domoticz this works fine ( see previous tread "HTTPRespons and json")

Can somebody help me out here?

dannybloe
Posts: 1481
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: HTTPRespons and json and Hue

Post by dannybloe » Thursday 08 February 2018 8:50

Interesting.. looks like parsing of the response data didn't result in any json code. Try:

Code: Select all

domoticz.log(item.data)
domoticz.log(item.headers)
domoticz.log(domoticz.utils.fromJSON(item.data))
When I feed your json sample above to the json parser I do get a nice Lua table in return.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Maxx
Posts: 18
Joined: Saturday 27 January 2018 21:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HTTPRespons and json and Hue

Post by Maxx » Friday 09 February 2018 18:05

Hello

I did the following:

Code: Select all

      domoticz.log(item.headers)
{["Date"]="Fri, 09 Feb 2018 15:51:00 GMT", ["Access-Control-Max-Age"]="3600", ["Pragma"]="no-cache", ["Cache-Control"]="no-store, no-cache, must-revalidate, post-check=0, pre-check=0", ["Expires"]="Mon, 1 Aug 2011 09:00:00 GMT", ["Content-Type"]="application/json", ["Access-Control-Allow-Origin"]="*", ["Connection"]="close", ["Access-Control-Allow-Headers"]="Content-Type", ["Access-Control-Allow-Credentials"]="true", ["Access-Control-Allow-Methods"]="POST, GET, OPTIONS, PUT, DELETE, HEAD", ["Server"]="nginx"

Code: Select all

        domoticz.log(item.statusCode)
returns 200

Code: Select all

       domoticz.log(item.data)
{"lights":{"4":{"state":{"on":false,"bri":51,"alert":"none","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:35:13"},"type":"Dimmable light","name":"Veranda 2","modelid":"LWB010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":false,"proxy":false}},"uniqueid":"00:17:88:01:02:b0:10:91-0b","swversion":"1.29.0_r21169","swconfigid":"C40F3F07","productid":"Philips-LWB010-1-A19DLv3"},"6":{"state":{"on":true,"bri":92,"hue":62095,"sat":118,"effect":"none","xy":[0.4782,0.3224],"ct":399,"alert":"none","colormode":"xy","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:34:30"},"type":"Extended color light","name":"Zithoek","modelid":"LCT010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":true,"proxy":true}},"uniqueid":"00:17:88:01:02:96:22:52-0b","swversion":"1.29.0_r21169","swconfigid":"6A139B19","productid":"Philips-LCT010-1-A19ECLv4"},"7":{"state":{"on":false,"bri":51,"alert":"none","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:34:32"},"type":"Dimmable light","name":"Veranda 1","modelid":"LWB010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":false,"proxy":false}},"uniqueid":"00:17:88:01:02:b0:0f:32-0b","swversion":"1.29.0_r21169","swconfigid":"C40F3F07","productid":"Philips-LWB010-1-A19DLv3"},"8":{"state":{"on":true,"bri":91,"hue":40414,"sat":65,"effect":"none","xy":[0.3236,0.3445],"ct":167,"alert":"none","colormode":"xy","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:35:32"},"type":"Extended color light","name":"Eethoek 2","modelid":"LCT010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":true,"proxy":true}},"uniqueid":"00:17:88:01:02:92:13:80-0b","swversion":"1.29.0_r21169","swconfigid":"6A139B19","productid":"Philips-LCT010-1-A19ECLv4"},"9":{"state":{"on":true,"bri":91,"hue":40411,"sat":65,"effect":"none","xy":[0.3236,0.3445],"ct":167,"alert":"none","colormode":"xy","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:34:46"},"type":"Extended color light","name":"Eethoek 1","modelid":"LCT010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":true,"proxy":true}},"uniqueid":"00:17:88:01:02:82:9a:c5-0b","swversion":"1.29.0_r21169","swconfigid":"6A139B19","productid":"Philips-LCT010-1-A19ECLv4"},"10":{"state":{"on":true,"bri":88,"alert":"none","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:35:07"},"type":"Dimmable light","name":"Overloop","modelid":"LWB010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":false,"proxy":false}},"uniqueid":"00:17:88:01:02:4a:17:7f-0b","swversion":"1.29.0_r21169","swconfigid":"C40F3F07","productid":"Philips-LWB010-1-A19DLv3"},"11":{"state":{"on":true,"bri":92,"hue":41103,"sat":58,"effect":"none","xy":[0.3296,0.3432],"ct":176,"alert":"none","colormode":"xy","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:34:35"},"type":"Extended color light","name":"Piano","modelid":"LCT012","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":true,"proxy":true}},"uniqueid":"00:17:88:01:02:85:44:11-0b","swversion":"1.29.0_r21169","swconfigid":"EA9279F9","productid":"Philips-LCT012-1-E14ECLv1"},"12":{"state":{"on":false,"bri":254,"hue":7688,"sat":199,"effect":"none","xy":[0.5014,0.4153],"ct":447,"alert":"none","colormode":"ct","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:34:38"},"type":"Extended color light","name":"Marieke","modelid":"LCT012","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":true,"proxy":true}},"uniqueid":"00:17:88:01:02:85:46:98-0b","swversion":"1.29.0_r21169","swconfigid":"EA9279F9","productid":"Philips-LCT012-1-E14ECLv1"},"13":{"state":{"on":true,"bri":90,"ct":269,"alert":"none","colormode":"ct","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:35:18"},"type":"Color temperature light","name":"Keuken","modelid":"LTW001","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":false,"proxy":false}},"uniqueid":"00:17:88:01:02:37:6c:45-0b","swversion":"5.105.0.21536"},"14":{"state":{"on":false,"bri":254,"alert":"none","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:34:51"},"type":"Dimmable light","name":"Lantaarnpaal 1","modelid":"LWB010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":false,"proxy":false}},"uniqueid":"00:17:88:01:02:b0:0f:41-0b","swversion":"1.29.0_r21169","swconfigid":"C40F3F07","productid":"Philips-LWB010-1-A19DLv3"},"15":{"state":{"on":false,"bri":254,"alert":"none","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:34:57"},"type":"Dimmable light","name":"Lantaarnpaal 2","modelid":"LWB010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":false,"proxy":false}},"uniqueid":"00:17:88:01:02:b1:76:d6-0b","swversion":"1.29.0_r21169","swconfigid":"C40F3F07","productid":"Philips-LWB010-1-A19DLv3"},"16":{"state":{"on":false,"bri":254,"alert":"none","mode":"homeautomation","reachable":true},"swupdate":{"state":"noupdates","lastinstall":"2017-11-14T19:35:02"},"type":"Dimmable light","name":"Lantaarnpaal 3","modelid":"LWB010","manufacturername":"Philips","capabilities":{"certified":true,"streaming":{"renderer":false,"proxy":false}},"uniqueid":"00:17:88:01:02:48:b2:c1-0b","swversion":"1.29.0_r21169","swconfigid":"C40F3F07","productid":"Philips-LWB010-1-A19DLv3"},"17":{"state":{"on":false,"bri":246,"hue":64131,"sat":125,"effect":"none","xy":[0.5133,0.3333],"ct":467,"alert":"none","colormode":"xy","mode":"ho

Code: Select all

   domoticz.log(domoticz.utils.fromJSON(item.data)) 


{["lights"]={["4"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=false, ["proxy"]=false}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:35:13", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="C40F3F07", ["type"]="Dimmable light", ["state"]={["on"]=false, ["mode"]="homeautomation", ["bri"]=51, ["reachable"]=true, ["alert"]="none"}, ["modelid"]="LWB010", ["productid"]="Philips-LWB010-1-A19DLv3", ["name"]="Veranda 2", ["uniqueid"]="00:17:88:01:02:b0:10:91-0b"}, ["10"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=false, ["proxy"]=false}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:35:07", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="C40F3F07", ["type"]="Dimmable light", ["state"]={["on"]=true, ["mode"]="homeautomation", ["bri"]=151, ["reachable"]=true, ["alert"]="none"}, ["modelid"]="LWB010", ["productid"]="Philips-LWB010-1-A19DLv3", ["name"]="Overloop", ["uniqueid"]="00:17:88:01:02:4a:17:7f-0b"}, ["17"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=true, ["proxy"]=true}}, ["swupdate"]={["lastinstall"]="2017-12-17T01:09:23", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="3416C2DD", ["type"]="Extended color light", ["state"]={["effect"]="none", ["sat"]=125, ["mode"]="homeautomation", ["ct"]=467, ["bri"]=246, ["alert"]="none", ["xy"]={0.5133, 0.3333}, ["hue"]=64131, ["reachable"]=true, ["colormode"]="xy", ["on"]=false}, ["modelid"]="LCT015", ["productid"]="Philips-LCT015-1-A19ECLv5", ["name"]="Naomi", ["uniqueid"]="00:17:88:01:03:fa:ec:12-0b"}, ["12"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=true, ["proxy"]=true}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:34:38", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="EA9279F9", ["type"]="Extended color light", ["state"]={["effect"]="none", ["sat"]=199, ["mode"]="homeautomation", ["ct"]=447, ["bri"]=254, ["alert"]="none", ["xy"]={0.5014, 0.4153}, ["hue"]=7688, ["reachable"]=true, ["colormode"]="ct", ["on"]=false}, ["modelid"]="LCT012", ["productid"]="Philips-LCT012-1-E14ECLv1", ["name"]="Marieke", ["uniqueid"]="00:17:88:01:02:85:46:98-0b"}, ["11"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=true, ["proxy"]=true}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:34:35", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="EA9279F9", ["type"]="Extended color light", ["state"]={["effect"]="none", ["sat"]=64, ["mode"]="homeautomation", ["ct"]=170, ["bri"]=150, ["alert"]="none", ["xy"]={0.3243, 0.3388}, ["hue"]=41225, ["reachable"]=true, ["colormode"]="xy", ["on"]=true}, ["modelid"]="LCT012", ["productid"]="Philips-LCT012-1-E14ECLv1", ["name"]="Piano", ["uniqueid"]="00:17:88:01:02:85:44:11-0b"}, ["14"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=false, ["proxy"]=false}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:34:51", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="C40F3F07", ["type"]="Dimmable light", ["state"]={["on"]=false, ["mode"]="homeautomation", ["bri"]=254, ["reachable"]=true, ["alert"]="none"}, ["modelid"]="LWB010", ["productid"]="Philips-LWB010-1-A19DLv3", ["name"]="Lantaarnpaal 1", ["uniqueid"]="00:17:88:01:02:b0:0f:41-0b"}, ["13"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=false, ["proxy"]=false}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:35:18", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="5.105.0.21536", ["type"]="Color temperature light", ["state"]={["on"]=true, ["mode"]="homeautomation", ["reachable"]=true, ["ct"]=254, ["bri"]=152, ["colormode"]="ct", ["alert"]="none"}, ["modelid"]="LTW001", ["name"]="Keuken", ["uniqueid"]="00:17:88:01:02:37:6c:45-0b"}, ["16"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=false, ["proxy"]=false}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:35:02", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="C40F3F07", ["type"]="Dimmable light", ["state"]={["on"]=false, ["mode"]="homeautomation", ["bri"]=254, ["reachable"]=true, ["alert"]="none"}, ["modelid"]="LWB010", ["productid"]="Philips-LWB010-1-A19DLv3", ["name"]="Lantaarnpaal 3", ["uniqueid"]="00:17:88:01:02:48:b2:c1-0b"}, ["9"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=true, ["proxy"]=true}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:34:46", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="6A139B19", ["type"]="Extended color light", ["state"]={["effect"]="none", ["sat"]=68, ["mode"]="homeautomation", ["ct"]=165, ["bri"]=152, ["alert"]="none", ["xy"]={0.3209, 0.34}, ["hue"]=40784, ["reachable"]=true, ["colormode"]="xy", ["on"]=true}, ["modelid"]="LCT010", ["productid"]="Philips-LCT010-1-A19ECLv4", ["name"]="Eethoek 1", ["uniqueid"]="00:17:88:01:02:82:9a:c5-0b"}, ["8"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=true, ["proxy"]=true}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:35:32", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="6A139B19", ["type"]="Extended color light", ["state"]={["effect"]="none", ["sat"]=68, ["mode"]="homeautomation", ["ct"]=165, ["bri"]=150, ["alert"]="none", ["xy"]={0.3209, 0.34}, ["hue"]=40785, ["reachable"]=true, ["colormode"]="xy", ["on"]=true}, ["modelid"]="LCT010", ["productid"]="Philips-LCT010-1-A19ECLv4", ["name"]="Eethoek 2", ["uniqueid"]="00:17:88:01:02:92:13:80-0b"}, ["7"]={["capabilities"]={["certified"]=true, ["streaming"]={["renderer"]=false, ["proxy"]=false}}, ["swupdate"]={["lastinstall"]="2017-11-14T19:34:32", ["state"]="noupdates"}, ["manufacturername"]="Philips", ["swversion"]="1.29.0_r21169", ["swconfigid"]="C40F3F07", ["type"]="Dimmable light", ["state"]={["on"]=false, ["mode"]="homeautoma

and

Code: Select all

 2018-02-09 17:01:01.489 dzVents: Error (2.4.1): An error occured when calling event handler Hue status
2018-02-09 17:01:01.489 dzVents: Error (2.4.1): ...omoticz/scripts/dzVents/generated_scripts/Hue status.lua:31: attempt to index field '?' (a nil value)


The json result code returns negative.

When I try the json calls:

Code: Select all

				url = 'http://192.168.1.105/api/xigTvboKxxxxXyp4gUzDLNKIXpUViF',
Hue
or

Code: Select all

			url = 'http://192.168.1.231:8080/json.htm?type=devices&rid=582',
KAKU temp sensor

The result on the firefox browser is different

dannybloe
Posts: 1481
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: HTTPRespons and json and Hue

Post by dannybloe » Friday 09 February 2018 18:17

Clearly domoticz.utils.fromJSON(item.data) is returning a proper Lua table. And the domoticz log doesn't print the entire data chunk as it is too large for the log. But I don't understand what you try to say in the last part of your post (the last three code blocks). And your json dump is totally different from your initial expected dump. So your info doesn't help me very much here.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Maxx
Posts: 18
Joined: Saturday 27 January 2018 21:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HTTPRespons and json and Hue

Post by Maxx » Friday 09 February 2018 19:47

Hello Danny,

I realise I was not clear.

Bottomline is that a json call to Hue returns isJson as false and when I do the json to a kaku temp device, it works.

While trying to look for the cause I noticed that the headers are different (doing the URL call in the Firefox browser):

For Hue

Code: Select all

Access-Control-Allow-Credentials	true
Access-Control-Allow-Headers	Content-Type
Access-Control-Allow-Methods	POST, GET, OPTIONS, PUT, DELETE, HEAD
Access-Control-Allow-Origin	*
Access-Control-Max-Age	3600
Cache-Control	no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection	close
Content-Type	application/json
Date	Fri, 09 Feb 2018 17:33:09 GMT
Expires	Mon, 1 Aug 2011 09:00:00 GMT
Pragma	no-cache
Server	nginx
For Kaku:

Code: Select all

Access-Control-Allow-Origin	*
Cache-Control	no-cache
Connection	Keep-Alive
Content-Encoding	gzip
Content-Length	635
Content-Type	application/json;charset=UTF-8
Keep-Alive	max=20, timeout=20
Pragma	no-cache
Also the "onbewerkte gegevens" in Firefox look different.


When I force the json to Hue from dzvents (bypassing the condition isJson ) it returns an error:

Code: Select all

2018-02-09 17:01:01.489 dzVents: Error (2.4.1): An error occured when calling event handler Hue status
2018-02-09 17:01:01.489 dzVents: Error (2.4.1): ...omoticz/scripts/dzVents/generated_scripts/Hue status.lua:31: attempt to index field '?' (a nil value)


Not sure if this points to the problem but I hope this makes my problem clearer

dannybloe
Posts: 1481
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: HTTPRespons and json and Hue

Post by dannybloe » Saturday 10 February 2018 14:50

Can you PM me your script code, both the http requests and both the response headers with the complete response data (copy it from Chrome's network tab).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

kimhav
Posts: 106
Joined: Tuesday 01 October 2013 8:31
Target OS: Raspberry Pi
Domoticz version: 4.10053
Location: Malmo, Sweden
Contact:

Re: HTTPRespons and json and Hue

Post by kimhav » Tuesday 14 August 2018 11:08

Just out of curiosity, as I was looking at the same thing here myself on how to handle the status when a Hue unit goes offline, will this script be included in future betas? Since it would be great to ensure that Domoticz keeps track whether a device is offline or not to block the user from turning on if it won't respond to it.
Member of Domoticz Sweden Telegram Group

Maxx
Posts: 18
Joined: Saturday 27 January 2018 21:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HTTPRespons and json and Hue

Post by Maxx » Thursday 16 August 2018 20:03

Hello,

I use the following script to get the status of my Hue lights:

Code: Select all

local light_ID = ''

return {
	on = {
		timer = {
			'every 5 minutes'
		},
		httpResponses = {
			'Light_Callback'
		}
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
		    domoticz.log('Timer event')
			domoticz.openURL({
				url = 'http://192.168.1.161/api/xigTvboK2fVo6lUViF/lights',
				method = 'GET',
				callback = 'Light_Callback', 
			})
		end

		if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
			if (item.statusCode == 200) and (item.isJSON) then
                    domoticz.log('JSON was detected')
                    for light_no=4,17,1 do 
                        light_ID = tostring(light_no)
                            local Bereikbaar = item.json[light_ID]['state']['reachable']
                            local Naam = item.json[light_ID]['name']
                            if Bereikbaar == false then
                                domoticz.log('---------Lamp ' .. Naam .. ' onbereikbaar---------')
                            elseif Bereikbaar == true then
                               domoticz.log('++++++++Lamp ' .. Naam .. ' bereikbaar+++++++++')                          
                            end
                    end
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end
	    end
	end
}
This works fine and you can use the output to feedback the real status to Domoticz, I didn't try this yet.

User avatar
Ascari
Posts: 36
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: Belgium
Contact:

Re: HTTPRespons and json and Hue

Post by Ascari » Wednesday 19 September 2018 12:18

@Maxx

I try to use your scrip but it's not working for me

IsJson = false

Can you help me ?

User avatar
waaren
Posts: 1212
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: HTTPRespons and json and Hue

Post by waaren » Wednesday 19 September 2018 12:59

Ascari wrote:
Wednesday 19 September 2018 12:18
I try to use your scrip but it's not working for me
IsJson = false
Can you help me ?
What do you see if you enter http://192.168.1.161/api/xigTvboK2fVo6lUViF/lights in your browser ?
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

User avatar
Ascari
Posts: 36
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: Belgium
Contact:

Re: HTTPRespons and json and Hue

Post by Ascari » Wednesday 19 September 2018 13:21

I see :

Code: Select all

{"1":{"state":{"on":true,"bri":20,"hue":14910,"sat":144,"effect":"none","xy":[0.4596,0.4105],"ct":369,"alert":"none","colormode":"ct","reachable":false},"type":"Extended color light","name":"Living1","modelid":"LCT001","manufacturername":"Philips","uniqueid":"00:17:88:01:00:d3:3e:01-0b","swversion":"5.23.1.13452"},"2":{"state":{"on":true,"bri":20,"hue":14910,"sat":144,"effect":"none","xy":[0.4596,0.4105],"ct":369,"alert":"none","colormode":"ct","reachable":false},"type":"Extended color light","name":"Living2","modelid":"LCT001","manufacturername":"Philips","uniqueid":"00:17:88:01:00:d3:39:8f-0b","swversion":"5.23.1.13452"},"3":{"state":{"on":true,"bri":20,"hue":14910,"sat":144,"effect":"none","xy":[0.4596,0.4105],"ct":369,"alert":"none","colormode":"ct","reachable":false},"type":"Extended color light","name":"Living3","modelid":"LCT001","manufacturername":"Philips","uniqueid":"00:17:88:01:00:ff:c0:4f-0b","swversion":"5.23.1.13452"},"4":{"state":{"on":true,"bri":254,"hue":14910,"sat":144,"effect":"none","xy":[0.4596,0.4105],"ct":369,"alert":"none","colormode":"ct","reachable":false},"type":"Extended color light","name":"Salon","modelid":"LCT001","manufacturername":"Philips","uniqueid":"00:17:88:01:00:d3:38:e5-0b","swversion":"5.23.1.13452"},"5":{"state":{"on":false,"bri":20,"hue":6682,"sat":254,"effect":"none","xy":[0.6290,0.3642],"alert":"none","colormode":"hs","reachable":true},"type":"Color light","name":"Meuble","modelid":"LST001","manufacturername":"Philips","uniqueid":"00:17:88:01:00:cd:a8:11-0b","swversion":"5.23.1.13452"},"6":{"state":{"on":false,"bri":254,"alert":"none","reachable":true},"type":"Dimmable light","name":"Boule","modelid":"LWB004","manufacturername":"Philips","uniqueid":"00:17:88:01:00:de:54:0c-0b","swversion":"5.38.2.19136"},"7":{"state":{"on":true,"bri":254,"alert":"none","reachable":false},"type":"Dimmable light","name":"Milo","modelid":"LWB004","manufacturername":"Philips","uniqueid":"00:17:88:01:00:de:48:f1-0b","swversion":"5.38.2.19136"},"8":{"state":{"on":false,"bri":186,"alert":"none","reachable":false},"type":"Dimmable light","name":"Chambre","modelid":"LWB004","manufacturername":"Philips","uniqueid":"00:17:88:01:00:de:57:6d-0b","swversion":"5.38.2.19136"}}
But in the logs Domoticz

["statusCode"]=200,
["isJSON"]=false,

Maxx
Posts: 18
Joined: Saturday 27 January 2018 21:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: HTTPRespons and json and Hue

Post by Maxx » Wednesday 19 September 2018 17:12

Looking at the respons in your browser you are using your own IP adres and username, which is good

Can you check the header of the json, it should be something like:

Code: Select all

Access-Control-Allow-Credentials	true
Access-Control-Allow-Headers	Content-Type
Access-Control-Allow-Methods	POST, GET, OPTIONS, PUT, DELETE, HEAD
Access-Control-Allow-Origin	*
Access-Control-Max-Age	3600
Cache-Control	no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection	close
Content-Type	application/json
Date	Wed, 19 Sep 2018 15:06:06 GMT
Expires	Mon, 1 Aug 2011 09:00:00 GMT
Pragma	no-cache
Server	nginx

User avatar
Ascari
Posts: 36
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: Belgium
Contact:

Re: HTTPRespons and json and Hue

Post by Ascari » Wednesday 19 September 2018 22:04

Strange because Firefoc say it's JSON

Access-Control-Allow-Credentials true
Access-Control-Allow-Headers Content-Type
Access-Control-Allow-Methods POST, GET, OPTIONS, PUT, DELETE, HEAD
Access-Control-Allow-Origin *
Access-Control-Max-Age 3600
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection close
Content-type application/json
Expires Mon, 1 Aug 2011 09:00:00 GMT
Pragma no-cache
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Connection keep-alive
Host 192.168.0.243
Upgrade-Insecure-Requests 1
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0

User avatar
waaren
Posts: 1212
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: HTTPRespons and json and Hue

Post by waaren » Thursday 20 September 2018 1:10

Ascari wrote:
Wednesday 19 September 2018 12:18
...
IsJson = false
...
Can you change the line

Code: Select all

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

Code: Select all

if item.ok and item.isJSON then
and check if that makes any difference? It worked for me but I have to admit that I have not figured out why :D
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

User avatar
Ascari
Posts: 36
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: Belgium
Contact:

Re: HTTPRespons and json and Hue

Post by Ascari » Thursday 20 September 2018 9:29

I have an error also

2018-09-20 09:29:00.387 Status: dzVents: Error (2.4.7): There was a problem handling the request
2018-09-20 09:29:00.387 Status: dzVents: Error (2.4.7): {["trigger"]="Light_Callback", ["callback"]="Light_Callback", ["isSecurity"]=false, ["_contentType"]="", ["isDevice"]=false, ["ok"]=true, ["isTimer"]=false, ["isGroup"]=false, ["statusCode"]=200, ["isVariable"]=false, ["baseType"]="httpResponse", ["headers"]={["Cache-Control"]="no-store, no-cache, must-revalidate, post-check=0, pre-check=0", ["Access-Control-Allow-Headers"]="Content-Type", ["Content-type"]="application/json", ["Access-Control-Max-Age"]="3600", ["Access-Control-Allow-Methods"]="POST, GET, OPTIONS, PUT, DELETE, HEAD", ["Connection"]="close", ["Expires"]="Mon, 1 Aug 2011 09:00:00 GMT", ["Pragma"]="no-cache", ["Access-Control-Allow-Credentials"]="true", ["Access-Control-Allow-Origin"]="*"}, ["isScene"]=false, ["isHTTPResponse"]=true, ["data"]="{"state":{"on":true,"bri":8,"hue":14910,"sat":144,"effect":"none","xy":[0.4596,0.4105],"ct":369,"alert":"none","colormode":"ct","reachable":false},"type":"Extended color light","name":"Living1","modelid":"LCT001","manufacturername":"Philips","uniqueid":"00:17:88:01:00:d3:3e:01-0b","swversion":"5.23.1.13452"}", ["isJSON"]=false}
2018-09-20 09:29:00.387 Status: dzVents: Info: ------ Finished 00YT

User avatar
waaren
Posts: 1212
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: HTTPRespons and json and Hue

Post by waaren » Thursday 20 September 2018 11:00

Ascari wrote:
Thursday 20 September 2018 9:29
I have an error also

2018-09-20 09:29:00.387 Status: dzVents: Error (2.4.7): There was a problem handling the request
......
You could try with a brute force approach..

replace the code lines

Code: Select all

domoticz.log('HTTP reponse was ok')
if (item.statusCode == 200) and (item.isJSON) then
    domoticz.log('JSON was detected')
with

Code: Select all

domoticz.log('HTTP reponse statusCode: '  .. item.statusCode )
if item.ok then
  item.json = domoticz.utils.fromJSON(item.data)  -- Forcing dzVents to handle data as JSON
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

User avatar
Ascari
Posts: 36
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: Belgium
Contact:

Re: HTTPRespons and json and Hue

Post by Ascari » Thursday 20 September 2018 12:06

Not working error also.

I'm working to a another script (parcer) and it's work to receive the reachable status.
Now I look to do a loop and clean code.
When ok I send the code

User avatar
Ascari
Posts: 36
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: Belgium
Contact:

Re: HTTPRespons and json and Hue

Post by Ascari » Saturday 22 September 2018 10:27

Hello
I try to make the same with Lua code.
I can parse the Json but I again a issue with the "While" only the last true condition update the hueStatus (strange).

When I find a solution for this, I want to add a check (if the statut is not already on OFF, to avaid udpate useless)

Code: Select all

commandArray = {}
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
-- Tableau Hue/Domotiz Exemple Idlamp Hue1 = idz108
lamp = {1,2,3,4,5,6,7,8} --Put your Hue lamp ID here
device = {108,109,110,111,112,113,114,115} --Put your Domotiz ID here
nblamp = 8 --Put here nbr lamp you want check

i = 1
while i <= nblamp 
do
	huelightid=lamp[i]
	domohueid=device[i]
	idjson = tostring(huelightid)
	urlid = tostring(domohueid) 
			--  API call
	local config=assert(io.popen('curl "http://IPHue/api/UserHue/lights/'..idjson..'"')) -- change Iphue and login hue
	local Stringjson = config:read('*all')
	config:close()
	local jsonData = json:decode(Stringjson)
	lampe = jsonData.name
	reachable = jsonData.state.reachable
			-- print (Stringjson)  -- debug json
	reachable = tostring(reachable)
			-- print('Hue Reachable Log: '..reachable..lampe) -- Test value for Debuging.
	if reachable == "false"
		then
		commandArray['OpenURL']='http://domoticzip:port/json.htm?type=command&param=switchlight&idx='..urlid..'&switchcmd=Off' -- Changedomoticzip and port
		print ('url : '..urlid) -- Log Debug Help
	end
i = i + 1
end
return commandArray
Code ok here with the help of Waaren :

TODO : CHECK (if the statut is not already on OFF, to avoid udpate useless)

Code: Select all

commandArray = {}

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
-- Tableau Hue/Domotiz Exemple Idlamp Hue1 = idz108
lamp = {1,2,3,4,5,6,7,8} --Put your Hue lamp ID here
device = {108,109,110,111,112,113,114,115} --Put your Domotiz ID here
nblamp = 8 --Put here nbr lamp you want check
i = 1
while i <= nblamp 
do
	huelightid=lamp[i]
	domohueid=device[i]
	idjson = tostring(huelightid)
	urlid = tostring(domohueid) 
			--  API call
	local config=assert(io.popen('curl "http://iphue/api/userloginhue/lights/'..idjson..'"'))
	local Stringjson = config:read('*all')
	config:close()
	local jsonData = json:decode(Stringjson)
	lampe = jsonData.name
	reachable = jsonData.state.reachable
			-- print (Stringjson)  -- debug json
	reachable = tostring(reachable)
			-- print('Hue Reachable Log: '..reachable..lampe) -- Test value for Debuging.
			
	if reachable == "false"
		then
        --print ('url : '..urlid) -- Log Debug Help
		-- Thx Waaren 
        commandArray[#commandArray+1] = {['OpenURL']='http://ipdomo:port/json.htm?type=command&param=switchlight&idx='..urlid..'&switchcmd=Off'}  
	end
i = i + 1
end
return commandArray

User avatar
Ascari
Posts: 36
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: Belgium
Contact:

Re: HTTPRespons and json and Hue

Post by Ascari » Saturday 22 September 2018 13:55

I have finish my code LUA and thx Waaren for help.
Here the code.

To use you need to adpat
IP/port Domoticz
IP Hue bridge and login
the two table (lamp & device)


Hope can help other user.

Code: Select all

-- Script : Auto OFF Hue if Unreachable
-- Auteur : Vincent Arancio
-- Date : 22/09/2018
-- Version : Preproduction Error
-- TODO : Set HavetimeOut var in true (Blink red on dashboard)

commandArray = {}

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
-- Tableau Hue/Domotiz Exemple Idlamp Hue1 = idz108
lamp = {1,2,3,4,5,6,7,8} --Put your Hue lamp ID here
device = {108,109,110,111,112,113,114,115} --Put your Domotiz ID here
nblamp = 8 --Put here nbr lamp you want check
i = 1
while i <= nblamp 
do
	huelightid=lamp[i]
	domohueid=device[i]
	idjson = tostring(huelightid)
	urlid = tostring(domohueid) 
	--  API call
	local config=assert(io.popen('curl "http://iphue/api/HueLoginDev/lights/'..idjson..'"'))
	local Stringjson = config:read('*all')
	config:close()
	local jsonData = json:decode(Stringjson)
	lampe = jsonData.name
	reachable = jsonData.state.reachable
			-- print (Stringjson)  -- debug json
	reachable = tostring(reachable)
	-- print('Hue Reachable Log: '..reachable..lampe) -- Test value for Debuging.			
	if reachable == "false"
		then
		local configs=assert(io.popen('curl "http://ipdomo:port/json.htm?type=devices&rid='..urlid..'"'))
		local Stringjson2 = configs:read('*all')
		configs:close()
		local DeviceData = json:decode(Stringjson2)
		--print ('ici encore1'..Stringjson2) -- Debug log
		Statusoff = DeviceData.result[1].Status
		-- print ('ici encore'..Statusoff) -- Debug log
		if Statusoff ~= "Off"
			then
			--print ('url : '..urlid) -- Log Debug Help
			commandArray[#commandArray+1] = {['OpenURL']='http://ipdomo:port/json.htm?type=command&param=switchlight&idx='..urlid..'&switchcmd=Off'}  
			end
	end
i = i + 1
end
return commandArray

Post Reply

Who is online

Users browsing this forum: waaren and 1 guest