home-assistant.github.io/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/index.html
2017-08-26 17:30:22 +00:00

291 lines
23 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>iBeacons: Making presence detection work better (part I) - Home Assistant</title>
<meta name="author" content="Greg Dowling">
<meta name="description" content="A step by step guide how to vastly improve your presence detection by integrating iBeacons.">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="iBeacons: Making presence detection work better (part I)">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/">
<meta property="og:type" content="article">
<meta property="og:description" content="A step by step guide how to vastly improve your presence detection by integrating iBeacons.">
<meta property="og:image" content="https://home-assistant.io/images/default-social.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@home_assistant">
<meta name="twitter:creator" content="@pavoni240">
<meta name="twitter:title" content="iBeacons: Making presence detection work better (part I)">
<meta name="twitter:description" content="A step by step guide how to vastly improve your presence detection by integrating iBeacons.">
<meta name="twitter:image" content="https://home-assistant.io/images/default-social.png">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet">
<link href="/atom.xml" rel="alternate" title="Home Assistant" type="application/atom+xml">
<link rel='shortcut icon' href='/images/favicon.ico' />
<link rel='icon' type='image/png' href='/images/favicon-192x192.png' sizes='192x192' />
</head>
<body >
<header class='site-header'>
<div class="grid-wrapper">
<div class="grid">
<div class="grid__item three-tenths lap-two-sixths palm-one-whole ha-title">
<a href="/" class="site-title">
<img width='40' src='/demo/favicon-192x192.png'>
<span>Home Assistant</span>
</a>
</div>
<div class="grid__item seven-tenths lap-four-sixths palm-one-whole">
<nav>
<input type="checkbox" id="toggle">
<label for="toggle" class="toggle" data-open="Main Menu" data-close="Close Menu"></label>
<ul class="menu pull-right">
<li><a href="/getting-started/">Getting started</a></li>
<li><a href="/components/">Components</a></li>
<li><a href="/docs/">Docs</a></li>
<li><a href="/cookbook/">Examples</a></li>
<li><a href="/developers/">Developers</a></li>
<li><a href="/blog/">Blog</a></li>
<li><a href="/help/">Need help?</a></li>
<li><a href='#' class='show-search'><i class="icon-search"></i></a></li>
</ul>
</nav>
<div class='search-container' style='display: none'>
<div class='search'>
<i class="icon-search"></i>
<input id='search' placeholder='Search the docs…'>
<a href='#' class='close'><i class="icon-remove-sign"></i></a>
</div>
</div>
</div>
</div>
</div>
</header>
<div class="grid-wrapper">
<div class="grid grid-center">
<div class="grid__item two-thirds lap-one-whole palm-one-whole">
<article class="post">
<header>
<h1 class="title indent">iBeacons: Making presence detection work better (part I)</h1>
<div class="meta clearfix">
<time datetime="2016-04-30T07:50:09+00:00" pubdate data-updated="true"><i class="icon-calendar"></i> April 30, 2016</time>
<span class="byline author vcard"><i class='icon-user'></i> Greg Dowling</span>
<span><i class='icon-time'></i> nine minutes reading time</span>
<span>
<i class="icon-tags"></i>
<ul class="tags unstyled">
<li>iBeacons</li>
<li>Presence-Detection</li>
<li>OwnTracks</li>
</ul>
</span>
<a class='comments'
href="#disqus_thread"
>Comments</a>
</div>
</header>
<p><em>This post is by Home Assistant contributor <a href="https://github.com/pavoni">Greg Dowling</a>.</em></p>
<p>In 2013 Apple introduced iBeacons: a class of Bluetooth low energy (LE) devices that broadcast their identifier to nearby devices, including most smartphones. At first glance its hard to imagine why they might be useful. In this two part blog Ill try and explain why they are useful and how you can use them with Home Assistant.</p>
<p>The reason I started using iBeacons was to improve presence detection (and I think thats the case with most people) so thats what Ill discuss in <em>part 1</em>. In <em>part 2</em> Ill talk about using iBeacons to track devices that cant track themselves.</p>
<h3><a class="title-link" name="using-beacons-to-improve-owntracks-location-data" href="#using-beacons-to-improve-owntracks-location-data"></a> Using beacons to improve OwnTracks location data</h3>
<p>When you use OwnTracks in standard <em>major move</em> mode (which is kind to your phone battery) it sometimes fails to update when youd like it to. In my case I found that it would often send a location update as I was on my way home, but then not update when I got home. The result would be that Home Assistant would think I was 500M away from home, and take quite a while to notice I was home. It would also mean that the automation that should turn on my lights when I got home didnt work very well! There were a few times when my phone location updated at 2am and turned the lights on for me. Fortunately my wife is very patient!</p>
<p>Luckily, OwnTracks supports iBeacons so I could use them to make presence detection more reliable. When OwnTracks sees a beacon it recognises, it will send an update. This means that if you put a beacon at your front door - OwnTracks will see it within a few seconds of you arriving home - and send an update saying it has seen this iBeacon.</p>
<a name="read-more"></a>
<h3><a class="title-link" name="getting-started" href="#getting-started"></a> Getting Started</h3>
<p>To do this you first need to set up <a href="/components/mqtt/#picking-a-broker">MQTT</a> and <a href="/components/device_tracker.owntracks/">OwnTracks</a> in Home assistant - and make sure that HA can track your phone.</p>
<p>You then have to (A) tell Home Assistant where the beacon is located and (B) tell OwnTracks to recognise the beacon.</p>
<h4><a class="title-link" name="a-tell-home-assistant-where-your-beacon-is-located" href="#a-tell-home-assistant-where-your-beacon-is-located"></a> A. Tell Home Assistant where your beacon is located</h4>
<p>You tell HomeAssistant about fixed locations by creating a Zone with the longitude and latitude of your beacon. You should also give the zone a name which you will also use when you set up OwnTracks. An an example this zone specifies the location of my drive way.</p>
<p><strong>Example <code class="highlighter-rouge">configuration.yaml</code> entry</strong></p>
<div class="language-yaml highlighter-rouge"><pre class="highlight"><code>
<span class="s">zone</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">name</span><span class="pi">:</span> <span class="s1">'</span><span class="s">Drive'</span>
<span class="s">latitude</span><span class="pi">:</span> <span class="s">XXX</span>
<span class="s">longitude</span><span class="pi">:</span> <span class="s">YYY</span>
<span class="s">radius</span><span class="pi">:</span> <span class="s">100</span>
</code></pre>
</div>
<p>The radius isnt used by the beacon code, but it is used by the GPS location sensing code. Ill come back to this a little later. For now just use 50 or 100.</p>
<p>Once youve created the zone - you need to restart HA. The next step is:-</p>
<h4><a class="title-link" name="b-tell-owntracks-to-track--your-beacon" href="#b-tell-owntracks-to-track--your-beacon"></a> B. Tell OwnTracks to track your beacon</h4>
<ol>
<li>Go to the OwnTracks app on your phone</li>
<li>Touch the <code class="highlighter-rouge">Regions</code> menu at the bottom of the screen</li>
<li>Touch the <code class="highlighter-rouge">+</code> symbol at the top right of the screen</li>
<li>Give the beacon a name e.g. <code class="highlighter-rouge">-drive</code> (start the name with a <code class="highlighter-rouge">-</code> see below for the reason)</li>
<li>Turn Share to <code class="highlighter-rouge">On</code></li>
<li>Skip the <code class="highlighter-rouge">Circular Region</code> section</li>
<li>Enter the <code class="highlighter-rouge">UUID</code> of your beacon - this may be written on your beacon - or you can copy it from the management app that came with your iBeacon device. Its a long number so its easier to copy if you can!</li>
<li>Enter the <code class="highlighter-rouge">Minor</code> and <code class="highlighter-rouge">Major</code> numbers for your iBeacon - or leave them at 0 which will match all beacons with that <code class="highlighter-rouge">UUID</code></li>
</ol>
<p class="img">
<img width="200" border="2" src="/images/blog/2016-04-ibeacons/owntracks_beacon_setup.png" />
</p>
<p>Once youve added the iBeacon - you should be able to see it on the OwnTracks region screen. If your phone can see the packets from that beacon, OwnTracks will turn the relevant Region red.</p>
<p class="img">
<img width="200" src="/images/blog/2016-04-ibeacons/owntracks_red_beacon.png" />
</p>
<p>When OwnTracks sees the beacon (and turns the region red), it also sends an MQTT packet to HA to say that you have entered that region.</p>
<p>The result of the configuration above would be to set the location of device.phone to <code class="highlighter-rouge">Drive</code> , (and the GPS location to XXX,YYY) when your phone sees the beacon.</p>
<p>So with the steps above you should be able to improve the reliability of tracking your phone - and send timely updates to HA. I did this for my home - and the lights now turn on before I reach the house on foot. If I arrive by car they turn on within a few seconds of arriving, before I can get to the front door.</p>
<p>Im also pleased to say I no longer get an <em>arrive home</em> event at 2am that turns the lights on. I hope Ive convinced you that iBeacons are worth trying!</p>
<h3><a class="title-link" name="mixing-beacons-and-gps-locations" href="#mixing-beacons-and-gps-locations"></a> Mixing Beacons and GPS locations</h3>
<p>You will probably use beacons to make entry into your existing GPS zones more reliable. By default either a beacon or a GPS location can cause you to enter a zone - and HA has some logic that should make them two work well together (it ignores GPS updates when youre in an iBeacon Zone).</p>
<p>However you can also use beacons for situations where GPS doesnt work well.</p>
<p>This might be because the zones are too close together - or even on top of each other!
For example, my wife works next door - and I couldnt detect whether shes at home or in the office via GPS because the accuracy wasnt high enough. However I can do this by using two beacons.</p>
<p>To make this type of presence detection work you need to turn GPS off for a zone in Home Assistant by making them <code class="highlighter-rouge">passive</code>. This is important because otherwise HA will try to decide between two close together zones without enough data. This doesnt work well.</p>
<p>A passive zone can only be entered via an iBeacon, so a GPS location update will always pick the other zone.</p>
<p>I set up my Home zone to be a standard region, and my office zone to be passive, so the home zone can be entered in the normal way via either GPS or a Beacon.</p>
<p><strong>Example <code class="highlighter-rouge">configuration.yaml</code> entry</strong></p>
<div class="language-yaml highlighter-rouge"><pre class="highlight"><code>
<span class="s">zone</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">name</span><span class="pi">:</span> <span class="s1">'</span><span class="s">Office'</span>
<span class="s">latitude</span><span class="pi">:</span> <span class="s">XXX</span>
<span class="s">longitude</span><span class="pi">:</span> <span class="s">YYY</span>
<span class="s">radius</span><span class="pi">:</span> <span class="s">3</span>
<span class="s">passive</span><span class="pi">:</span> <span class="s">true</span>
</code></pre>
</div>
<p>You could use this technique to try to detect which room someone is in. This might allow you to notice whether someone is in the living room or the bedroom - even though one is above the other (although beacon packets do pass through walls and floors).</p>
<p>To get this to work youll probably need to experiment with the beacon signal strength to try to match the beacon reception area to the location you want to track. Let me know if you get this to work (it doesnt make sense in my open plan house)</p>
<h3><a class="title-link" name="conclusion" href="#conclusion"></a> Conclusion</h3>
<p>Presence tracking sounds easy - and its an important part of Home Automation. Trying it shows how difficult it is to get presence detection right. Ive found that iBeacons have improved the reliability and timeliness of knowing where I am, and I hope I encouraged you to try them too.</p>
<h3><a class="title-link" name="tips" href="#tips"></a> Tips</h3>
<p>You can find out more about configuring the OwnTracks application and beacons <a href="http://owntracks.org/booklet/features/beacons/">here</a></p>
<p>There is information about configuring Homeassistant to use beacons <a href="https://home-assistant.io/components/device_tracker.owntracks/">here</a></p>
<h4><a class="title-link" name="connections-and-disconnecting" href="#connections-and-disconnecting"></a> Connections and disconnecting</h4>
<p>Owntracks treats a region name with a leading <code class="highlighter-rouge">-</code> as a hint that it shouldnt disconnect after a single missed packet. This improves the ability to keep a connection to a beacon.</p>
<p>However, even when using this feature Ive noticed that you can still lose connections (although it seems to vary by beacon manufacturer and type - Ill talk more about this in <em>part 2</em>). This means that its best to take into account that you may see false enter/leave events in HA. You may be able to improve this by changing how often the beacons send packets - and by increasing the signal strength (both will drain your beacon batteries more quickly). You can usually change these parameters in the app supplied by the iBeacon maker. You can also find some high power beacons (which have worked well for me).</p>
<p>In automations you can use a <code class="highlighter-rouge">for:</code> to avoid triggering during a brief disconnect, or use a script with a delay. Stay tuned for <em>part 2</em> for an example of this.</p>
<h4><a class="title-link" name="using-multiple-beacons-for-the-same-zone" href="#using-multiple-beacons-for-the-same-zone"></a> Using Multiple beacons for the same Zone</h4>
<p>iBeacons have a <code class="highlighter-rouge">UUID</code> (usually set to the same value for beacons from the same manufacturer), as well as a <code class="highlighter-rouge">minor</code> and <code class="highlighter-rouge">major</code> number. If you set two beacons to have exactly same details then OwnTracks will think multiple beacons are at the same location.</p>
<p>This means you can have more than one beacon around your home - and a connection to any of them will count as <code class="highlighter-rouge">home</code> to OwnTracks and HA. This reduces disconnections.</p>
<p>You can achieve the same effect by using the same the same <code class="highlighter-rouge">UUID</code> but different <code class="highlighter-rouge">major</code> / <code class="highlighter-rouge">minor</code> numbers - and tell OwnTracks not to worry about the <code class="highlighter-rouge">minor</code> / <code class="highlighter-rouge">major</code> numbers for a particular region by setting them to 0).</p>
<p><em>Make sure to also check out <a href="/blog/2016/05/26/ibeacons-how-to-track-things-that-cant-track-themselves-part-ii/">part II</a> where I talk about how to use iBeacons to track any object.</em></p>
</article>
<section id="disqus">
<h3 class="indent title">Comments</h3>
<div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript></div>
</section>
</div>
<aside id="sidebar" class="grid__item one-third lap-one-whole palm-one-whole">
<div class="grid">
<section class="aside-module grid__item one-whole lap-one-half">
<h1 class="title delta">About Home Assistant</h1>
<ul class="divided">
<li>
Home Assistant is an open-source home automation platform running on Python 3. Track and control all devices at home and automate control.
</li>
<li><a href='/getting-started/'>Get started with Home Assistant</a></li>
<li><a href='/demo/'>Try the online demo</a></li>
<li><a class="twitter-follow-button" href="https://twitter.com/Home_Assistant">Follow Home Assistant on Twitter</a></li>
<li><div class="fb-like" data-href="https://www.facebook.com/homeassistantio/" data-layout="standard" data-action="like" data-size="small" data-show-faces="true" data-share="false"></div></li>
</ul>
</section>
<div id="fb-root"></div>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.async=true;js.src='//platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<script>(function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(d.getElementById(id)){return;}js=d.createElement(s);js.id=id;js.async=true;js.src="//connect.facebook.net/en_US/all.js#appId=338291289691179&xfbml=1";fjs.parentNode.insertBefore(js,fjs);}(document,'script','facebook-jssdk'));</script>
<section class="sharing aside-module grid__item one-whole lap-one-half">
<h1 class="title delta">Share this post</h1>
<a href="//twitter.com/share"
class="twitter-share-button"
data-via="home_assistant"
data-related="home_assistant"
data-url="/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/"
data-counturl="/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/" >Tweet</a>
<div class="fb-share-button" style='top: -6px;'
data-href="/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/"
data-layout="button_count">
</div>
<div class="g-plusone" data-size="standard"></div>
</section>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<section id="recent-posts" class="aside-module grid__item one-whole lap-one-half">
<h1 class="title delta">Recent Posts</h1>
<ul class="divided">
<li class="post">
<a href="/blog/2017/08/26/release-0-52/">0.52: Scripts editor, Nello.io locks, HipChat and Abode Home Security</a>
</li>
<li class="post">
<a href="/blog/2017/08/12/release-51/">0.51: Massive history speed up, finished automation editor and official vacuum cleaner support</a>
</li>
<li class="post">
<a href="/blog/2017/08/01/hasspodcast-ep-5/">Home Assistant Podcast #5</a>
</li>
<li class="post">
<a href="/blog/2017/07/29/release-50/">0.50: Voice control, History improvements, and Xiaomi</a>
</li>
<li class="post">
<a href="/blog/2017/07/27/talk-python-podcast/">Talk Python interview with Paulus Schoutsen</a>
</li>
</ul>
</section>
</div>
</aside>
</div>
</div>
<footer>
<div class="grid-wrapper">
<div class="grid">
<div class="grid__item">
<div class="copyright">
<a rel="me" href='https://twitter.com/home_assistant'><i class="icon-twitter"></i></a>
<a rel="me" href='https://facebook.com/homeassistantio'><i class="icon-facebook"></i></a>
<a rel="me" href='https://plus.google.com/110560654828510104551'><i class="icon-google-plus"></i></a>
<a rel="me" href='https://github.com/home-assistant/home-assistant'><i class="icon-github"></i></a>
<div class="credit">
Contact us at <a href='mailto:hello@home-assistant.io'>hello@home-assistant.io</a> (no support!).<br>
Website powered by <a href='http://jekyllrb.com/'>Jekyll</a> and the <a href='https://github.com/coogie/oscailte'>Oscalite theme</a>.<br />
Hosted by <a href='https://pages.github.com/'>GitHub</a> and served by <a href='https://cloudflare.com'>CloudFlare</a>.
</div>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">home-assistant.io</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
</div>
</div>
</div>
</div>
</footer>
<script>
var _gaq=[['_setAccount','UA-57927901-1'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
</script>
<script>
var disqus_shortname = 'home-assistant';
// var disqus_developer = 1;
var disqus_identifier = 'https://home-assistant.io/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/';
var disqus_url = 'https://home-assistant.io/blog/2016/04/30/ibeacons-part-1-making-presence-detection-work-better/';
var disqus_script = 'embed.js';
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}());
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
<script type="text/javascript">
docsearch({
apiKey: 'ae96d94b201c5444c8a443093edf3efb',
indexName: 'home-assistant',
inputSelector: '#search',
debug: false // Set debug to true if you want to inspect the dropdown
});
document.querySelector('.search .close').addEventListener('click', function(ev) {
ev.preventDefault();
document.querySelector('.search-container').style.display = 'none';
});
document.querySelector('.show-search').addEventListener('click', function(ev) {
ev.preventDefault();
document.querySelector('.search-container').style.display = 'block';
document.getElementById('toggle').checked = false;
document.querySelector('.search-container input').focus();
});
</script>
</body>
</html>