[userModule] Network Presence

Tips, Tricks and Scripts to enhance your home automation and workaround known device bugs, limitations and incompatibilities
Provo
Posts: 112
Joined: 19 Oct 2016 19:54

[userModule] Network Presence

Post by Provo » 06 Jan 2018 13:29

There already exists a userModule called Ping Presence that lets you ping an IP to track its presence on the network. However, this module is a singleton which means you can only have one instance of it, and it only lets you track a single IP. I needed to track several, and I wanted better control of parameters like time between pings, ping timeout, number of packets, etc. The creator of the Ping Presence module doesn't seem to be active anymore, so I created a new one with the features I need.

The way it works should be fairly self explanatory, but if you have questions, feel free to ask them here. Regarding bug reports and feature requests, please post them in this thread or as issues on the projects gitlab page.

To install through the app store, use the token kvolden_beta or wait until the ZWay team has verified it.

robbelt
Posts: 6
Joined: 27 May 2016 09:00

Re: [userModule] Network Presence

Post by robbelt » 11 Jan 2018 01:02

Works realy nice with my Android-Phone. But what about an iPhone which disables Wifi on Standby/Lockscreen?

Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: [userModule] Network Presence

Post by Provo » 11 Jan 2018 09:15

Glad you liked it.

As I wrote in the description, many modern smart phones put their WiFi to sleep after the display is turned off. This is true for many Android phones as well. There's not really anything that can be done to prevent this on the module side, unfortunately. You will have to change the phone's WiFi/power settings, if possible, such that the WiFi remains on and the phone can reply to ping.

robbelt
Posts: 6
Joined: 27 May 2016 09:00

Re: [userModule] Network Presence

Post by robbelt » 11 Jan 2018 12:36

Maybe it is prossible to improve the presence detection using hping like mentioned here? https://community.openhab.org/t/iphone- ... rp/18171/9

Another great feature would be to have the possibility to configure a delay (in minutes) after that the presence is switched to "absent". So only if no ping was successfull in the last maybe 30 minutes switch to "absent". This can be very useful in some usecases to make the presence detection more robust. An easy way to implement this can be to make your "ping hysteresis" configurable (not only the last three pings).

Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: [userModule] Network Presence

Post by Provo » 11 Jan 2018 15:23

robbelt wrote:
11 Jan 2018 12:36
Maybe it is prossible to improve the presence detection using hping like mentioned here? https://community.openhab.org/t/iphone- ... rp/18171/9
I'm not super familiar with hping, but I don't think hping itself will help. The solution you link to also uses arp, but in my experience that's not very reliable for device detection in general – for instance, it seems that Android devices are undiscoverable with arp. What seems to make that solution somewhat reliable, is something similar to what I called "ping hysteresis", only with 19 instead of 3 attempts.

It's fair to say that if the WiFi is turned off, it's not connected to the local network anymore, and it will not be discovered as such. But thanks for the suggestion, though.
robbelt wrote:
11 Jan 2018 12:36
Another great feature would be to have the possibility to configure a delay (in minutes) after that the presence is switched to "absent". So only if no ping was successfull in the last maybe 30 minutes switch to "absent". This can be very useful in some usecases to make the presence detection more robust. An easy way to implement this can be to make your "ping hysteresis" configurable (not only the last three pings).
Yes, I've actually been thinking about making the size of the hysteresis configurable, like you're suggesting. I'll see if I can get around to implementing that as soon as I find the time.

Thanks for your input, much appreciated.

Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: [userModule] Network Presence

Post by Provo » 13 Jan 2018 13:25

Version 0.1 was just verified and can be installed without the token.

I have also updated the module to version 0.2. It is no longer a singleton, so if you want to add devices with different settings, you can now instantiate several instances each with different settings for their associated devices. I have also added configurability to the ping hysteresis size.

Beware, I just realized there is no sanity checking on the hysteresis size. Make sure this is 1 or more, or it will have strange side effects. I will get to fixing this as soon as I can.

Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: [userModule] Network Presence

Post by Provo » 14 Jan 2018 12:05

Ping hysteresis size is now limited to no less than 2 (1 would not produce unexpected side effects, but is equivalent to no hysteresis). Version is bumped to 0.3 to make sure the update is pushed. This version is currently being verified, but it's available with the token mentioned in the first post.

Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: [userModule] Network Presence

Post by Provo » 06 Feb 2018 12:21

Just uploaded version 0.4.

The most significant change from 0.3 is that it now supports using netcat – and will even use netcat as default, though ping is still available under Advanced configuration. To use this feature, remember to add "netcat" to .syscommands as well.

The module uses netcat to attempt a socket connection to the given IP on a user definable port (default: 80). The port does not have to be open on the device. Without going into detail, judging by the result of this attempt, the module discovers whether the device is connected to the network. This method is much more reliable with mobile phones than ping is: even with the device in sleep mode, it is usually discovered correctly as either present or absent, provided the timeout value is high enough. The timeout value defaults to 10 seconds, but this can be changed under Advanced configuration. If you experience that your device is often considered present even when it is not, you can experiment with increasing the timeout value somewhat.

Version 0.4 also includes some bug fixes and other less visible improvements.

It's available using the token, and will be available to everyone when it's verified by the Z-Way team.

PS: The config interface may seem a little buggy at first, even though it's not critical, but this is because of an issue with conditional dependencies and default values that is out of my control, which has been flagged as an issue with the Z-Way people. When they fix that, the config interface will automatically work as expected.

PPS: For those wondering, yes, in principle this whole thing wouldn't need netcat. It could use the socket implementations in Z-Way, had it not been for the fact that the tcp socket seems to throw no error at all, and the websocket does not specify the kind of error it throws. This makes them unusable for this purpose. If I ever become aware of this changing, I will consider dropping netcat for the internal sockets so it's easier for users to get it up and running without modifying .syscommands.

Eagle
Posts: 55
Joined: 18 Jun 2017 19:04

Re: [userModule] Network Presence

Post by Eagle » 25 Feb 2018 19:37

Hi,
i really like your module, because there is no other app that supports more than one ip.

but i have a few downsides id like to mention, maybe this is because im using a WD PR4100 with uzb stick.

First of all if im using ping, i get high processor load on every ping and a very laggy system for 1-3 seconds every minute. Ping works, as it recognizes both ip adresses i entered. When i use the other ping presence app, i have no load issues and the system runs fine as usual.

On the other hand i needed to install netcat separately, because on the wd linux system there is no netcat preinstalled. netcat now works in ssh, but the network presence shows both ip adresses aviable, even if they are not in the network registered.

Maybe you got an idea to solve my issues?
netcat and ping are alredy written in .syscommands

Thanks for your work!

Provo
Posts: 112
Joined: 19 Oct 2016 19:54

Re: [userModule] Network Presence

Post by Provo » 26 Feb 2018 09:12

Thanks for reporting this.

Ok, a laggy system is a bit surprising. The pinging is supposed to be asynchronous, so the system calls shouldn't hold up the thread (unless Z-Way is doing something strange internally). But you are also saying that you only have the laggy system when using ping, and not when using netcat?

To take ping first. You are only tracking two IPs? How is your config? Have you made any changes to the advanced config, like timeout or ping count or hysteresis count? What are these values currently set to?

Regarding netcat, what is your config here? This could be a too short timeout issue, or it could be some unexpected netcat output. Try to ssh into your box, and run "time netcat -v -z -w 600 10.0.0.1 80" exchanging the IP with the IP of the device that is failing to show up as absent. At the end it should output the netcat report and some timing information, what does it say?

Also note that with netcat sometimes the first couple of attempts after you've disconnected may show that you're still present, somewhat dependent on your timeout value, so you might want to repeat this a few times or at least make sure your device has been offline for some time, just to have a clean case to work from. You could also experiment with changing the port (which in the command above is set to 80, which is also the default in the module).

Finally, you could try to restart Z-Way, in case for some reason a bunch of chron jobs have been collecting up triggerin large amounts of scheduled pings. By restarting, all jobs should be cleared and the modules will reschedule what they need. This shouldn't happen, and shouldn't affect just ping if it did happen, but I wouldn't completely rule it out.

Post Reply