How to: Pimatic Presence Sensor with OpenWRT en

By Whatson on Monday 18 July 2016 10:40 - Comments are closed
Category: -, Views: 3.043

You can already detect devices on your network with pimatic using the pimatic-ping plugin, but this has one big drawback: phones do not reply to such ping requests if they are in standby.

So the best way is to login to your router and check if the phone is connected to your wifi network. With OpenWRT on your router this is fairly easy to realise.

Assuming you have Pimatic and OpenWRT already running on your Raspberry Pi and router, the first step is to install the shell-execute plugin from the pimatic plugin menu.

After the plugin is done installing, go to the [installed plugins] tab and press [activate].

Next press Restart in the top right corner to restart Pimatic.

Now we need to create a key such that the root user of your raspberry pi can login to your OpenWRT router without using a password.

Open the terminal (OSX and Linux) or open Putty (Windows) and ssh into your raspberry pi:
ssh pi@raspberrypi.lan
(you can also use the ip address instead if you prefer that)

use sudo su to become root (as pimatic is running as root)
pi@raspberrypi:~ $ sudo su

next use:
ssh-keygen -t dsa
to generate a new keypair (just press enter when asked for input)

Copy the key to your OpenWRT router:
scp ~/.ssh/ root@

Login to your OpenWRT router (from your Raspberry):
root@raspberrypi:/home/pi# ssh root@

You will see something like this:
  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 BARRIER BREAKER (14.07, r42258)
  * 1/2 oz Galliano         Pour all ingredients into
  * 4 oz cold Coffee        an irish coffee mug filled
  * 1 1/2 oz Dark Rum       with crushed ice. Stir.
  * 2 tsp. Creme de Cacao

Copy the key to the autorized_keys file:
cd /etc/dropbear
cat /tmp/id_*.pub >> authorized_keys
chmod 0600 authorized_keys

leave your OpenWRT router by using:

test if you do not need a password anymore by logging in to your OpenWRT router:
ssh root@
(you might be prompted for a password for the last time)

leave your OpenWRT router by using:

Go to your browser and go to

Have a look at the DHCP leases table on this page and copy the mac address of the device you want to monitor (in lower case!)

Go back to your terminal window (still as root) and enter:
ssh root@ 'iw dev wlan0 station dump | grep -c XX:XX:XX:XX:XX:XX' | tee
Where XX:XX:XX:XX:XX:XX is the mac address you just copied (in lower case!)
This should return a 1 if the device is connected to wifi or a 0 if it's not.

Now create a new Shell Presence Sensor in Pimatic:
Go to: http://raspberrypi.lan/#devices-page

And create a new device with the following settings:

Pick a name and id, select ShellPresenceSensor as class, enable command and enter the ssh command from above, select the interval at which Pimatic should check for your phone in milliseconds (=seconds x 1000) You want to be easy on this as Pimatic has to login to your router each time. 30-60 seconds should be fine.

press [save] and you can start creating rules with your new presence sensor!