On/Off action php

Moderator: leecollings

Post Reply
rednas
Posts: 132
Joined: Tuesday 20 October 2015 12:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

On/Off action php

Post by rednas »

Hello,

Is it possible to fire a php file in the domoticz scripts folder with an on or off action when a switch is pushed?
This action works:

Code: Select all

script:///home/pi/domoticz/scripts/file.sh
And what I want to do is something like:

Code: Select all

script:///home/pi/domoticz/scripts/file.php
Is this possible?

Thanks in advance!
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: On/Off action php

Post by Egregius »

You can do so much more with php!
Have a look at my signature.
I launch php with a lua script, that's about 100 times faster than the on/off action.
User avatar
vetalok
Posts: 22
Joined: Friday 30 December 2016 12:40
Target OS: Linux
Domoticz version: 3.6273
Location: Lviv, Ukraine
Contact:

Re: On/Off action php

Post by vetalok »

Create a new sh file with code:

Code: Select all

#!/usr/bin/php
<?php
//here your php code
?>
And in action paste

Code: Select all

script:///home/pi/domoticz/scripts/file.sh
liderbug
Posts: 20
Joined: Monday 02 January 2017 17:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: On/Off action php

Post by liderbug »

I was going to post my experience under General but this seems to be a better location.

My inventory:
  • X10 PR511 Dual Floodlight Motion Detector
    several X10 switches and outlets, CM15a
    several Z-Wave switches/outlets
I wanted the configuration: Someone/us/ups/fedex pulls into the driveway, motion detected, (aside pr511s tend to burn out due to load, so I unwired the relay in the unit and wired the lights through a X10 WS467). The 511 sends out x10 d1,2,3,4 on/off, d4 is the switch to the floods - driveway lites up. Then a Mochad script detects D4-On and sends "echo 'pl D5 on' | nc localhost 1099" and the kitchen door light comes on then D6 front door. Then the local power company talked me into whole house protection - which messed up my X10. Some, nothing, others dead, some on not off. Solution, zwave, Domoticz, etc.

Current config: Raspberry Pi B+, Raspberrian (x2) (1 at garage w camera, cm15a. 1 in greenhouse with Domoticz and GH control).

A dom scene 'DriveAlarm':
  • On: script://drive.php On
    Off: script://drive.php Off
    Activation = X10-D4 on

Code: Select all


#!/usr/bin/php
<?php

function srss() {
        $sunrise = date_sunrise(time(), SUNFUNCS_RET_DOUBLE, 39, -104, 90, -7);
        $sunset  = date_sunset(time(),  SUNFUNCS_RET_DOUBLE, 39, -104, 90, -7);
        $now = date("H") + date("i") / 60 + date("s") / 3600;

        if ($sunrise < $sunset)
                if (($now > $sunrise) && ($now < $sunset)) return 1;
                else return 0;
        else
                if (($now > $sunrise) || ($now < $sunset)) return 1;
                else return 0;
}

function OnOff ($idx, $OO)
{
        if ( srss () == 0 )
        {

          $URL = "sg53:8080/json.htm?type=command&param=switchlight&idx=$idx&switchcmd=$OO";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $URL);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $output = curl_exec($ch);
          curl_close($ch);
        }
}

#### main () ###
        $OO = $argv[1];

        OnOff (4, $OO);  //front door
        OnOff (5, $OO);  //kitchen door

My orig script had:
shell_exec(sprintf('bash /home/cal/bin/Drivealarm > /dev/null 2>&1 &'));
and Drivealarm contained:
curl -s -i -m 5 -H "Accept: application/json" "http://sg53:8080/json.htm?type=command& ... itchcmd=On"

Note: on != On & off != Off case sensitive

During the day the flood will always come on but not the 2 doors. As for a doorbell we have three 4 legged doorbells.
asjmcguire
Posts: 71
Joined: Saturday 13 July 2013 2:45
Target OS: Linux
Domoticz version: 3.5877
Location: Scotland
Contact:

Re: On/Off action php

Post by asjmcguire »

Just a few notes....

$sunrise will always be less than $sunset so there is no need to test for this.

if ($now < $sunrise || $now > $sunset)
is all you need to get nighttime status (because just like the the first test - once it rolls over midnight, then $sunrise will be the time of the new day - it's always dark whenever $now < $sunrise or $now > $sunset)

Code: Select all

#!/usr/bin/php
<?php

function srss() {
        $now = time();
        $sunriset = date_sunrise($now, SUNFUNCS_RET_DOUBLE, 39, -104, 90, -7);
        $sunsett  = date_sunset($now,  SUNFUNCS_RET_DOUBLE, 39, -104, 90, -7);
        $t = explode(":",$sunriset);
        $sunrise = mktime($t[0],$t[1]);
        $t = explode(":",$sunsett);
        $sunset = mktime($t[0],$t[1]);

        if ($now > $sunrise && $now < $sunset) { return 1; }
        else { return 0; }
}

function OnOff ($idx, $OO)
{
        if (!srss())
        {

          $URL = "sg53:8080/json.htm?type=command&param=switchlight&idx=$idx&switchcmd=$OO";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $URL);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $output = curl_exec($ch);
          curl_close($ch);
        }
}

#### main () ###
        if (!isset($argv[1])) { exit; }
        $OO = $argv[1];

        OnOff (4, $OO);  //front door
        OnOff (5, $OO);  //kitchen door
Converting the response of sunrise and sunset date funcs to unix timestamps makes them easier to work with - if you need to do anything more complicated in the future. As you can see to achieve this I just split the string using the ":" as the delimiter. As for the check to see what srss returns - if you are looking for it returning 1 (or true) then "if (srss()) { " is preferred - and for checking if it is false or 0 - then "if (!srss()) { "

Also I assigned time() to $now at the start of the function so that time() isn't called multiple times when calculating sunrise and sunset.

I added a quick check to make sure that $argv[1] actually contains something - otherwise since there is no check anywhere as to what $OO actually contains - you could be calling the script with no argument which will still attempt to connect to Domoticz but switchcmd will be empty.

Just some hints for the future ;)

And thoughts for the further future -
I've replaced all this with MQTT - so instead of CURL connecting to the JSON domoticz endpoint - I would now just fire off 2 MQTT messages which Domoticz will receive and process.

{"command":"switchlight","idx":4,"switchcmd":"{$OO}"},{"command":"switchlight","idx":5,"switchcmd":"{$OO}"} published to "domoticz/in" would do the job - and while I could use phpMQTT to publish the messages - I tend to opt for executing mosquitto_pub.

Code: Select all

$t = "/usr/bin/mosquitto_pub -h $MQTT_BROKER -t \"{$MQTT_TOPIC}\" -u \"{$MQTT_USER}\" -P \"{$MQTT_PWD}\" -m \"{\\\"command\\\":\\\"switchlight\\\",\\\"idx\\\":{$idx},\\\"switchcmd\\\":\\\"{$OO}\\\"}\";
exec($t,$out);
AEOTEC ZStick, 11 ZWave Nodes, RFXCOMM, 50ish Byron Sockets.. HE851 (PIR), 2x HE852 (DoorContact)
WS2300, CM180, CC128, 2xTHGR122NX, 2xPiZeroW w/DS18B20, 8Ch 1W Relay Board.
8 Panasonic IP Cams, 1 16ch CCTV DVR + 15 CCTV Cams
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: On/Off action php

Post by Egregius »

Have a look at http://php.net/manual/en/function.date-sunrise.php
You can directly receive sunrise in timestamp:

Code: Select all

function srss() {
        $now = time();
        $sunriset = date_sunrise($now, SUNFUNCS_RET_TIMESTAMP, 39, -104, 90, -7);
        $sunset  = date_sunset($now,  SUNFUNCS_RET_TIMESTAMP, 39, -104, 90, -7);
        if ($now > $sunrise && $now < $sunset) { return 1; }
        else { return 0; }
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest