[SOLVED] Using php script es presence sensor

  • Platform information:

    • Hardware: pi3+
    • OS: raspbian stretch
    • Java Runtime Environment: zulu
    • openHAB version: 2
  • Issue of the topic:

Hello ,
I am using following script to determine if a network device is present or not

#!/usr/bin/php
<?php
if(empty($argv[1])){
        die("MAC address missing");
}else{
        $mac = $argv[1];
}
if(empty($argv[2])){
        die("BOX IP or IPs  missing");
}else{
        $ipList = explode(":",$argv[2]);
}


function checkDevice($ip,$mac){
        $result = "";
        $uri = "urn:dslforum-org:service:Hosts:1";
        $location = "http://".$ip.":49000/upnp/control/hosts";
        $client = new SoapClient(
            null,
            array(
                'location'   => $location,
                'uri'        => $uri,
                'noroot'     => True,
                'login'      => "",
                'password'   => "",
                'connection_timeout' => 5
                )
        );
        try{
                $query = $client->GetSpecificHostEntry(new SoapParam($mac,'NewMACAddress'));
                $result = $query['NewActive'];
        }catch(SoapFault $fault){
                $result = 0;
        }
        return $result;

}

function checkAllDevices($ipList,$mac){
        $result = 0;
        foreach($ipList as $ip){
                $resultCheck = checkDevice($ip,$mac);
                if($resultCheck == 1){
                        $result = 1;
                }
        }
        return $result;
}

echo checkAllDevices($ipList,$mac);
?>

how can I integrate this script which returns 0 or 1 as a presence sensor in my openhab??? Can you help me?

Thanks

You will need to use the executeCommandLine action or the exec binding
You may want to change your script to return ON or OFF to be directly compatible with the openHAB switch items.

Can you give meine Sample Item Line sorry i am nee to openhab since 2 days

Examples are in the links I provided

Hello I have done the following

default.things

Thing exec:command:presence [command="php /home/pi/presence.php D0:04:01:30:A9:E0 fritz.box:fritz.repeater", interval=60, timeout=20]

default.items

Switch Presence "da" <switch> {channel="exec:command:presence:output"}

the script returns ON/OFF now

But nothing happens???

Whtas wrong???

Did you install the Exec binding.?

Yes IT IS installed

with a text item everything is fine…so how can i set the state of a switch???

You need two items and a rule:

items

String textItem { exec channel goes here }
Switch switchItem
rule "String to Switch"
when
    Item stringItem changed
then
    switchItem.postUpdate(stringItem.state.toString)
end

Okay cool thanks…

How can I Set the Switch to “on” instantly but “off” with a delay of 10seconds??

rule "String to Switch"
when
    Item stringItem changed
then
    if (stringItem.state == "ON") {
        switchItem.postUpdate(ON)
    } else if (stringItem.state == "OFF") {
        createTimer(now.plusSeconds(10), [ |
            switchItem.postUpdate(OFF)
        ])
    }
end