The Homey Community has been moved to https://community.athom.com.
This forum is now read-only for archive purposes.
Setup Homey with MQTT for location detection
I just started a couple of months ago, with domotica and after reading lots of reviews I decided to go forward with the Homey. I already bought Philips Hue and the experience with geofences was great! Coming home, lights go on, leaving home light go off, perfect, as it should be!
Then I started using the geofencing of Homey… It didn’t work right away so I tried various settings (fixed home position, change range in the iOS app, enable/disable “use home detection”) and it worked for a couple of days where after it broke down again and again. I noticed a lot of users with the same problem and was quite surprised in the almost zero response from Athom. Honestly, I did not try to reach out to them as the many forum topics really discouraged me, maybe I should have done that. Don’t get me wrong, I love the homey, but it stills feels like a startup product and the guys at Athom seem to have too much on their plates.
Using a domotica controller without proper location detection is quite useless in my opinion, so I started researching all the possibilities:
https://apps.athom.com/app/com.gps-trace you need to have the IOS app running at the foreground all the time = huge battery drain.
https://apps.athom.com/app/nl.terryhendrix.smartpresence this is based on network presence (Homey pings your device), so is your phone in the same network as your home (e.g. your Wi-Fi network). I quite like this approach but the huge downside is that phones often go in sleep mode, hence I was all the time away, and home, and away, etc.
https://apps.athom.com/app/com.ifttt) IFTTT also has a location service which works perfect with geofences. The only problem is that you can only use one IFTTT account with Homey. Yes, I can install IFTTT in my phone and my wife’s phone, but you can’t determine who came home as we are using the same IFTTT account.
So, after many hours of experimenting I was about to sell the Homey as really nothing worked that reliable. In the end, I found the Owntracks app https://apps.athom.com/app/nl.scanno.owntracks of Menno van Grinsven which is based on the MQTT Client for Homey made by Johan Kuster. (Kudos to both!) This solved everything!!
What is Owntracks and MQTT
Owntracks http://owntracks.org/ is an Open Source project which provides an iOS and an Android app with which your smartphone records its current location. It supports geofences, iBeacons and makes use of the MQTT protocol http://mqtt.org/. It runs extremely stable and there is no battery drain when using significant mode. This mode is sufficient to work properly, so there is no need for active tracking, hence keeping the app open. Manual: http://owntracks.org/booklet/
Install MQTT on Raspberry pi3 / Synology
First off all we need a MQTT server also called a broker. It’s a messaging server to which you can send messages and it is a server where you can subscribe to. There are many platforms to install MQTT on, I will show you 2 ways; on a Raspberry and on a Synology. You can also use a public MQTT server, but privacy is at stake then.
I installed Raspbian Jessie Lite (https://www.raspberrypi.org/downloads/raspbian/) on my Pi 3, but you can use whatever distro you want. Next I installed MQTT by following these steps borrowed from various sites, but this one covers it all: http://www.switchdoc.com/2016/02/tutorial-installing-and-testing-mosquitto-mqtt-on-raspberry-pi/
Basically, it’s a matter of connecting with a terminal (ssh) to your raspberry and run the following commands:
sudo apt-key add mosquitto-repo.gpg.key
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
and then stop the service
sudo /etc/init.d/mosquitto stop
configure the MQTT service, see the article for the details. One thing I added which is not described is: listener 8883 [ip of your raspberry] like listener 8883 192.168.0.123
sudo nano /etc/mosquitto/mosquitto.conf
Start the service
sudo /etc/init.d/mosquitto start
I made the MQTT service to start automatically with the following command:
sudo update-rc.d mosquitto defaults
sudo update-rc.d mosquitto enable
For mqtt on Synology, https://sites.google.com/site/mccworkshop2015/mosquitto-on-synology-nas
If MQTT package is not starting:
1) ssh to your synology and log in with your normal account
2) execute: cd /var/packages/mosquitto/scripts
3) sudo vi start-stop-status (you need to be root, hence the sudo), give your admin password when requested
4) using vi, comment the line which declares the USER such that it reads:
(in case you are not so familiar with vi (or any other reader for that matter): use arrows to go to the line, make sure you are at the first character, press 'i' to enter insert mode, press the '#' to uncomment, press 'esc' and ':wq'. If the last command says you are not allowed to write the file, go back to the command line with ':q!', and start again at step 3, the SUDO part is rather important there :-)
Now the mosquitto service should start normally using the package manager to run the package.
Note, don’t forget to forward port 1883 on your router to the internal ip address of your MQTT server! Otherwise you won’t be able to connect from outside your network. Example below is of a Ziggo Router.
Install Owntracks app on iOS
Setup your connection as below where TrackerID is a 2 letter indication on the map (not important). Deviceid is your name which will be used in the Homey flows. Host is your public ip address of your internet connection, in my case I use the DNS name of my Ziggo connection. If my ip address changes then I will keep the connection as I am using the DNS name. Port is the same as you have configured in the /etc/mosquitto/mosquitto.conf file. Authentication is the same user/password you use to access your raspberry pi.
Once successfully connected:
Setup geofences, called regions as:
Red means I am currently in the region Home. Make sure you share the region and make the radius bigger than 0 meter, in my case I made it 100 meter.
Now you have installed the MQTT server and setup your app it’s time to connect it to Homey. I assume you already installed the owntracks app https://apps.athom.com/app/nl.scanno.owntracks
Basically, there is not really much to configure. Deselect the “use public broker” option, enter the ip address of your MQTT server, leave port on 1883. Enter your pi credentials.
I have set the accuracy to 250m, you have try out what works for you. If you set it too low geofence reports might be discarded. By the way, the logging feature is great, it tells you exactly what is happening with incoming messages.
Save it and the log should tell you it’s connected.
For presence detection, I use multiple flows and apps to make it work. It would really save a lot of flows when Athom finally supports the OR condition. But for now, I created the following:
First, or a 2-person household, setup 3 variables within better logic https://apps.athom.com/app/net.i-dev.betterlogic
Translated (Nadine at home, somebody is home, Ingmar is home):
Next I created the following flows. BIG NOTE, it is really important that whenever you work with Owntracks geofences you must enter it as: "your owntracks topic"/event in my case owntracks/pi/ingmar/event with the geofence name you shared in the region section in your app.
OT Inbound home Ingmar
OT outbound home Ingmar
OT Inbound home Nadine
OT outbound home Nadine
set variable iemand thuis - nadine vertrekt
set variable iemand thuis - ingmar vertrekt
How it works
When both my wife and left the “niemand thuis” variable is false, so you can trigger all kinds of thing on that. In my case I arm the alarm, turn of all the lights, etc.
If one of us comes home (read, enters the geofence) the “niemand thuis” variable is set to true, which disables the alarm, turn on the lights, etc.
Setup MQTT with TLS which encrypts the traffic between your MQTT server and your device.
Setup iBeacon on the raspberry PI to use with the Owntracks app, as an extra location service.