Page 1 of 1

Include node trough json?

Posted: Sunday 07 May 2017 23:17
by johos
Hi,

Is it possible to start node inclusion trough a json call?

Re: Include node trough json?

Posted: Monday 08 May 2017 8:41
by Egregius
Ofcourse. Everything you can do in the web interface is done by json calls.

Start inclusion (idx is the idx of your zwave controller):

Code: Select all

json.htm?type=command&param=zwaveinclude&idx=3&secure=false
Check if node is included:

Code: Select all

json.htm?type=command&param=zwaveisnodeincluded&idx=3
Cancel the inclusion:

Code: Select all

json.htm?type=command&param=zwavecancel&idx=3
Tip: All this is found using the developer view of a good browser.

Re: Include node trough json?

Posted: Monday 08 May 2017 23:08
by johos
Many thanks for your reply Egregius.

Seems though as if domoticz hangs when i try it, which browser would you recommend to be able to view from developer view?

Re: Include node trough json?

Posted: Tuesday 09 May 2017 18:00
by asjmcguire
johos wrote:Many thanks for your reply Egregius.

Seems though as if domoticz hangs when i try it, which browser would you recommend to be able to view from developer view?
They all pretty much offer this now - I use Chrome, but basically anything that lets you see the actual network requests being made.
So you load Domoticz web page, open developer tools, and then click on the stuff you want to control and monitor what network requests are made. In this case you would navigate to the hardware tab, choose the ZWave hardware, and then start include mode - and check what network request that generates, and then while the dialog is open saying it is waiting for you to include a node - the network requests will continuously be made to the URL the web interface uses to check if the node was included. You can verify what data Domoticz returns when a node is successfully included.

Re: Include node trough json?

Posted: Tuesday 09 May 2017 20:24
by johos
Thank you for your reply asjmcguire!
i'll try it out.

when i try using the json command "json.htm?type=command&param=zwaveinclude&idx=1&secure=false" my domoticz becomes unresponsive, is there something i'm missing?

Re: Include node trough json?

Posted: Friday 12 May 2017 19:57
by asjmcguire
johos wrote:Thank you for your reply asjmcguire!
i'll try it out.

when i try using the json command "json.htm?type=command&param=zwaveinclude&idx=1&secure=false" my domoticz becomes unresponsive, is there something i'm missing?
Well the first question to ask - is - is your ZWave stick actually listed as idx 1 in the list of hardware?
The second question is - when you say it is unresponsive - does it still respond to (for example) :
json.htm?type=devices&filter=all

I notice when I do certain ZWave tasks - like heal the network, that my CPU use jumps to 100%

You should check if json.htm?type=command&param=zwaveisnodeincluded&idx=1 returns anything after you have started the include process. Domoticz seems to call this every second once it starts the include.

json.htm?type=command&param=zwavecancel&idx=1 is called when you abort the process.

Re: Include node trough json?

Posted: Friday 12 May 2017 22:21
by johos
asjmcguire,

I was so certain that the controller was idx1 but actually it was idx2

so problem solved, thank you for excellent support!!

BTW, you wouldn't happen to know if it's possible to auto assign devices after node inclusion?

Re: Include node trough json?

Posted: Friday 12 May 2017 22:36
by Egregius
What do you mean with auto assign?

Re: Include node trough json?

Posted: Sunday 14 May 2017 3:47
by asjmcguire
Egregius wrote:What do you mean with auto assign?
I expect he means that after you add a node through inclusion in ZWave, it's still not actually available to Domoticz until you have go to settings, clicked on Allow new hardware for 5 minutes, and then gone back to the ZWave screen, into the OZW Control Panel and actually changed the status of the device - at which point it will show up as a device in the Unused devices in Domoticz.

It's a pain with stuff that monitors energy because quite often the Allow new hardware setting has past the 5 minute timer and shut off and so you miss the ZWave message with the data about energy use.

In answer to the question - yes I expect it is possible.
Here is how I would do it:

After the node has been included (ie - json.htm?type=command&param=zwaveisnodeincluded&idx=2 says True instead of false.)
I would then query

Code: Select all

json.htm?type=devices&filter=all&used=false&order=id
and grab the last IDX number. I would then GET

Code: Select all

json.htm?type=command&param=allownewhardware&timeout=5
(changing the 5 to another number might work, I haven't tested).

You then query

Code: Select all

json.htm?type=devices&filter=all&used=false&order=id
again in a loop checking for whether any new IDXs have appeared since the one you stored in your code before you asked the system to allow new hardware.

I'd probably store a variable ($count = 1) at this point and every time a new IDX appears in the list over the next 5 minutes I would request:

Code: Select all

json.htm?type=setused&idx=<NEWIDX>&name=AutoAdd_{$count}&used=true
which would automatically add the new device with the name AutoAdd_1 and then increment $count ( $count++; ) ready to do it again.

You'd need to keep checking on

Code: Select all

/json.htm?type=settings
to make sure the field AcceptNewHardware still equals 1 and then quit the loop looking for new devices when the value changes back to 0.

So this is the flow (untested and possibly containing parsing errors or infinite loops):

Code: Select all

<?php
$domBase = "192.168.1.6:8080"; //IP and Port

// This is roughly how to add new nodes:
$result = domZWaveInclude(2); //your ZWave Hardware IDX, default 120 second timeout
if ($result) {
	$devs = domZWaveAddNode(); //default 300 second timeout, 1 device limit
	print_r($devs);
}

function domURLHelper($url) {
global $domBase;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://{$domBase}{$url}/json.htm?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 200) {
	if (stripos($info['content_type'],"application/json") !== false) {
		return json_decode($ret,true);
	}
	return $ret;
}
}

function domZWaveInclude($zwaveIDX,$timeout = 120) {
$included = 0;
$count = 0;
domURLHelper("command&param=zwaveinclude&idx={$zwaveIDX}&secure=false");
sleep(1);
while (!$included) {
	$ret = null;
	$ret = domURLHelper("command&param=zwaveisnodeincluded&idx={$zwaveIDX}");
	if ($ret['result']) {
		// a node was included
		$included = 1;
		break;
	}
	$count++;
	if ($count >= $timeout) { break; }
	sleep(1);
}

if (!$included) { return 0; }
return 1;
}

function domZWaveAddNode($timeout = 300, $numdevs = 1) {
$devcount = 0;
$timecount = 0;
$devs = array();
$ret = domURLHelper("type=devices&filter=all&used=false&order=id");
$last = array_reverse($ret['result']);
$last_idx = $last['idx'];
domAllowHardware();
while($timecount < $timeout) {
	$ret = null;
	$ret = domURLHelper("type=settings");
	if (!$ret['AcceptNewHardware']) { break; }
	$ret = null;
	$ret = domURLHelper("type=devices&filter=all&used=false&order=id");
	$last = null;
	$last = array_reverse($ret['result']);
	$t = $last['idx'];
	if ($t !== $last_idx) {
		$devcount++;
		domURLHelper("type=setused&idx={$t}&name=AutoAdd_{$devcount}&used=true");
		$devs[] = $t;
		if ($devcount == $numdevs || $timecount >= $timeout) {
			break;
		}
	}
	$timecount++;
	sleep(1);
}
return $devs;
}

function domAllowHardware($timeout = 5) {
domURLHelper("type=command&param=allownewhardware&timeout={$timeout}");
}
?>

Re: Include node trough json?

Posted: Sunday 14 May 2017 15:56
by asjmcguire
By the way - it's just occurred to me -

Hitting the acceptnewhardware URL mentioned above and putting something like 15 in the timeout, BEFORE going through the ZWave include process, would probably mean Domoticz would automatically add any nodes you include until the timer runs out.