Presence detection is the ability to detect who is home at any given time, this piece of information is crucial for every Home Automation System, it allows you to customize every gadget to the taste of every member of the family. Implementing presence detection can get very sophisticated but you may want to start with something simple and improve it over time. Today, I want to walk you through the simplest and quickest way to implement presence detection with OpenHab.
If you are not yet familiar with OpenHab I suggest you check this post: First Steps with OpenHab and Home Automation.
Let’s get started!
Requirements of your Presence Detection System
Like for every new functionality, you need some kind of requirements gathering before starting your implementation. I want my Presence Detection system to be able to do at least the following:
- Provide information when a given person arrives or leaves the apartment.
- Monitor at any time who is home.
- Having an indicator that tells me if there are people at the apartment.
I know! It is very simple…but you have to start with something, don’t you?
You might be thinking…let’s put motion sensors all over the place!
Or you might just be thinking that you are hungry…who knows…
Anyway, motion sensors are not always a great idea and here are the reasons why:
- They can tell you that there is someone at home but they can’t tell you who that person is.
- I share my apartment with pets, a crazy cat and a crazy dog, and motion detectors go nuts with things like the cat jumping around the apartment.
- For my first approach to presence detection with Openhab, I want to keep it simple and avoid buying new gadgets.
Ok, so no motion sensors ruled out…
What is it going to be then?
One of the things that distinguish ourselves from our pets is that we have smartphones and they don’t, in most cases at least…
The first thing that your smartphone does when you get home is connecting to the WiFi. At this point, you were probably able to join the dots…
That’s right! we are going to detect who is home by checking phones connected to the wifi.
Let’s get down to business then…
Beginner’s Guide to Implement Presence Detection with OpenHab
Assign Static IP’s to the devices you want to Track
This is the kind of thing people often forget therefore I have decided to put it in the first place. Every device that you want to track must have a static IP, otherwise, the next time that you restart your router it will mess everything up. Pretty much every router allows this setting but the setup will be slightly different for each router.
I can’t really cover every single router so I think it is best to leave this part for you to investigate.
Install and configure the Network Binding
In order to monitor the devices connected to the network, we will use the OpenHab Network Binding. The installation and configuration is pretty simple but I will leave you a few screenshots.
1. Open the PaperUI and go to the addons section.
2. Look for the Network Binding and click on Install.
It will take a few minutes. After the process is done, you can go to your inbox and see what your new binding has discovered.
3. Create Things for the devices you want to track
If you go to your PaperUI Inbox you should see all the devices that the network binding has discovered.
Among the new devices, you will see the static ip’s of the smartphones that you want to track. If you don’t see them, run the discovery process again.
Once you have identified the static ip’s you can create things for the devices that you want to track by clicking on the tick icon. Repeat the process for all the devices you are willing to track before moving to the next step, normally one per person you want to keep track on.
4. Create a group of items
At this point we should be able to track every device separately however, most of the times we only need to know if there is somebody at home. We don´t need to know specifically who that person is.
– How can you do that?
– You could ping every single device one by one until you find one that is online!
– Sure, that would work…it is a bit painful and rather difficult to maintain though.
Instead, we are going to use a group item. Every time that you want to add a new device to your household you only need to add it to the group. Easy and low maintenance 🙂
Edit one of your items files under /etc/openhab2/items/ and add the following content:
Group:Switch:OR(ON,OFF) gPresence "gPresence" <icon_presence>
The status of the group will be ON if any of the smartphones in the group is connected to the network.
The group will allow you to know if there is somebody at home for general rules, but you will still be able to trigger different actions by a person (device) detected, you’ll have the best of both worlds.
5. Create Items for the phones you want to track
From the Things menu, click on the thing that you want to track the status for and link a new item to the channel online.
As part of the definition, make sure you add the item to the group defined in the step 4, gPresence.
Repeat this step until you have an item for every device that you want to track.
You presence detection with openhab is ready to be used.
If Openhab doesn´t detect that your devices are online or produces unreliable results, check the following tips:
1. Make sure that you are looking for the right IP’s.
2. Make sure that the device is online using the following command in SSH.
sudo arping -w 5 -c 1 -I INTERFACE IP_ADDRESS
** Replace the interface and the IP address of your particular case. Run ifconfig if you don’t know the name of your interface.
3. Make sure arping is installed
sudo apt-get install iputils-arping
Let’s see what can you do with it!
Presence Detection with OpenHab in your Sitemap
One of the things that you may want to do is to show in your sitemap who is home. Sitemaps are one of the features available on Openhab to interact with your Home Automation System. If you are not familiar with it you can learn more about it here OpenHab Sitemaps.
Here you can find a simple example of how you can do that. You need to replace the name of the items by the ones that you have created in the step 5.
Switch item=gPresence icon="parents_1_1" label="Presence"
Text label="People" icon="parents_1_1"
Switch item=network_david_online icon="boy_3" label="David"
Switch item=network_sara_online icon="girl_3" label="Sara"
Presence Detection with Openhab in your Rules
There are tons of use cases to use presence detection in your rules, I’ll give you a very simple one.
One of the coolest things about Home Automation is that it helps us saving energy by optimizing the use of resources (lights, temperature, water…). If you want to know more about this topic check 8 Ways to Save Energy with Home Automation.
The key for optimization is to design rules that adapt your smart home to your lifestyle.
One of the things that I used to forget 9 out 10 times that I left the apartment is to shut down the AC or HEAT. This results in wasted energy and money. Here is a simple rule to turn off the AC/HEAT when you leave your house or apartment.
rule "Temperature: Away"
Item gPresence changed to OFF
This is only a starting point but I hope you found it useful.
Where else you think that Presence Detection can help you? Do you have any other ideas on how to detect presence? How do you use your presence detection system? I would like to hear all your ideas.