Python plugin: Presence detection from wireless router

Python and python framework

Moderator: leecollings

sapnho
Posts: 45
Joined: Monday 27 August 2018 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9962
Location: Frankfurt am Main
Contact:

Re: Python plugin: Presence detection from wireless router

Post by sapnho »

Hi EscApe,

the #forcegeneric option didn't make the Cloud Key controller work. But the direct IP link to the access points provides really fast presence data. Your solution is so good, I would not like to dilute it with anything that makes it worse.

So, I guess what I am going to do is to treat the 3 access points as 3 different routers. The only question, and you mentioned that here earlier in the thread, is that I need to combine them into one "Anyone home" switch using the override function. But I don't really have an idea how to do that.

Can you point me in the right direction how this could be implemented? Thanks!
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

Lets say your Mesh routers are called mesh1, mesh2 and mesh3.
Add the plugin 3 times, once for every router.
Set the override mode for mesh1 to ‘indefinitely’.
Create a (blockly or lua) action that does something like:
If anyone mesh2 is on or anyone mesh3 is on then set override on mesh1 is on. Add another that does: if anyone mesh2 is off and anyone mesh3 = off then override mesh1 is off. (This is not actual working code, just a pointer towards the logic).

Override for mesh1 will now control anyone on mesh1 (which is also controlled by the actual devices on mesh 1).
If there is anyone connected to any of the 3 routers the anyone on mesh1 should show it. If there is nothing connected to any of the routers then it should be off.
The override buttons on mesh2 en mesh3 can still be used for temporary overrides.

This should work in theory. I have never tried it (only use one router) but would be very interested to hear if it really works and how it performs.

Maybe I will someday add multi router support but that’s going to take some time and is not a priority for me right now.
Prutsium
Posts: 92
Joined: Monday 18 June 2018 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany / Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by Prutsium »

EscApe wrote: Saturday 01 September 2018 22:24 Lets say your Mesh routers are called mesh1, mesh2 and mesh3.
Add the plugin 3 times, once for every router.
Set the override mode for mesh1 to ‘indefinitely’.
Create a (blockly or lua) action that does something like:
If anyone mesh2 is on or anyone mesh3 is on then set override on mesh1 is on. Add another that does: if anyone mesh2 is off and anyone mesh3 = off then override mesh1 is off. (This is not actual working code, just a pointer towards the logic).

Override for mesh1 will now control anyone on mesh1 (which is also controlled by the actual devices on mesh 1).
If there is anyone connected to any of the 3 routers the anyone on mesh1 should show it. If there is nothing connected to any of the routers then it should be off.
The override buttons on mesh2 en mesh3 can still be used for temporary overrides.

This should work in theory. I have never tried it (only use one router) but would be very interested to hear if it really works and how it performs.

Maybe I will someday add multi router support but that’s going to take some time and is not a priority for me right now.
Would it not be more easy to hardcode Plugin-2 and Plugin-3 to use the values from Plugin-1? (except for Ip/User/Pass)
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

I don’t think that would be easier, but maybe you have something in mind I did not think of(?) you would have to mess around with the plugins python logic. If you have those skills you could also try to add multi router support (throw multiple router addresses and there (individually detected) capabilities in a python dictionary and loop thru them on every poll. Sounds easy enough, but will take lots of testing and tuning to get e.g. acceptable timeout periods).

The method I suggested is relatively easy to implement (standard domoticz scripting approach). It will be easier to maintain if the plugin is updated in the future (should just keep working).
hjzwiers
Posts: 163
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Presence detection from wireless router

Post by hjzwiers »

I added #forcegeneric and now all phones are "seen". I will see how this works the coming days.

Thnx
sapnho
Posts: 45
Joined: Monday 27 August 2018 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9962
Location: Frankfurt am Main
Contact:

Re: Python plugin: Presence detection from wireless router

Post by sapnho »

Thanks escApe. The logic sounds simple enough. I tried this with a Blockly script but it doesn't work yet.
In my cash mesh1=EG, mesh2=Mitte and mesh3=Turm.

This is what I used for on: Image

and for off: Image

I turned the phones in mesh1 and mesh2 off and they are shown as off. Mesh3 phones are still connected but Override mesh1 is set to off.

Anything I am missing?
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

Hi spapnho,

I made some quick screenshots to explain. This is not the actual logic, but the same principal.

Your blocky and/or looks like:
wrong use of blockly.png
wrong use of blockly.png (39.34 KiB) Viewed 1248 times
which means: if thingA = ( on or (thingB = on))

But should look like:
blockly example.png
blockly example.png (36.78 KiB) Viewed 1247 times
which means: if (thingA = on) or (thingB = on)
sapnho
Posts: 45
Joined: Monday 27 August 2018 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9962
Location: Frankfurt am Main
Contact:

Re: Python plugin: Presence detection from wireless router

Post by sapnho »

Changed it still doesn't work.

Image

Maybe I am doing something wrong in event handling? Trigger is device?
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

Are mitte anyone and turm anyone changing as expected (i think the event logic will only run if there is a change)
other things to check:
- is override mode of EG set to ‘indefinitely’?
- is the evensystem working (do other blockly actions work)?
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

Uhm.. oke.. couldn't help myself. Attached you will find a experimental version of the plugin that can connect to multiple routers.

I am programming in the blind here, since i only have one router to test on.
Please test this in a setup with more than one router and share your results.

You can configure multiple routers by separating them with a comma on the settings page:

192.168.0.1, 192.168.0.2, 192.168.0.3

Note:
- all routers must have the same username
- all routers must use the same password OR must heave been setup to use key based ssh authentication
- the #forcegeneric should NOT be used. If even one of the routers uses a generic command, then you are no better of than connecting to just that router. (please check your logs to see if every router is using the preferred hardware based tools).
- the capabilities of each router are detected separately and the poll cycle will contact each router with it's own suitable command
- Keep an eye on performance. Eg: If you poll every 5 seconds and the plugin has to check 4 routers each time, then this could impact your domoticz performance. Especially if some routers are offline or responding slowly. Worst case each ssh command could take the full timeout period (2-3 seconds). 2 x 4 is 8 seconds of waiting every 5 seconds ... That's gonna hurt ;-)

Please support further enhancement of this plugin by sharing your findings. Is it working? Do you notice any performance impact? What poll frequency did you set for how many routers? etc

If you encounter issues, then please share de plugins debug log (from startup + first poll cycle)

To install: simply replace the plugin.py of an existing (and working) version with this one (remove the .txt extension)
Attachments
plugin.py.txt
(24.98 KiB) Downloaded 35 times
sapnho
Posts: 45
Joined: Monday 27 August 2018 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9962
Location: Frankfurt am Main
Contact:

Re: Python plugin: Presence detection from wireless router

Post by sapnho »

Hi escApe, ok, great stuff, just got back, deleted the iDetect hardware and switches and installed the new one.

So far, with all iPhones in mesh1 it works. I will now try the other floors.. ;)
sapnho
Posts: 45
Joined: Monday 27 August 2018 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9962
Location: Frankfurt am Main
Contact:

Re: Python plugin: Presence detection from wireless router

Post by sapnho »

Moving the phones around the various access points also seems to work. I turned the phones off in one AP and on in another. So far, it works every time.

Hardware: 3 Unifi AP Pro.

You're a genius! Thanks! 8-)
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by mvzut »

I'm also trying to set things up for use with multiple routers now, but I get an error message from my second router (used as access point only):

Code: Select all

SSH subprocess failed with error (255):b''
It's a pretty old Linksys E3000 router with Tomato firmware.
If I enter the folllowing command from a command line on the Raspberry Pi where Domoiticz is installed:

Code: Select all

sudo ssh [email protected] -i /root/.ssh/id_rsa "wl -i eth1 assoclist;wl -i eth2 assoclist"
I get the following result:

Code: Select all

assoclist 6C:96:CF:D5:0C:C7                                                                                                                                         
assoclist 8C:8E:F2:EB:B4:1E
So it seems to react as intended to SSH scripts?

On my Asus router everything works fine.
Any ideas?
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

Just published version 0.6.1 to Github with some new features! :-D

**version 0.6.1**
- Added: Ability to monitor multiple routers (eg in Mesh setup)
- Added: Option to ignore individual mac addresses from the 'Anyone Home' detection. The device in question will still be monitored individualy.
- Fixed: The poll frequency did not reset to its original setting after a ssh failure was restored.

@sapnho: Thanks for testing!
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@mvzut

I set the timeouts pretty low to prevent the plugin from blocking the eventsystem. You can try setting them a bit higher. Your router might be slow to respond. It is also possible that the old router does not have key based authentication setup correctly, but i would expect a more specific error code in that case.

To experiment with te timeouts look at line 85 of version 0.6.1

It says

Code: Select all

 def getfromssh(self, host, user, passwd, routerscript, alltimeout=2, sshtimeout=1): 
The alltimeout determines how long the entire command may take. The sshtimeout determines how long it may take to establish the ssh session. You could even try 10 and 10 en work back from there to see which values you need.

Please let me know if this solves your problem and which times worked for you. Maybe i can make it more dynamic a future version.

EDIT: Authentication failures might also lead to a timeout of the overall process
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by mvzut »

Thanks for the quick reply!

I think I know what's going wrong... I'm not able to set the same username on both routers.
- On the Asus router, it can be anything except "root" or "guest"
- On the Linksys, SSH access ALWAYS needs to use "root" as username, this cannot be changed

Could you perhaps make it so that you can enter different usernames for each router, separated by a comma, just as with the IP addresses?

EDIT:
When I only have the Linksys router in the list with "root" as user, I get the same response:

Code: Select all

2018-09-03 22:19:27.744 Error: (WiFi presence detection 2) SSH subprocess failed with error (255):b''
2018-09-03 22:19:27.744 Error: (WiFi presence detection 2) Could not retreive router capabilities from 192.168.178.253
But when I add #forcegeneric behind the router's address, it works! Any idea what this could mean?
Last edited by mvzut on Monday 03 September 2018 22:26, edited 3 times in total.
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
sapnho
Posts: 45
Joined: Monday 27 August 2018 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9962
Location: Frankfurt am Main
Contact:

Re: Python plugin: Presence detection from wireless router

Post by sapnho »

EscApe wrote: Monday 03 September 2018 21:07 Just published version 0.6.1 to Github with some new features!
Great stuff! What's the right way to update your plugin without resetting everything? ;)
sapnho
Posts: 45
Joined: Monday 27 August 2018 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9962
Location: Frankfurt am Main
Contact:

Re: Python plugin: Presence detection from wireless router

Post by sapnho »

I went ahead and replaced the plugin.py. However now I get error messages every minute (my polling time):
2018-09-03 22:19:16.599 Error: (iDetect) No usable commandlines or routers to check presence. Check configuration and routers.
Last edited by sapnho on Monday 03 September 2018 22:26, edited 1 time in total.
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by mvzut »

sapnho wrote: Monday 03 September 2018 22:21 I went ahead and replace the plugin.py. However now I get error messages every minute (my polling time):
2018-09-03 22:19:16.599 Error: (iDetect) No usable commandlines or routers to check presence. Check configuration and routers.
Did you stop and start the plugin, or restart Domoticz? Otherwise it's probably not re-initializing properly.
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
EscApe
Posts: 528
Joined: Thursday 02 April 2015 8:46
Target OS: Linux
Domoticz version: 2020+
Location: The Netherlands
Contact:

Re: Python plugin: Presence detection from wireless router

Post by EscApe »

@mvzut:

That's just nasty ;-)
I will think about implementing something like that in the future. Don't want to overcomplicate things with all these extra options to cope with very specific situations though. Don't hold your breath, because i have already let this plugin distract me from other projects.

Maybe there is a way to make the routers accept other usernames... even if it is not officially supported(?) I don't know if this is helpful: http://tomatousb.org/tut:adding-your-own-users

You could also consider using a different firmware on the linksys.

@sapnho:
Just copy the new plugin.py over the existing one. Nothing should be reset. At least not with this version, since it uses the some configuration page (xml) and data (domoticz devices).
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest