Page 1 of 1

Getting stuck, to get state information out of json (hue)

Posted: Saturday 19 January 2019 15:37
by hoeby
I got stuck with my dzvents scripts. I hope soomebody can help me.
Did a search on "hue" here of the forum, but the word is to small. With manual searching didn't found the answer.

What do i like to do: Read out the status of one single hue devices.

When running "http://192.168.xxx.xxx/api/xxxxxxxxxxxx ... x/lights/5" i get nice information, where i can find the imformation i am searching for, but how to get it out of there.

This is the information i get:

Code: Select all

{"state":{"on":false,"bri":203,"hue":7674,"sat":199,"effect":"none","xy":[0.5017,0.4151],"ct":443,"alert":"none","colormode":"xy","mode":"homeautomation","reachable":true},"swupdate":{"state":"readytoinstall","lastinstall":"2017-11-25T18:20:27"},"type":"Extended color light","name":"Keukenkast","modelid":"LLC020","manufacturername":"Philips","productname":"Hue go","capabilities":{"certified":true,"control":{"mindimlevel":40,"maxlumen":300,"colorgamuttype":"C","colorgamut":[[0.6915,0.3083],[0.1700,0.7000],[0.1532,0.0475]],"ct":{"min":153,"max":500}},"streaming":{"renderer":true,"proxy":true}},"config":{"archetype":"huego","function":"decorative","direction":"omnidirectional"},"uniqueid":"xx:xx:xx:xx:xx:xx:xx:xx-xx","swversion":"5.105.0.21169"}
What is my dzvents scripts:

Code: Select all

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

		if (item.isTimer) then
		    domoticz.log('Timer event')
			domoticz.openURL({
				url = 'http://192.168.xxx.xxx/api/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/lights/5',
				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')
                              local State = item.json['name']
                                domoticz.log('--------- ' .. State .. '---------')
                            end
                  end
	end
}
When running that script, i get the "name" as response, which is "keukenkast", that works.
But i would like to know how i can get the information from {"state":{"on"}}, which should be TRUE or FALSE
Does somebody know how to get this info out of the response?

Re: Getting stuck, to get state information out of json (hue)

Posted: Saturday 19 January 2019 19:27
by waaren
hoeby wrote: Saturday 19 January 2019 15:37 I got stuck with my dzvents scripts. I hope soomebody can help me.
What do i like to do: Read out the status of one single hue devices.

But i would like to know how i can get the information from {"state":{"on"}}, which should be TRUE or FALSE
Does somebody know how to get this info out of the response?
Try this

Code: Select all

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

	
		if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
			if (item.statusCode == 200) and (item.isJSON) then
                    domoticz.log('JSON was detected',domoticz.LOG_FORCE)
                    local State = item.json['name']
                domoticz.log('--------- ' .. State .. '---------',domoticz.LOG_FORCE)
                -- domoticz.log(item.data,domoticz.LOG_FORCE)
                domoticz.log(item.json.name .. " is on ==>> " .. tostring(item.json.state.on),domoticz.LOG_FORCE)
            end
        else
		    domoticz.log('Timer event')
			domoticz.openURL({
				url = 'http://192.168.192.72/api/xxxxx/lights/5',
				method = 'GET',
				callback = 'Light_Callback', 
			})
		end
    end
}

Re: Getting stuck, to get state information out of json (hue)

Posted: Saturday 19 January 2019 22:36
by hoeby
@Waaren,

Thanks with your help, it works beautiful
Added a command to switch a dummy, that also works.

thank you

Re: Getting stuck, to get state information out of json (hue)

Posted: Sunday 20 January 2019 7:42
by hoeby
Maybe you could help me one more time ? :oops:

The basic script you posted, that works.
But i extended it and that i can't get it to work.
When i do only if onstate == "false" then that works.
But when i do an if whit and that doesn't work. I want it to work like if (onstate == "false") and (device.state == On') then'
What i do and try. Everytime it give Nihil value. Could you help me what i am doing wrong?

Is there also a possiblity to use the script for more Hue ID's in one dzvents script?
But it than also has to answer to different IDX numbers.
In lua i used while and do, but in dzvents it works different.
Copied this out of an other thread;

Code: Select all

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) 

This is de code which Waaren posted, but with the extended that i made, where the and function doesn't work

Code: Select all

return {
	on = {
		timer = { "every 1 minutes" },
		httpResponses = {'Light_Callback'}
		devices = {'HueAchterdeurLamp'}
	},
	execute = function(domoticz, item, device)

	
		if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse was ok')
			if (item.statusCode == 200) and (item.isJSON) then
                    domoticz.log('JSON was detected',domoticz.LOG_FORCE)
                    local State = item.json['name']
                domoticz.log('--------- ' .. State .. '---------',domoticz.LOG_FORCE)
                -- domoticz.log(item.data,domoticz.LOG_FORCE)
                domoticz.log(item.json.name .. " is on ==>> " .. tostring(item.json.state.on),domoticz.LOG_FORCE)
                    onstates = item.json.state.on
		    onstate = tostring(onstates)
	            print('Hue On/Off Log: '.. onstate) -- Test value for Debuging.
	             if (onstate == "false") and (device.state == On') then
		          os.execute ('curl "http://xxx.xxx.xxx.xx:xxxx/json.htm?type=command&param=switchlight&idx=312&switchcmd=Off"')
                   end
             end
        else
		    domoticz.log('Timer event')
			domoticz.openURL({
				url = 'http://xxx.xxx.xxx.xx/api/xxxxxxxxxxxxxxxxxxxxxx/lights/3',
				method = 'GET',
				callback = 'Light_Callback', 
			})
		end
    end
}

Re: Getting stuck, to get state information out of json (hue)

Posted: Sunday 20 January 2019 8:45
by waaren
hoeby wrote: Sunday 20 January 2019 7:42 The basic script you posted, that works.
But i extended it and that i can't get it to work.
Check this

Code: Select all

return {
    on = {
            timer           = { "every 1 minutes" },
            devices         = ( "HueAchterdeurLamp"),
            httpResponses   = { "Light_Callback"}
    },
    
    
    logging =   {
                    level           =   domoticz.LOG_DEBUG,
                    marker          =   hueStates
                },

    execute = function(dz, item)
        
        local light = dz.devices("HueAchterdeurLamp")
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        if item.isHTTPResponse then
            logWrite("HTTPReponse found")
            if item.ok then
                logWrite("item OK")
                local state     = item.json.name
                local onstate   = item.json.state.on
                logWrite(state .. " is on ==>> " .. tostring(onstate),dz.LOG_FORCE)
                 if onstate and light.state == "Off" then
                    light.switchOn().silent()
                 elseif not onstate and light.state == "On" then
                    light.switchOff().silent()
                 end
            else
                logWrite("Invalid response")
            end
        else
            logWrite("Timer or Device triggered event")
            dz.openURL(
                            {
                                url         = "http://xxx.xxx.xxx.xx/api/xxxxxxxxxxxxxxxxxxxxxx/lights/3",
                                method      = "GET",
                                callback    = "Light_Callback", 
                            })
        end
    end
}
In lua i used while and do, but in dzvents it works different.
dzVents is Lua
Is there also a possiblity to use the script for more Hue ID's in one dzvents script?
Have a look at this post
Or even better install latest Beta where the inconsistent states between hue and domoticz are solved

Re: Getting stuck, to get state information out of json (hue)

Posted: Sunday 20 January 2019 13:35
by hoeby
Thanks Waaren,
I worked exactly where i was looking for.

Everytime i think i am on the correct way to write the script. Sometime i ask for help, like this one, and everytime the working script looks way different then i started with. Do you have some information where to look for correct information how i can fix this myself. Or is this just experiance, which i am missing?

I will look at that post, thanks.
Installing latest Beta, don't know if i am doing right with that. In the past i had troubles with a beta. The stable version 4.9700 works beautifull for me. Sometimes it needs works arounds, but i can life with that.
The troubles where at the beginning of domoticz use (for me). Of course i forgot to make a backup. Needed to learn it the hard way.
Now these days, i make backups before i start changing things

Re: Getting stuck, to get state information out of json (hue)

Posted: Sunday 20 January 2019 16:03
by waaren
hoeby wrote: Sunday 20 January 2019 13:35 Everytime i think i am on the correct way to write the script. Sometime i ask for help, like this one, and everytime the working script looks way different then i started with. Do you have some information where to look for correct information how i can fix this myself. Or is this just experiance, which i am missing?
Part of it is just a different style of coding, plus experience and studying Lua code of other forum members. You can also find a lot of interesting Lua code in the dzVents/runtime directory and below.