Presence detection is a useful functionality to implement on your HA system. Not only is it useful to know when certain family members are in or out of the house, it allows you to trigger functions based on this data.
There are couple of options for implementing presence detection, these include BLE Tags, PIR Sensors and WiFi, however these aren’t all convenient. Asking each member of the family to put a BLE tag on their keys isn’t a huge inconvenience, however there is a cost impact as well as remembering to charge them. PIR sensors negate the need to carry extra kit around, however they are not 100 reliable in this scenario and have no way to determine between different people.
Most people these days use a mobile phone, so the logical answer is to use WiFi to determine when a users phone enters and leaves the house. In my pre-existing presence detection mechanism, I was using a monitoring script to ping each phone’s IP address, after configuring my router to hand out a reserved address to each phone. To be fair this worked pretty well, but was prone to the odd bout of unreliability.
To get super-reliable presence detection via WiFi, the ideal solution is to query your wireless access-point directly, that way as soon as a device associates, it updates Domoticz and the same for when the device disassociates. Most access-points don’t provide a problematical way to query this information, so this idea is often not feasible.
However, in my house, I use Ubiquit Unfi wireless equipment. Unifi is a low-cost managed wireless solution, consisting of a range of Access-Points and a software based controller. The controller provides an API that exposes a huge amount of data from the system. This is what we will tap into to get our presence data!
I think there are some scripts floating around to achieve the same result from OpenWRT routers and similar, but this guide will focus on Unifi.
Part 1 – Domoticz Setup
The first thing to do is to create some dummy switches that will represent our users phones.
Login to your Domoticz controller and head to Setup > Hardware. Add a new hardware device with the name Presence and set the type to Dummy (Does nothing, use for virtual switches only). Leave the Data Timeout as it is and click Add.
You should now have a line with the new hardware device called Presence, now head to the Switches tab.
Click Manual Light / Switch. Set the hardware to Presence and enter the name of the first device, e.g. Bobs Phone as device name. Leave the rest of the settings at the defaults and click Add Device. Repeat this process for each device you want to get presence data about.
Now head to Setup > Devices and sort by IDX to get the newest devices at the top, you should see the devices you just created. Make a note of each devices IDX and to which device it applies, this is very important!
Part 2 – Script Setup
First we need to do a bit of information gathering. First, make sure all of the devices you want to get presence information about are connected to your wireless network. Next login to your Unifi controller and head to clients page. Find each device and make a note of its name. You may see multiple devices called android******** and not know which is which, so the alternative is to get each phone and get the name from there from the about section. You can also push out a hostname via DHCP if you prefer.
Next, login to your Domoticz controller via SSH. At this point I shall explain that I keep all my scripts in a directory called /opt/domoticz-scripts, so i will use this example in this guide, however you can store your script wherever you like. Change to your script directory and create a new script called unifi_detect.sh. Make it executable, chmod +x unifi_detect.sh.
Next, make sure you have curl installed, on a Debian based system use apt-get install curl.
Now to populate the script. The script I am using was created by BakSeeDaa on the Domoticz forums and can be found here. Scroll down through the first post until you get to the 7th code box, this contains the script. Copy the code and then paste it into the unifi_detect.sh file you created on your Domoticz server.
Save the file and then reopen it. In the CONFIG START section, enter the credentials for your Unifi and Domoticz Servers.
You shouldn’t need to change the sites declaration line unless you are doing something a little non-standard with your Unifi configuration.
Now find the lines starting:
Edit these to reflect your Unifi device names and the related Domoticz IDX numbers. Add more lines if you have more than 3 devices to monitor.
Now save the script and run it:
You should see some status and see it turning the associated Domoticz switches on or off depending on whether the person is at home or not. You may need to make a few tweaks here.
Once happy create a crontab entry to run the script automatically:
*/5 * * * * /your/script/path/unifi-detect/unifi_detect.sh