Presence Detection

Posted: September 21, 2015 in HA, Hacking and playing, House
Tags: , ,

The ‘ol bug bear – how do I see if anyone is home?

I’ve tried OwnTracks, but the app on Android isn’t the most accurate and reliable (although it is being updated). I’ve tried the network health monitoring binding in OpenHAB and asked it to detect the phone when it connects to the home network (spot the flaw #1). I’ve tried using a BlueTooth (BT) dongle in the OH controller and running regular scans for my phone.

But none of them work 100%. OwnTracks just lets me down (waypoints especially). I leave my Wi-Fi off most of the time to save battery (and network health doesn’t seem to detect it when it is on). BT does work, as long as i am in range. But the minute I go upstairs the phone is out of range and the system thinks I’ve left the house. The other gotcha is that my wife doesn’t turn BT on, so that’s a pain as well.

When I’d spoken to SmartThings (some CES trip before Samsung got involved!) we had discussed the presence tag that their system supported. Fast-forward to the UK release of the new Samsung version and the presence tags suddenly are actually available in the UK. Yay!

Boo! They are ZigBee. So absolutely bugger all use to me. I’m not plugging in the Almond+ or building some new Xbee device just to solve this issue.

I need a device that is small enough to be with me all the time, not be intrusive and allows for reliable long term use before requiring battery changing. Oh and the batteries need to be cheap. I’m not spending more on a replacement battery than the actual device cost me. The other driver is that I’ve managed to get some more scripts written that control our heating system. So if I can prove that no one is home, I can automatically turn the heating off!

BlueTooth Low Energy

A long time ago (~2012) I’d done some experiments on presence detection for work. My concept was to use BT, BTLE, Wi-Fi etc etc to try and establish who was in a room during set-top box use. While this wouldn’t give a one-to-one correlation between who actually watched something and who was there, it would narrow the field considerably. If device X was always present when cooking shows were watched, we could start to recommend cooking shows when device X was in the room. The more devices we detected  and more watching data we got, the better the correlation would become.

Anyway, the idea was greeted with enthusiasm and then totally mis-understood and ended up being something to do with localised advertising in bus stops…. No I don’t know how it got to that either!

The point of this is that when I was looking at this, BTLE beacons were just starting to actually be available. Devices like the Fitbit bands used BTLE to communicate with the phone.  What was the state of cheap BTLE devices today?

The Concept

After thinking about it, the only thing that I always have on me when I leave the house is my door keys. When I come in, the door keys get put in the front door lock and that’s where they live. Can I find a device that I can detect in this position in the house reliably? That is also small enough to fit on my keyring? And is tough enough to survive being thrown around as part of the normal lifecycle of a bunch of keys!

Google and Amazon searches revealed several companies selling iBeacons in various size and power formats. After a couple of email conversations with a company called Avvel I ordered a ‘long range iBeacon’.

Avvel iBeacon (long range)

The device turned up the next day and was simple to setup and configure. At this point I’m not really bothered about the Major and Minor broadcast values – this is only for me. Maybe later if it works I’ll look to use these to indicate the device and who has it…

The Code

I’m already using the bluez library via python on my RPi2 to detect the phone via BT. Every 5 minutes I scan for the BT address of our phones. The true false flag is posted into our mqtt broker where OpenHAB uses the value to set the in/out switch. The plan was to do exactly the same except using BTLE.

As usual, it didn’t happen. While i could happily

sudo hcitool lescan

and find the keyfob, the ability to do this in the python script eluded me. Various tutorials talked about using gatttools or adding extra libraries (that failed to either install, compile or run), but none of them worked.  I wanted to use python in order to limit extra packages, libraries and complexity that installing extra things could add to the system. I can’t have a HA controller fall over because of a clash between different bits.

FInally I expanded my search to other languages and found noble for NodeJS.  I’ve actually used noble when I was doing the presence detection work before – that time I was using it embedded in Node RED. As all my recent work has been in NodeJS, I figured I’d try it for this.

The beacon is set to broadcast every 9.9 seconds (at least I think that’s what I set it to be), so the script scans for 25 seconds every three minutes. At the end of the 25 seconds, the script totals up how many times it has seen each of the specific devices and then sends a true or false value to the mqtt broker for each one where it is again picked up by OH and used. The three minute cycle is controlled by a crontab -e while the 25 seconds is a simple JS setTimeOut().

The Result

Well so far it has worked (sort of). The script picks up the keyfob successfully and OH gets the updates as I come and go. The issue is signal propagation. In a straight line with no obstructions I’m able to detect the keyfob up to about 50m (bottom of the garden!). The issue is that between the RPi2 that has the USB dongle doing the detection and the front door where the keys are supposed to live are walls, radiators, kitchen units and potentially small children. This drops the successful detection rate down considerably – it doesn’t stop it completely, but it does drop it enough to make it unreliable.

My next option is to move the OH box to a better location that may have a clearer ‘line of sight’, but that will mean grubbing around behind the AV cupboard and thats not a job to be taken lightly!

 **UPDATE**

As the keyfob is on a big clip on my door keys, it’s actually easier for me to just unclip it and drop it with my other keys in the lounge rather than leave it on the door keys. It’s also safer than me rummaging around in the AV cupboard.

Advertisements
Comments
  1. davidwoody297563184 says:

    If you use a router with Openwrt or dd-wrt, then you can use your router to detect presence. http://tinsley.io/2015/03/openhab-presence-detection-with-dd-wrt/

    • TwistedNix says:

      That’s true – assuming that my phone has wifi on ( it doesn’t by default, I turn it on when I need it).

      I can actually use the network monitoring capability of OpenHAB to do the same thing, but I keep wi-if off unless I need it.

      • davidwoody297563184 says:

        If you are using an android phone, you can use tasker to turn on the wifi when you get near your home’s closest cell tower. I am working on the same issue at my house, still haven’t found the perfect solution. Great blog though, I enjoyed reading it.

  2. TwistedNix says:

    Yup tried Tasker with AutoLocation as well for geofencing…. But the granularity of the geofence becomes an issue. If I take the kids for a scoot around the block, although I am out of the house, I am still within the geofence and the system thinks I am home. Lowering the geofence radius generates false results the other way.

    Other option is to ping for the wi-fi SSId using Tasker as well as geofencing, cell signal/tower names, but it all gets a bit of a pain to maintain.

    I’m actually using Owntracks on my work phone to set a value in OpenHAB, as well as the BTLE dongle, simple network monitoring (device detection on the network) and generic BT. If several of these are ON I’m home. So far it works… Now I just need to persuade the other half to let me tag her!

    If I get that done I can tie the ‘no one is home’ flag in OH to put the heating on to economy automatically.

  3. davidwoody297563184 says:

    I use tasker to turn on the wifi on my phone about a mile before I get to my house, once I pull into the driveway my dd-wrt router detects my connection to the wifi and tell OH that I am home. Just started playing with OH and HA, so I don’t have it doing anything yet. I have 3 wifi thermostats and 1 wifi plug that I can control, but I have a Wink hub on the way and I plan using it the same way you use your Vera.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s