Page 1 of 1

dzVents: Interact with all switches and sensors using JSON with an optional delay argument

Posted: Tuesday 04 April 2017 10:07
by BakSeeDaa
This is a primarily a question for the author @dannybloe

Wouldn't it be nice to incorporate the following into Domoticz.lua ?

Some devices like the RGBW device seems to work best if controlled by API/Json URL commands. The small function below would help doing those commands and it also allows a delay (seconds).

Code: Select all

	-- Interact with all switches and sensors using JSON with an optional delay argument
	function self.jsonAPI(apiCall, delay)
		delay = delay or 0
		local url = 'http://'..self.settings['Domoticz ip']..':'..self.settings['Domoticz port']..'/json.htm?'
		if delay == 0 then
			self.sendCommand('OpenURL', url..apiCall)
		else
			os.execute('(sleep '..delay..';curl -s "'..url..apiCall..'" > /dev/null)&')
		end
	end
I can make a pull request if you think this is useful ...

For controlling my RGBW ligt, it's essential for me. A pause of a few seconds seems needed after turning of a animation program and switching to a fixed color.

Re: dzVents: Interact with all switches and sensors using JSON with an optional delay argument

Posted: Tuesday 04 April 2017 10:17
by BakSeeDaa
Another useful thing is if You would like to trigger on the text change of a virtual text device. If You are using the traditional commandarray to change the text device, the change won't be triggered by scripts. However if You use a API/Json URL command changes of the text device can be triggered in scripts.

I use the code below:

Code: Select all

	function self.urlencode(str)
		if (str) then
			str = string.gsub (str, '\n', '\r\n')
			str = string.gsub (str, '([^%w ])',
			function (c) return string.format ('%%%02X', string.byte(c)) end)
			str = string.gsub (str, ' ', '+')
		end
		return str
	end

	-- Setting text devices the "Normal" way doesn't cause changes to be triggered.
	function self.setTextDevice(idx, sValue)
		self.jsonAPI('type=command&param=udevice&idx='..idx.."&nvalue=0&svalue="..self.urlencode(sValue))
	end
Just some ideas ;) ;)

Cheers!

Re: dzVents: Interact with all switches and sensors using JSON with an optional delay argument

Posted: Wednesday 05 April 2017 8:28
by dannybloe
Without looking at my code I believe there are already some devices in dzVents that work like this. Can you put a ticket in the tracker? In the coming weeks I will be continuing fixing/adding stuff to dzVents.

Re: dzVents: Interact with all switches and sensors using JSON with an optional delay argument

Posted: Wednesday 05 April 2017 10:09
by digimate
dannybloe wrote:In the coming weeks I will be continuing fixing/adding stuff to dzVents.
Thanks in advance Danny! Great job your dzVents!!