SOLVED:- Help with PHP Script

Moderator: leecollings

Post Reply
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

SOLVED:- Help with PHP Script

Post by simon_rb »

I have been running the script below for a while now and I didn't realise that the script updates the switch every minute even if its off. So I have over 87K log entries lol

Been trying to find online documentation that allows me to only update switch is its actually Off if its On and switch it On if its Off rather then update it every minute regardless of state.

Code: Select all

#!/usr/bin/php
<?php
$ctx = stream_context_create(array('http'=>array('timeout' => 3,)));
$domoticzurl='http://127.0.0.1:8080/';
$denon_address = 'http://192.168.1.12';
$time=$_SERVER['REQUEST_TIME'];
function Schakel($idx,$cmd) {
   global $domoticzurl;
   $reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd='.$cmd.'&level=0&passcode='),true);
   if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
   return $reply;
}
$denonmain=json_decode(json_encode(simplexml_load_string(file_get_contents($denon_address.'/goform/formMainZone_MainZoneXml.xml?_='.$time,false,$ctx))),TRUE);
if($denonmain['ZonePower']['value']=='ON') Schakel(216,'On');
else Schakel(216,'Off');
Cheers
Last edited by simon_rb on Sunday 13 August 2017 21:31, edited 1 time in total.
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: Help with PHP Script

Post by pvm »

You can first retrieve its value and check before switching
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with PHP Script

Post by simon_rb »

pvm wrote: Wednesday 09 August 2017 23:03 You can first retrieve its value and check before switching
Thats what I plan to do but its been a while and my PHP skills are rather limited.. lol

I assume this will get the result and have to work out how to implement this

Code: Select all

$json_string = file_get_contents("http://192.168.1.26:8080/json.htm?type=devices&rid=216");
 $parsed_json = json_decode($json_string, true);
 $parsed_json = $parsed_json['result'][0];
 $stat = $parsed_json['Status'];
 echo "Status ".$stat."\n";
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Help with PHP Script

Post by Egregius »

I see that these are parts of my code :)
Best option would be to implement my pass2php code because PHP then already knows the status of the switch.
Another option would be to query domoticz for the status before updating it.
Both will work the same, pass2php is very expandable, the second one more limited if you'd like to add stuff later.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with PHP Script

Post by simon_rb »

Egregius wrote: Thursday 10 August 2017 3:57 I see that these are parts of my code :)
Best option would be to implement my pass2php code because PHP then already knows the status of the switch.
Another option would be to query domoticz for the status before updating it.
Both will work the same, pass2php is very expandable, the second one more limited if you'd like to add stuff later.
More than likely! Any examples of pass2php. I didn't think it would be so involved to add a check before updating! lol

Found this (of yours) and read through it. I understand the principles however just trying to add it to the above code and I'm at a loss lol
https://www.domoticz.com/forum/viewtopi ... 64&t=12343
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Help with PHP Script

Post by Egregius »

Where are you blocked? At installation of pass2php? Then please respond in that topic.
At integration of pass2php for this issue?
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with PHP Script

Post by simon_rb »

Egregius wrote: Thursday 10 August 2017 22:49 Where are you blocked? At installation of pass2php? Then please respond in that topic.
At integration of pass2php for this issue?
Integration, am I rewriting a whole script or adding it to the above? Please forgive my ignorance. My coding (if you could even call it that) is limited to having examples so I can understand and reuse bits to make it all work. It’s how I learn anyways lol
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Help with PHP Script

Post by Egregius »

In pass2php this would go in one of the cron scripts:

Code: Select all

$denon_address='http://192.168.1.12';
$denonmain=json_decode(json_encode(simplexml_load_string(file_get_contents($denon_address.'/goform/formMainZone_MainZoneXml.xml?_='.$time,false,$ctx))),TRUE);
$denon=apcu_fetch('sdenon');
if($denonmain['ZonePower']['value']=='ON'&&$denon!='On')sw('denon','On');
elseif($denonmain['ZonePower']['value']=='OFF'&&$denon!='Off')sw('denon','Off');
PWiAM
Posts: 2
Joined: Saturday 12 August 2017 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Help with PHP Script

Post by PWiAM »

I can help with php. I'm net to this forum, what program/script need this code? What is your version of PHP, and what renders it? Also, please provide some of the logs so i know what you're saving.

Let's do some debugging. Look at this code, and do 3 runs. each run wait a bit and note if log has ON, OFF, or ERROR, or missing or syntax error. I note where to comment out by saying FROM HERE and TO HERE so just encapsulate the code withing starting tag "/*" and ending tag "*/".

Like so:

Code: Select all

<?php

    /**
     * FROM HERE
     */
     /*
     var_dump(['derp' => 'pred']);
     */
    /**
     * TO HERE
     */

?>
Comment code on designated places and please post results.

Code: Select all

#!/usr/bin/php
<?php
$ctx           = stream_context_create(['http' => ['timeout' => 3,]]);
$domoticzurl   = 'http://127.0.0.1:8080/';
$denon_address = 'http://192.168.1.12';
$time          = $_SERVER['REQUEST_TIME'];

function Schakel($idx, $cmd)
{
    global $domoticzurl;

    /**
     * comment this block out and tell me if logs updated
     *
     * FROM HERE
     */
    $reply = json_decode(file_get_contents($domoticzurl . 'json.htm?type=command&param=switchlight&idx=' . $idx . '&switchcmd=' . $cmd . '&level=0&passcode='), true);

    if ($reply['status'] == 'OK') {
        $reply = 'OK';
    }
    else {
        $reply = 'ERROR';
    }

    return $reply;
    /**
     * TO HERE
     */
}

$denonmain = json_decode(json_encode(simplexml_load_string(file_get_contents($denon_address . '/goform/formMainZone_MainZoneXml.xml?_=' . $time, false, $ctx))), true);

if ($denonmain['ZonePower']['value'] == 'ON') {
    /**
     * FROM HERE
     */
    Schakel(216, 'On'); // seems like if ZonePower is in ON state, this line still puts it on
    /**
     * TO HERE
     */
}
else {
    /**
     * FROM HERE
     */
    Schakel(216, 'Off');
    /**
     * TO HERE
     */
}
Please post results.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with PHP Script

Post by simon_rb »

Thanks PWiAM!!! :)

The script actually runs fine, I just needed to find ways of only updating the switch if it needed updating.

Egregius

Right, so I install pass2PHP and will I then just need that new script above and forget the older/origin one? I am just trying to understand how it works then I can adapt and learn :D

Thank you!
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Help with PHP Script

Post by Egregius »

Correct, once pass2php is operational the above script should go in for example __cron60.php to check every minute.
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Help with PHP Script

Post by Egregius »

Thinks this should do it:

Code: Select all

#!/usr/bin/php
<?php
$ctx = stream_context_create(array('http'=>array('timeout' => 3,)));
$domoticzurl='http://127.0.0.1:8080/';
$denon_address = 'http://192.168.1.12';
$time=$_SERVER['REQUEST_TIME'];
function Schakel($idx,$cmd) {
   global $domoticzurl;
   $reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd='.$cmd.'&level=0&passcode='),true);
   if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
   return $reply;
}
$denonmain=json_decode(json_encode(simplexml_load_string(file_get_contents($denon_address.'/goform/formMainZone_MainZoneXml.xml?_='.$time,false,$ctx))),TRUE);
$denon=json_decode(file_get_contents($domoticzurl.'json.htm?type=devices&rid=216'),true);
if(isset($denon))$denon=$denon['result'][0]['Status'];
if($denonmain['ZonePower']['value']=='ON'&&$denon=='Off')Schakel(216,'On');
elseif($denonmain['ZonePower']['value']=='OFF'&&$denon=='On') Schakel(216,'Off');
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with PHP Script

Post by simon_rb »

Egregius wrote: Sunday 13 August 2017 13:29 Thinks this should do it:

Code: Select all

#!/usr/bin/php
<?php
$ctx = stream_context_create(array('http'=>array('timeout' => 3,)));
$domoticzurl='http://127.0.0.1:8080/';
$denon_address = 'http://192.168.1.12';
$time=$_SERVER['REQUEST_TIME'];
function Schakel($idx,$cmd) {
   global $domoticzurl;
   $reply=json_decode(file_get_contents($domoticzurl.'json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd='.$cmd.'&level=0&passcode='),true);
   if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
   return $reply;
}
$denonmain=json_decode(json_encode(simplexml_load_string(file_get_contents($denon_address.'/goform/formMainZone_MainZoneXml.xml?_='.$time,false,$ctx))),TRUE);
$denon=json_decode(file_get_contents($domoticzurl.'json.htm?type=devices&rid=216'),true);
if(isset($denon))$denon=$denon['result'][0]['Status'];
if($denonmain['ZonePower']['value']=='ON'&&$denon=='Off')Schakel(216,'On');
elseif($denonmain['ZonePower']['value']=='OFF'&&$denon=='On') Schakel(216,'Off');
Thank you. So I have pasted the above in a file called denon_cron60.php and put that in the secure folder. I assume I now have to make sure that executes every minute. How is that achieved ? I also assume I must change you pass2php.php to enter the address of where the secure folder is which contains the denon_cron60.php..
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Help with PHP Script

Post by Egregius »

Hmmm, this script was intended to run withoit pass2php, so exactly the way how you used to run it.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with PHP Script

Post by simon_rb »

Egregius wrote: Sunday 13 August 2017 20:34 Hmmm, this script was intended to run withoit pass2php, so exactly the way how you used to run it.
Oh... Haha!!!!!! :oops:

Run it manually and get this

Code: Select all

PHP Fatal error:  Uncaught Error: Call to undefined function simplexml_load_string() in /home/pi/domoticz/scripts/Denon_Airplay_Check.php:13
Stack trace:
#0 {main}
  thrown in /home/pi/domoticz/scripts/Denon_Airplay_Check.php on line 13
I have uncommented extension=php_xmlrpc.dll in the php.ini file

I think PHP 5.6 is still installed - would this need to be removed? does it default to 5.6 instead of 7.0?

Ah, ok. I ran it as PHP7.0 from command line and it worked! Thanks for your help!! Thanks ALOT! Your patience has been amazing!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest