[SOLVED] Presence detection via ping script... -> Klenzel.de

Hi,

I found the OpenHab Project at Klenzel.de. A nice german page with interesting hints to bind different “Things”…

on https://klenzel.de/3328 is described a solution to detect the presence via ping and Bluetooth.
Due to the fact, that my openHab-RapberryPI is located in the basement and the WiFi-router in the livingroom, the Wifi-connection of our smartphones is supposed to be much more reliable than the Bluetooth. Therefore I tried to implement just the Ping (WiFi) solution. but it doesn’t works.

The problem is the website isn’t supported anymore. Therefore I won’t get any answers at their discussion board.

Maybe someone knows the page and that solution?!

How can I debug the script? The site and the RESTservice shows both OFF. When I’m logged in as the “pi” user and try the script, it shows the expected state (ON). Some one on the discussion of the source site supposed that openHAB user has no access (permission) to call a ping!?

I won’t believe that, who is not permitted to call a PING !?

In the meanwhile I found a comment to call Linux commands on behalf other users. When I tried the script as openHAB user, it shows the expected state (ON). Therefore I think it is not a permission problem…??

But how could I debug?

bash script located: /scripts/ping.sh

#!/bin/bash
if [ -z "$1" ] ;then
    echo "parameter missing"
    exit 1;
fi
ping -c1 $1 > /dev/null
if [ $? -ne 0 ]; then
        echo "OFF"
else
        echo "ON"
fi

the related items/binding:

Switch Phone1_WLAN 	"Her mobile" 	(gPresencePhone) 	{exec="<[/scripts/ping.sh 192.168.1.1:60000:REGEX((.*?))]"}
Switch Phone2_WLAN 	"His mobile" 	(gPresencePhone) 	{exec="<[/scripts/ping.sh 192.168.1.115:60000:REGEX((.*?))]"}

As you can see, it tests the ip of the my router (192.168.1.1) just to make sure it is a valid and active IP.

And finally the sitemap like this:

Frame label="SmartPhone presence"  
{
Text item=Phone1_WLAN 	label="Her mobile [MAP(praesenz.map):%s]" icon="present"
Text item=Phone2_WLAN 	label="His mobile [MAP(praesenz.map):%s]" icon="present"
}

Again, my question, where could the be problem? How to debug this solution???

Thx in advance!

1 Like

Hello!

What version of OpenHAB are you using? In 1.X you have Network Health binding, and in OH 2.0 there is Network binding. Not sure if 1.X binding have both modes, but in 2.0 you can use it to, either ping host, or try connecting to it on (any) custom port to see if it’s available (and therefore present/turned on)

Best regards,
Davor

I’ m using OH 1.8

the source-site is talking about “network-health” and some problems how java is accessing it, therefore this litte script is necessary.

In my opinion the scipt itself is not the problem.

If i try it on console it runs fine:

pi@openHAB:/scripts $ ./ping.sh 192.168.1.1
ON

Easiest way in OH1: use network health and

Edit: no need for any additional scripts … works fine at my installation since almost two years.

In a locked down system or when running in something like a Docker container not having access to ping is not unusual at all. It is unlikely the problem here though.

So why not use the Network (if on OH 2) or the NetworkHealth (if on OH 1) binding? Both implement a periodic ping to detect devices on the network with no need for the complications of a script. The Network binding even gives you a choice to use the implemented ping in the binding or call the system ping utility.

Have you actually tried the NetworkHealth binding to see if you actually have the problem described? I’ve never heard of any such problem and it will frankly be a lot less work than to try and debug problems with the Exec binding. In other words, try NetworkHealth and see if it works. If not THEN come back and try to make the script work.

No but getting OH to execute the script is. In my experience on this forum using the Exec binding is among the top three most difficult ones to get working when something goes wrong.

But, if you insist on doing it the hard way, to figure out why OH can’t run the script you need to:

  • create a Rule that triggers periodically, it looks like you are doing five minutes
  • in the Rule use
val results = executeCommandLine(<command>, 5000)
logInfo("test", "results from ping script: " + results)
  • watch the logs when the rule executes and see what the script generates. Usually it will be the error that will tell you why it won’t work.

Common things that usually go wrong:

  • openhab user doesn’t have permission to run the executables or access the files, update permissions
  • openhab user has no PATH variable and therefore may not have the commands needed in its path, use the full path to all files in the called scripts
  • the exec bindings and executeCommandLine sometimes has problems with spaces, replace spaces with @@

Right now I’m trying the NetworkHealth Binding.

the first IP I tried was well:

Switch NH_Router	"Router"   (gPresence)   {nh="192.168.1.1"}

but when I added the second one, the PC where I’m working at, it doesn’t show the right state: Offline

But others are fine as well… so, there is an issue with NH and some devices…?!

Do you see any errors in the log and did you wait long enough for it to refresh? Do you see it updating the Switch for the PC in events.log?

The answers will indicate whether the problem is with the binding or your configuration.

It looks like I’ve the same issue like this:

Thanks to all of you!

NetworkHealth works fine after this litte tooling:

For the record, the reason you needed to do the above is because java did not have permission to use ping.

As ping is not very reliable in my situation, I use netcat, or nc which is the same. It is executed through the exec binding. See https://github.com/cvdenzen/etc_openhab2.

I have a rule only to translate the value, but this is not necessary. Probably this can be done by changing the ON and OFF values in the item file.

Essentially this is the item in https://github.com/cvdenzen/etc_openhab2/blob/master/openhab2/items/pir.items:
// de tv op begane grond, west, tv, west
// The ping command is not very reliable. Packets are easily lost, resulting in false negatives. I think netcat (=nc) is better.
// options: z: do not send any data, -v verbose, -w : timeout. Use an unused port, that will report “Connection refused”.
// The output line looks like this, when the computer (or smart tv) is on:
// nc: connect to 192.168.2.109 port 23999 (tcp) failed: Connection refused
// When off:
// nc: connect to 192.168.2.79 port 23999 (tcp) failed: No route to host
// nc -zv -w 20 192.168.2.109 23999 2>&1 | grep " connect to " | sed -e “s/.onnection refused./ON/” -e “s/.connect to./OFF/“
Switch bg_w_tv_w “TV huiskamer [%3s]” { exec=”<[/bin/sh@@-c@@nc -zv -w 20 192.168.2.79 23999 2>&1 | grep " connect to " | sed -e “s/.onnection refused./ON/” -e “s/.connect to./OFF/”:30000:REGEX((.*))]”}
// tv tbv persistence, via rule