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 wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
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
For iOS https://itunes.apple.com/us/app/owntracks/id692424691?mt=8
For Android https://play.google.com/store/apps/details?id=org.owntracks.android
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.
If you have any suggestions for the app, let me know. I have some new stuff in the develop branch like reverse geo lookup and user/location table. Idea was to be able to ask homey for the location of user and that homey responds with adress or geo fence name.
But did not find the time to complete that (the interaction with homey)
is it maybe owntracks/deviceID name?
Should be owntracks/[your synology user]/deviceID
Let me think about that, by the way a good free API for reverse LATLON lookup is http://www.geonames.org/maps/us-reverse-geocoder.html
Here are my current settings:
a quick google showed me: http://www.hivemq.com/try-out/
Connect to Public Broker
Websocket Port: 8000
So no TLS!
Be careful with a public broker. Should only be used for testing, because everyone can see your data.
If you do not want to host your own broker, then take a look at https://www.cloudmqtt.com they have a free plan that is more then enough for owntracks. And you can configure security there.
Used it for testing owntracks when I was implementing TLS.
Thx, the reverse geo lookup is already implemented. You can test it, if you use the develop branch and install using athomcli.
Thanks, will do soon
buy a danalock, that is working as you are suggesting ;-)
Heb 3 regions gedefinieerd maar ik zie niet dezelfde groene en rode bollen als ingmar en op de MQTT server zie ik alleen:
niks met /event aan het einde.
Heb zowel bluetooth als GPS regions geprobeerd en de Region op shared gezet in owntracks.
To enable use the following guide at http://www.wadewegner.com/2014/05/create-an-ibeacon-transmitter-with-the-raspberry-pi/
After installing all the required stuff (2 minute job) I start the iBeacon with:
sudo hciconfig hci0 up
sudo hciconfig hci0 leadv 3
sudo hciconfig hci0 noscan
sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8 63 B5 00 00 00 00 C8 00
Once the iBeacon is sending it's UUID, in this case 636f3f8f-6491-4bee-95f7-d8cc64a863b5 (converted to hex), add this as a region as well in the Owntracks app on your phone. Make sure the radius is set to zero and optionally, set the description name starting with a minus "-". This prevents a lot of enter/leave events, it acts as a kind of filter. For all the options and explanation see http://owntracks.org/booklet/features/beacons/
I added the next 2 flows for precense detection. I also added a new better logic variable "iBeacon woonkamer" which I update on enter and leave. You could use an iBeacon for each room or at your frontdoor for example ;-)
start this iBeacon stuff as a service after rebooting the raspberry.
Could it be that it is missing some kind of iBeacon identification protocol type?
Would be cool if it could detect regular discoverable bluetooth devices.
My flows are not triggered. The log says it's trying to trigger an event for 'undefined':
20170225-10:35:48 Accuracy is within limits
20170225-10:35:48 Trigger enter card for undefined
20170225-10:35:48 Trigger generic card for undefined
20170225-10:35:48 state.topic = owntracks/marvin/sailfish/event topic = owntracks/marvin/sailfish/event state.fence = undefined geofence = Home
Any idea? I have configured my flow with
owntracks/marvin/sailfish/event and Home
I don't know about the Samsung
i suggest to remove the region and recreate it.
I've installed Mosquitto on my synology, add a forward in my router (port 1883) and sometimes Owntracks is connected but often i received this message. Do you have any clue?