Page 2 of 2

Re: Wrong zwave switch level displayed On, 255 %

Posted: Thursday 10 December 2015 12:06
by Egregius
As shown in http://www.domoticz.com/forum/viewtopic ... =40#p54276

Create a file for each zwave device you want to update, ex refresh10.php
make it executable with sudo chmod +x refresh10.php
in the command on and off actions off domoticz add script:///path/to/script/refresh10.php

Code: Select all

#!/usr/bin/php
<?php
$domoticzurl='http://ip:port/';
file_get_contents($domoticzurl.'json.htm?type=openzwavenodes&idx=5'); //Change idx to idx of zwave hardware
RefreshZwave(10); //Updates zwave device with zwave ID 10. 

function RefreshZwave($node) {
	global $domoticzurl;
	$zwaveurl=$domoticzurl.'ozwcp/refreshpost.html';
	$zwavedata=array('fun'=>'racp','node'=>$node);
	$zwaveoptions = array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query($zwavedata),),);
	$zwavecontext=stream_context_create($zwaveoptions);
	for ($k=1;$k<=5;$k++){sleep(2);$result=file_get_contents($zwaveurl,false,$zwavecontext);if($result=='OK') break;}
}

Re: Wrong zwave switch level displayed On, 255 %

Posted: Thursday 10 December 2015 12:42
by warp
a big thank you, what I was missing it was only:
the #!/usr/bin/php in the script
and the script:///path/to/script/refresh10.php in the actions.

Re: Wrong zwave switch level displayed On, 255 %

Posted: Thursday 10 December 2015 13:38
by Egregius
You only need the #!/usr/bin/php if you want to run the script without curl or webserver.

Re: Wrong zwave switch level displayed On, 255 %

Posted: Monday 14 December 2015 16:28
by warp
Hello @Egregius,
This does not work, here's what I have :
Capture.PNG
Capture.PNG (3.78 KiB) Viewed 1969 times
Capture2.PNG
Capture2.PNG (14.71 KiB) Viewed 1969 times
Capture3.PNG
Capture3.PNG (73.83 KiB) Viewed 1969 times
/home/pi/refreshzwave.php

Code: Select all

#!/usr/lib/cgi-bin/php
<?php 
file_get_contents('http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3'); //Change IDX to IDX of your zwave controller
function RefreshZwave($node) {
	$zwaveurl='http://127.0.0.1:8080/ozwcp/refreshpost.html';
	$zwavedata=array('fun'=>'racp','node'=>$node);
	$zwaveoptions = array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query($zwavedata),),);
	$zwavecontext=stream_context_create($zwaveoptions);
	for ($k=1;$k<=5;$k++){sleep(2);$result=file_get_contents($zwaveurl,false,$zwavecontext);if($result=='OK') break;}
}
RefreshZwave(100); //Change NodeID to NodeID of your zwave devices
RefreshZwave(101); //Change NodeID to NodeID of your zwave devices
RefreshZwave(102); //Change NodeID to NodeID of your zwave devices

What is not good in all this ? I do not understand why it does not work !!

Re: Wrong zwave switch level displayed On, 255 %

Posted: Monday 14 December 2015 18:34
by Egregius
You need the zwave node id, not the device idx.

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 10:58
by jvdz
@Egregius, Thanks for sharing this script. It is a real nice option to refresh devices.
I have a couple TKB dimmers which also don't update correctly and with this script they update fine.
I have also implemented this on a couple of ZWAVE.ME and POPP devices as they only sometimes don't update correctly.
One thing I noticed is that when you add the on/off action scripts like you specified, it seems the eventsystem gets "blocked" until the script ends before processing the next event.
Most devices report statuses several times which means that the script runs 2-4 times for each device each time.
This means that when I switches several devices Off or On at the same time, the event system became slow/unresponsive for quite a number of seconds.
The way I have worked around that is to put a bash script in between which is fired by the device On/Off action and doesn't wait for the php to end. It will also check whether the PHP refresh is already running for that particular device in which case it skips firing the php again.
This is the script I create for each device I want to do a refresh for: ( e.g. script:///home/pi/domoticz/scripts/refresh/refresh-tafel.sh)

Code: Select all

#!/bin/bash
#~ Specify the ZWAVE NodeID to refresh
devid=37
#~ Check whether the refrsh is already running for this device and only fire when not.
chk=`sudo ps x | grep "refresh-zwavedevice.php $devid" | grep -cv grep`
if  [ "$chk" = "0" ] ; then
	php /home/pi/domoticz/scripts/refresh/refresh-zwavedevice.php $devid >> /var/tmp/refresh.log 2>&1 &
else
	echo "$(date +%x) $(date +%X) Already running for $devid" >> /var/tmp/refresh.log 2>&1 &
fi
The PHP look like yours with the addition of the parameter containing the Zwave NodeID:

Code: Select all

#!/usr/bin/php
<?php
$devices=file_get_contents('http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=5'); //Change IDX to IDX of your zwave controller
function RefreshZwave($node) {
   echo date("m/d/y H:i:s "),"Start Refresh for $node\n";
   $zwaveurl='http://127.0.0.1:8080/ozwcp/refreshpost.html';
   $zwavedata=array('fun'=>'racp','node'=>$node);
   $zwaveoptions = array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query($zwavedata),),);
   $zwavecontext=stream_context_create($zwaveoptions);
   //~ print_r($zwaveoptions);
   //~ print_r($zwavecontext);
   for ($k=1;$k<=5;$k++){sleep(2);$result=file_get_contents($zwaveurl,false,$zwavecontext);echo date("m/d/y H:i:s "),"refresh ",$node," ",$result,"\n";if($result=='OK') break;}
}
if(!empty($argv[1])) {
	RefreshZwave($argv[1]);
	}
else {
	echo date("m/d/y H:i:s "),"no idx defined\n";
	}
As you can see, it also creates a log file so I can see what is going on.
Cheers
Jos

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 11:37
by Egregius
Great, implemented and operational. With some minor changes (like use domoticz log and argument for node id).

In my cron.php:

Code: Select all

if($STlichtterrasgarage>$vijfsec) RefreshZwave(16);
if($STlichthallzolder>$vijfsec) RefreshZwave(20);
if($STlichtinkomvoordeur>$vijfsec) RefreshZwave(23);
if($STkeukenzolder>$vijfsec) RefreshZwave(56);
if($STwerkbladtuin>$vijfsec) RefreshZwave(68);
if($STwasbakkookplaat>$vijfsec) RefreshZwave(69);
if($STlichtbadkamer>$vijfsec) RefreshZwave(65); 
if($STkerstboom>$vijfsec) RefreshZwave(70); 
in secure/functions.php

Code: Select all

function RefreshZwave($node) {
	shell_exec('/var/www/secure/refreshzwave.sh '.$node);
}
In secure/refreshzwave.sh:

Code: Select all

#!/bin/bash
#~ Specify the ZWAVE NodeID to refresh
devid="$1"
name="$2"
#~ Check whether the refrsh is already running for this device and only fire when not.
chk=`sudo ps x | grep "refreshzwave.php $devid" | grep -cv grep`
if  [ "$chk" = "0" ] ; then
   php /var/www/secure/refreshzwave.php $devid $name >> /var/log/domoticz.log 2>&1 &
else
   echo "$(date +%Y-%m-%d) $(date +%X)     Zwave refresh already running for $devid $name" >> /var/log/domoticz.log 2>&1 &
fi
in secure/refreshzwave.php:

Code: Select all

#!/usr/bin/php
#!/usr/bin/php
<?php 
$devices=file_get_contents('http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=5'); //Change IDX to IDX of your zwave controller
function RefreshZwave($node,$name) {
   echo date("Y-m-d H:i:s "),"     ZWave Refresh Started for $node $name\n";
   $zwaveurl='http://127.0.0.1:8080/ozwcp/refreshpost.html';
   $zwavedata=array('fun'=>'racp','node'=>$node);
   $zwaveoptions = array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query($zwavedata),),);
   $zwavecontext=stream_context_create($zwaveoptions);
   //~ print_r($zwaveoptions);
   //~ print_r($zwavecontext);
   for ($k=1;$k<=5;$k++){
	   sleep(2);
	   $result=file_get_contents($zwaveurl,false,$zwavecontext);
	   echo date("Y-m-d H:i:s "),"     ZWave Refresh ",$node," ",$name," ",$result,"\n";
	   if($result=='OK') break;}
}
if(!empty($argv[1])&&!empty($argv[2])) {
   RefreshZwave($argv[1],$argv[2]);
   }
else {
   echo date("Y-m-d H:i:s "),"     ZWave Refresh: no idx or name defined\n";
   }

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 11:54
by jvdz
Egregius wrote:Great, implemented and operational. With some minor changes (like use domoticz log and argument for node id).
That was quick :D
Did you noticed the same with the event system before this last change?
Just for my curiosity: is there still a need for the cron job as the script is always fired when a device changes?

Jos

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 12:08
by Egregius
It happens sometimes that switches don't respond instantly. They are set in the logfile, the status is on in Domoticz but the switch waits several seconds. I blame it on Zwave network being to busy at that moment.
Then I disabled and removed all power reports, made already a huge difference.
Now I have to wait to see if this is also a better solution.

About the cron:
In my setup I only call switches of 1 roomplan and run a small part of my cron.php file upon every device change. Only time critical stuff is done, like switch a light on on movement.
Then with cron, I execute the whole script every minute. That handles switching off lights, heating and lots of other stuff.

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 12:22
by Egregius
Script seems to work :D
It just saved a not nescessary refresh

Code: Select all

2015-12-18 12:19:50.899  OpenZWave: Domoticz has send a Switch command! NodeID: 16 (0x10)
2015-12-18 12:19:50.900  (ZWave) Lighting 2 (Licht_Garage)
2015-12-18 12:19:50.912  Executing script: /home/pi/domoticz/scripts/domoticz_main
2015-12-18 12:19:51.179  (ZWave) Lighting 2 (LichtTerrasGarage)
2015-12-18 12:19:51.485  (RFXCOM) Lighting 2 (PIR_Living)
2015-12-18 12:19:52.232  Executing script: /home/pi/domoticz/scripts/domoticz_main
2015-12-18 12:19:52.663  Executing script: /home/pi/domoticz/scripts/domoticz_main
2015-12-18 12:19:52      RefreshZwave Started for node 16 (TerrasGarage)
2015-12-18 12:19:53      RefreshZwave already running for node 16 (TerrasGarage)
2015-12-18 12:19:55      RefreshZwave node 16 (TerrasGarage) OK

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 12:42
by jvdz
Egregius wrote:Script seems to work :D
It just saved a not necessary refresh
Nice :)
The other big difference is that the PHP is shelled and the bash script returns right away making the event system continue right away as you can see in the log.

Jos

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 16:50
by Egregius
Must say that this is some good stuff you did.

Another one:

Code: Select all

2015-12-18 16:47:17.766  (ZWave) Lighting 2 (Keuken)
2015-12-18 16:47:18.037  (ZWave) Lighting 2 (KeukenZolder)
2015-12-18 16:47:18.681  Executing script: /home/pi/domoticz/scripts/domoticz_main
2015-12-18 16:47:19.089  Executing script: /home/pi/domoticz/scripts/domoticz_main
2015-12-18 16:47:19      RefreshZwave Started for node 56 (KeukenZolder)
2015-12-18 16:47:19      RefreshZwave already running for node 56 (KeukenZolder)
2015-12-18 16:47:21      RefreshZwave node 56 (KeukenZolder) OK
That happens with the Qubino Flush 2 relays.
'Keuken' is switched, thats Q1 or switch ID2. Those relays then update the switch on ID1 causing a second trigger of the domoticz_main script.
Maybe I'll test some more to prevent simultanous runs of the domoticz_main script.

Re: Wrong zwave switch level displayed On, 255 %

Posted: Friday 18 December 2015 17:02
by jvdz
Egregius wrote: That happens with the Qubino Flush 2 relays.
'Keuken' is switched, thats Q1 or switch ID2. Those relays then update the switch on ID1 causing a second trigger of the domoticz_main script.
Maybe I'll test some more to prevent simultanous runs of the domoticz_main script.
Isn't that what you want to be able to test for the new status of the second changed device?
Not sure how your events are triggered with domoticz_main as I am using LUA for all my EVENTS logic, and that fires for each changing device separately providing all changed values for that device in the DeviceChanged{} table. In this case the first run would present the new status for "Keuken" and the second run for "KeukenZolder".

Jos

Re: Wrong zwave switch level displayed On, 255 %

Posted: Monday 28 December 2015 0:53
by G3rard
jvdz wrote:@Egregius, Thanks for sharing this script. It is a real nice option to refresh devices.
...
Cheers
Jos
I am trying this code as well because I have a Popp/Duwi dimmer which updates the value in Domoticz after change only after a while.
What is the function of this code in the php?

Code: Select all

$devices=file_get_contents('http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=5'); //Change IDX to IDX of your zwave controller
It gives an error when you open the url (changed it to the right idx) and the $devices seems not be used anywhere else in the code.

Re: Wrong zwave switch level displayed On, 255 %

Posted: Monday 28 December 2015 7:00
by Egregius
Without it the second url fails after couple of days.
Try opening /ozwcp/cp.html directly without going first to hardware/zwave, it'll be a blank page.
In my latest version $devices is used to detect nodes marked as dead and send them a heal command.

Code: Select all

#!/usr/bin/php
<?php 
include 'functions.php';
$devices=json_decode(file_get_contents($domoticzurl.'json.htm?type=openzwavenodes&idx='.$zwaveidx),true); //Change IDX to IDX of your zwave controller
function RefreshZwave2($node,$name) {
	global $domoticzurl;
	$zwaveurl=$domoticzurl.'ozwcp/refreshpost.html';
	$zwavedata=array('fun'=>'racp','node'=>$node);
	$zwaveoptions = array('http'=>array('header'=>'Content-Type: application/x-www-form-urlencoded\r\n','method'=>'POST','content'=>http_build_query($zwavedata),),);
	$zwavecontext=stream_context_create($zwaveoptions);
	for ($k=1;$k<=5;$k++){
		sleep(1);
		$result=file_get_contents($zwaveurl,false,$zwavecontext);
		logwrite('RefreshZwave node '.$node.' '.$name.' '.$result);
		if($result=='OK') break;
		sleep(1);
	}
}
if(!empty($argv[1])&&!empty($argv[2])) {
	RefreshZwave2($argv[1],$argv[2]);
}
else {
	logwrite("RefreshZwave: no idx or name defined");
}
sleep(3);
if($mc->get('deadnodes')<$tienmin) {
	foreach($devices as $node=>$data) {
		if ($node == "result") {
		foreach($data as $index=>$eltsNode) {
		  if ($eltsNode["State"] == "Dead") {
			  $reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command&param=zwavenodeheal&idx='.$zwaveidx.'&node='.$eltsNode['NodeID']),true);
			  logwrite('Node '.$eltsNode['NodeID'].' '.$eltsNode['Description'].' ('.$eltsNode['Name'].') marked as dead, healing command = '.$reply['status']);
			  telegram('Node '.$eltsNode['NodeID'].' '.$eltsNode['Description'].' ('.$eltsNode['Name'].') marked as dead, healing command = '.$reply['status'].'.Errors='.$preverrors);
			  $errors=$errors+1;
		  }
		}   
	 }
	}
	$mc->set('deadnodes',$time);
}
$totalerrors=$preverrors+$errors;if($totalerrors!=$preverrors) $mc->set('errors',$totalerrors);