This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

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

Intro

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.

Raspberry

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

cd /etc/apt/sources.list.d/

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

Synology

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:

#USER="mosquitto"

(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.

Configure Homey

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.

Create Flow

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.

To-do

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.

«13456711

Comments

  • Great work! Thanks for writing this! 

  • Great work! Thanks for writing this! 


    You're welcome.
  • ingmar said:
    Great work! Thanks for writing this! 


    You're welcome.

    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) 
  • Nice tutorial. But do you know the path in a flow for a synology?
    is it maybe owntracks/deviceID name?
  • posthok said:
    Nice tutorial. But do you know the path in a flow for a synology?
    is it maybe owntracks/deviceID name?

    Should be owntracks/[your synology user]/deviceID
    like
    owntracks/admin/pino
  • ingmar said:
    Great work! Thanks for writing this! 


    You're welcome.

    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) 

    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
    Demo:
    http://api.geonames.org/findNearestAddress?lat=37.451&lng=-122.18&username=demo
  • do you know how to connect to the hivemq public server? I've tried using the iOS app but the status stays "Connecting".
    Here are my current settings:
  • Yannick said:
    do you know how to connect to the hivemq public server? I've tried using the iOS app but the status stays "Connecting".
    Here are my current settings:

    a quick google showed me: http://www.hivemq.com/try-out/

    Connect to Public Broker

    Host: broker.hivemq.com
    Port: 1883
    Websocket Port: 8000

    So no TLS!


  • Yannick said:
    do you know how to connect to the hivemq public server? I've tried using the iOS app but the status stays "Connecting".
    Here are my current settings:

    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. 
  • edited February 2017
    ingmar said:
    ingmar said:
    Great work! Thanks for writing this! 


    You're welcome.

    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) 

    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
    Demo:
    http://api.geonames.org/findNearestAddress?lat=37.451&lng=-122.18&username=demo

    Thx, the reverse geo lookup is already implemented. You can test it, if you use the develop branch and install using athomcli. 

    See https://github.com/scanno/nl.scanno.owntracks/commits/develop
  • Anybody tested it with Bluetooth proximity?
  • Yannick said:
    Anybody tested it with Bluetooth proximity?
    Still working on it

  • ingmar said:
    ingmar said:
    Great work! Thanks for writing this! 


    You're welcome.

    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) 

    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
    Demo:
    http://api.geonames.org/findNearestAddress?lat=37.451&lng=-122.18&username=demo

    Thx, the reverse geo lookup is already implemented. You can test it, if you use the develop branch and install using athomcli. 

    See https://github.com/scanno/nl.scanno.owntracks/commits/develop
    ingmar said:
    ingmar said:
    Great work! Thanks for writing this! 


    You're welcome.

    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) 

    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
    Demo:
    http://api.geonames.org/findNearestAddress?lat=37.451&lng=-122.18&username=demo

    Thx, the reverse geo lookup is already implemented. You can test it, if you use the develop branch and install using athomcli. 

    See https://github.com/scanno/nl.scanno.owntracks/commits/develop
    Thanks, will do soon
  • ingmar said:
    Yannick said:
    Anybody tested it with Bluetooth proximity?
    Still working on it
    would be cool if we could use it to open the door when we walk up to it.
  • Yannick said:
    ingmar said:
    Yannick said:
    Anybody tested it with Bluetooth proximity?
    Still working on it
    would be cool if we could use it to open the door when we walk up to it.

    buy a danalock, that is working as you are suggesting ;-)
  • I already have a Yale z-wave lock without BT
  • Mijn Owntracks stuurt geen Region updates naar m'n mqtt broker (heb cloudmqtt geconfigd nu).
    Heb 3 regions gedefinieerd maar ik zie niet dezelfde groene en rode bollen als ingmar en op de MQTT server zie ik alleen:
    owntracks/jmxvluhq/bubmc
    {"batt":47,"lon":4.282221353998592,"acc":32,"p":101.8785934448242,"vel":0,"vac":26,"lat":52.11022141111647,"conn":"w","tst":1487968875,"alt":-2,"_type":"location","tid":"MC"}

    niks met /event aan het einde.

    Heb zowel bluetooth als GPS regions geprobeerd en de Region op shared gezet in owntracks.
  • Yannick said:
    Mijn Owntracks stuurt geen Region updates naar m'n mqtt broker (heb cloudmqtt geconfigd nu).
    Heb 3 regions gedefinieerd maar ik zie niet dezelfde groene en rode bollen als ingmar en op de MQTT server zie ik alleen:
    owntracks/jmxvluhq/bubmc
    {"batt":47,"lon":4.282221353998592,"acc":32,"p":101.8785934448242,"vel":0,"vac":26,"lat":52.11022141111647,"conn":"w","tst":1487968875,"alt":-2,"_type":"location","tid":"MC"}

    niks met /event aan het einde.

    Heb zowel bluetooth als GPS regions geprobeerd en de Region op shared gezet in owntracks.

    Yannick said:
    Mijn Owntracks stuurt geen Region updates naar m'n mqtt broker (heb cloudmqtt geconfigd nu).
    Heb 3 regions gedefinieerd maar ik zie niet dezelfde groene en rode bollen als ingmar en op de MQTT server zie ik alleen:
    owntracks/jmxvluhq/bubmc
    {"batt":47,"lon":4.282221353998592,"acc":32,"p":101.8785934448242,"vel":0,"vac":26,"lat":52.11022141111647,"conn":"w","tst":1487968875,"alt":-2,"_type":"location","tid":"MC"}

    niks met /event aan het einde.

    Heb zowel bluetooth als GPS regions geprobeerd en de Region op shared gezet in owntracks.
    Heb je wel /event in je flows toegevoegd zoals in de screenshots? Want die positie meldingen in je log krijg je alleen als je dus niet gebruikt van de /event. Overigens, volgens mij heb je ook niet een radius groter dan 0 op de regions gezet?

  • ingmaringmar Member
    edited February 2017

    To-do

    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.


  • I have added the iBeacon detection as well. For this I used the internal Bluetooth of the raspberry pi 3B.
    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 ;-)



    To-do
    start this iBeacon stuff as a service after rebooting the raspberry.
  • YannickYannick Member
    edited February 2017
    Thanks, editing radius setting worked!
  • Tried with the UUID of my Samsung TV's bluetooth transmitter which is constantly broadcasting but owntracks doesn't seem to see it.
    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.
  • EvertorNEvertorN Member
    edited February 2017
    Thanks @ingmar

    However,
    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
  • Yannick said:
    Tried with the UUID of my Samsung TV's bluetooth transmitter which is constantly broadcasting but owntracks doesn't seem to see it.
    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.
    Tried to turn off and on your Bluetooth to simulate entering / leaving?
    I don't know about the Samsung   
  • EvertorN said:
    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
    Sometimes disabling and enabling the homey OwnTracks app works. Also check if Home is really Home in your app instead of home. Mind case sensitivity
  • ingmar said:
    EvertorN said:
    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
    Sometimes disabling and enabling the homey OwnTracks app works. Also check if Home is really Home in your app instead of home. Mind case sensitivity
    Changed it to 'home' in the flows and on my phone and restarted the app, still no luck. 20170225-10:58:47 waiting owntracks/marvin/sailfish/event 20170225-11:00:31 getLogLines called 20170225-11:02:32 OnMessage called 20170225-11:02:32 received '{"_type":"transition","tid":"sh","acc":19.2948,"event":"enter","lat":-37.86688484,"lon":145.00134072,"tst":1487981023,"wtst":1487977183,"t":"c"}' on 'owntracks/marvin/sailfish/event' 20170225-11:02:32 Accuracy is within limits 20170225-11:02:32 Trigger enter card for undefined 20170225-11:02:32 Trigger generic card for undefined Any idea? When I check my phone (android) > status it says 'not available' for 'Endpoint state message', could this be the cause?
  • EvertorN said:
    ingmar said:
    EvertorN said:
    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
    Sometimes disabling and enabling the homey OwnTracks app works. Also check if Home is really Home in your app instead of home. Mind case sensitivity
    Changed it to 'home' in the flows and on my phone and restarted the app, still no luck. 20170225-10:58:47 waiting owntracks/marvin/sailfish/event 20170225-11:00:31 getLogLines called 20170225-11:02:32 OnMessage called 20170225-11:02:32 received '{"_type":"transition","tid":"sh","acc":19.2948,"event":"enter","lat":-37.86688484,"lon":145.00134072,"tst":1487981023,"wtst":1487977183,"t":"c"}' on 'owntracks/marvin/sailfish/event' 20170225-11:02:32 Accuracy is within limits 20170225-11:02:32 Trigger enter card for undefined 20170225-11:02:32 Trigger generic card for undefined Any idea? When I check my phone (android) > status it says 'not available' for 'Endpoint state message', could this be the cause?
    I don't see your region in the message, that's why you get undefined : '{"_type":"transition","tid":"sh","acc":19.2948,"event":"enter","lat":-37.86688484,"lon":145.00134072,"tst":1487981023,"wtst":1487977183,"t":"c"}' on 'owntracks/marvin/sailfish/event'

    i suggest to remove the region and recreate it. 

  • Great work Ingmar, Thanks for the effort.
    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?


    MQTT.png 207.7K
  • dians said:
    Great work Ingmar, Thanks for the effort.
    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?


    Seems like an issue with the package or network issues. Maybe sleep mode on your synology?
  • ingmar said:
    dians said:
    Great work Ingmar, Thanks for the effort.
    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?


    Seems like an issue with the package or network issues. Maybe sleep mode on your synology?
    So simple ;-) Synology into sleep mode after 20 min..... Sometimes the simplest answers are the most obvious ones.
Sign In or Register to comment.