A continuation of the owntracks discussion from beta

@napalmcsr

Actually, there is already a write-up in the community on it. It’s using different elements from mine, but mostly the same.

I don’t use HiveMQ and @RRodman helped me with tweaking some of the parameters on my phones so that I was a bit more granular. You can find that information on the owntracks documentation. But, I will warn you that the documentation is confusing.
The simple answer is to install owntracks on your phone. Decide what you want to use to connect to your ip address. HiveMQ is encrypted (I think). I used duckdns. All in all you direct whatever you use to your ip address and port forward that to your mqtt server so that you can get the topic from there. Make sure that you have a password set up on your mqtt server. Also make sure that you set up a password to encrypt your location data in the owntracks node and under advanced in the preferences. The owntracks node is simply a passphrase that you plug in to your phone as well. You can find information on all of the owntracks settings in the documentation. Don’t try their tutorial. It’s confusing. Just move through the settings and figure out what works for you. I use this in conjunction with wifi presence to determine that we’re home or away for modes and automations. Here’s the flow minus my owntracks data. In the end of this flow, I set up ping to start pinging my phone on the network until it reports false when owntracks reports away so that it reports quickly as it can sometimes take a few minutes to get reported through unifi. It shows me away before I’m a block away. If anything is going to get left open, I’ll know about it before I’m down the road.

[{"id":"fbe411534cd57d36","type":"subflow","name":"Ping Status","info":"","category":"","in":[{"x":40,"y":140,"wires":[{"id":"da3e45d275aeb5dd"},{"id":"3147d2f356c74496"},{"id":"639e734e5e945a1f"}]}],"out":[{"x":1140,"y":200,"wires":[{"id":"404ab9a4eff10c45","port":"0"},{"id":"6addbc0af6da3997","port":"1"}]}],"env":[{"name":"ip","type":"str","value":"127.0.0.1"}],"meta":{},"color":"#DDAA99","status":{"x":700,"y":260,"wires":[{"id":"8b557b207283261a","port":0}]}},{"id":"be981b11edd798c0","type":"bigssh","z":"fbe411534cd57d36","name":"","commandLine":"ping -W 1 -c 1 ","commandArgs":"","minError":"2","minWarning":1,"noStdin":true,"format":"ascii","payloadIsArg":true,"myssh":"5c03f08e973d95fe","x":340,"y":200,"wires":[["8b557b207283261a"],[],[]]},{"id":"8b557b207283261a","type":"function","z":"fbe411534cd57d36","name":"Ping Phone Presence","func":"if(msg.payload.indexOf(\"ping statistics\") >= 0) {\n   msg.payload = msg.payload.indexOf(\"100% packet loss\") < 0;\n   node.status({fill:\"green\", shape:\"dot\", text:msg.payload});\n    return msg\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":200,"wires":[["404ab9a4eff10c45"]]},{"id":"404ab9a4eff10c45","type":"switch","z":"fbe411534cd57d36","name":"","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":730,"y":200,"wires":[[],["6addbc0af6da3997"]]},{"id":"3147d2f356c74496","type":"stoptimer","z":"fbe411534cd57d36","duration":"1","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":180,"y":80,"wires":[["508beb86a3062ce7"],[]]},{"id":"f881b94cd7c6620e","type":"delay","z":"fbe411534cd57d36","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":700,"y":100,"wires":[["da3e45d275aeb5dd"]]},{"id":"639e734e5e945a1f","type":"change","z":"fbe411534cd57d36","name":"","rules":[{"t":"set","p":"#:(ram)::withinMinute","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":40,"wires":[[]]},{"id":"508beb86a3062ce7","type":"change","z":"fbe411534cd57d36","name":"","rules":[{"t":"set","p":"#:(ram)::withinMinute","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":80,"wires":[[]]},{"id":"6addbc0af6da3997","type":"switch","z":"fbe411534cd57d36","name":"","property":"#:(ram)::withinMinute","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":910,"y":220,"wires":[["f881b94cd7c6620e"],[]]},{"id":"da3e45d275aeb5dd","type":"change","z":"fbe411534cd57d36","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"ip","tot":"env"},{"t":"delete","p":"reset","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":140,"wires":[["be981b11edd798c0"]]},{"id":"5c03f08e973d95fe","type":"SSH_Credentials","host":"192.168.1.84","port":"22","userlabel":"[email protected]"},{"id":"ab06d8e4a11e79ef","type":"rbe","z":"b12af926a1d7a0d3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":770,"y":860,"wires":[["6345fcf2b0f72206"]]},{"id":"833c80770b1c4a66","type":"rbe","z":"b12af926a1d7a0d3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","x":770,"y":900,"wires":[["84093091bc2dd378"]]},{"id":"d803899836e87520","type":"change","z":"b12af926a1d7a0d3","name":"JeffUnifi","rules":[{"t":"delete","p":"#:(file)::JeffUnifiPresence","pt":"global"},{"t":"set","p":"#:(file)::JeffUnifiPresence","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140,"y":900,"wires":[["ef0502beacbcc5c5"]]},{"id":"f9622ecb22d2f655","type":"change","z":"b12af926a1d7a0d3","name":"AprilUnifi","rules":[{"t":"delete","p":"#:(file)::AprilUnifiPresence","pt":"global"},{"t":"set","p":"#:(file)::AprilUnifiPresence","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140,"y":860,"wires":[["788b25061616a301"]]},{"id":"3e7b631378dffafe","type":"function","z":"b12af926a1d7a0d3","name":".event->payload","func":"return {\n    payload:msg.payload.event,\n            }\n;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":800,"wires":[["a43b8b09ce7d0a72"]]},{"id":"a43b8b09ce7d0a72","type":"rbe","z":"b12af926a1d7a0d3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":870,"y":800,"wires":[["96b1614980160a31"]]},{"id":"96b1614980160a31","type":"change","z":"b12af926a1d7a0d3","name":"enter->home, leave->away","rules":[{"t":"change","p":"payload","pt":"msg","from":"enter","fromt":"str","to":"home","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"leave","fromt":"str","to":"away","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":800,"wires":[["2a9b83c8520f761f"]]},{"id":"2a9b83c8520f761f","type":"change","z":"b12af926a1d7a0d3","name":"Set Global April OwnTracks ","rules":[{"t":"set","p":"OwnTrxA","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1320,"y":800,"wires":[["9b9a5c65aefbc964"]]},{"id":"c2a11c0c011eb9d1","type":"function","z":"b12af926a1d7a0d3","name":".event->payload","func":"return {\n    payload:msg.payload.event,\n            }\n;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":960,"wires":[["ca271af3f2c397a1"]]},{"id":"ca271af3f2c397a1","type":"rbe","z":"b12af926a1d7a0d3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","x":870,"y":960,"wires":[["90cffdd2139409a2"]]},{"id":"90cffdd2139409a2","type":"change","z":"b12af926a1d7a0d3","name":"enter->home, leave->away","rules":[{"t":"change","p":"payload","pt":"msg","from":"enter","fromt":"str","to":"home","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"leave","fromt":"str","to":"away","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":960,"wires":[["6cf082cea16ca1f4"]]},{"id":"6cf082cea16ca1f4","type":"change","z":"b12af926a1d7a0d3","name":"Set Global Jeff OwnTracks ","rules":[{"t":"set","p":"OwnTrxJ","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1320,"y":960,"wires":[["eadbf663a61ff970"]]},{"id":"84093091bc2dd378","type":"change","z":"b12af926a1d7a0d3","name":"false->away,true->home","rules":[{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"away","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"home","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":900,"wires":[["d803899836e87520"]]},{"id":"6345fcf2b0f72206","type":"change","z":"b12af926a1d7a0d3","name":"false->away,true->home","rules":[{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"away","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"home","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":860,"wires":[["f9622ecb22d2f655"]]},{"id":"788b25061616a301","type":"switch","z":"b12af926a1d7a0d3","name":"Unifi Status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"home","vt":"str"},{"t":"eq","v":"away","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1310,"y":860,"wires":[["89506f783618bfcb"],["0db8b1139d67f19c"]]},{"id":"ef0502beacbcc5c5","type":"switch","z":"b12af926a1d7a0d3","name":"Unifi Status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"away","vt":"str"},{"t":"eq","v":"home","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1310,"y":900,"wires":[["b0371dfb6a0936b3"],["f587d720b37fba06"]]},{"id":"0db8b1139d67f19c","type":"switch","z":"b12af926a1d7a0d3","name":"OwnTracks Away","property":"OwnTrxA","propertyType":"global","rules":[{"t":"eq","v":"away","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1530,"y":860,"wires":[["4ed32763e9b82af8"]]},{"id":"b0371dfb6a0936b3","type":"switch","z":"b12af926a1d7a0d3","name":"OwnTracks Away","property":"OwnTrxJ","propertyType":"global","rules":[{"t":"eq","v":"away","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1530,"y":900,"wires":[["13fd98cf850e9d6e"]]},{"id":"eadbf663a61ff970","type":"switch","z":"b12af926a1d7a0d3","name":"Owntracks Status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"home","vt":"str"},{"t":"eq","v":"away","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1550,"y":960,"wires":[["f587d720b37fba06"],["5b82a0c3558de5bf","bd929be7b6494f18"]]},{"id":"5b82a0c3558de5bf","type":"switch","z":"b12af926a1d7a0d3","name":"Unifi away","property":"#:(file)::JeffUnifiPresence","propertyType":"global","rules":[{"t":"eq","v":"away","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1750,"y":980,"wires":[["13fd98cf850e9d6e"]]},{"id":"3de93beaabdeb22a","type":"switch","z":"b12af926a1d7a0d3","name":"Unifi away","property":"AprilUnifiPresence","propertyType":"global","rules":[{"t":"eq","v":"away","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1730,"y":760,"wires":[["4ed32763e9b82af8"]]},{"id":"9b9a5c65aefbc964","type":"switch","z":"b12af926a1d7a0d3","name":"Owntracks Status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"away","vt":"str"},{"t":"eq","v":"home","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1550,"y":800,"wires":[["242e7b81f2f1e12d","3de93beaabdeb22a"],["89506f783618bfcb"]]},{"id":"4ed32763e9b82af8","type":"change","z":"b12af926a1d7a0d3","name":"set away","rules":[{"t":"set","p":"payload","pt":"msg","to":"away","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1920,"y":860,"wires":[["e6455681d16f57fe"]]},{"id":"89506f783618bfcb","type":"change","z":"b12af926a1d7a0d3","name":"set home","rules":[{"t":"set","p":"payload","pt":"msg","to":"home","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1920,"y":820,"wires":[["e6455681d16f57fe"]]},{"id":"e6455681d16f57fe","type":"rbe","z":"b12af926a1d7a0d3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","x":2090,"y":840,"wires":[["688d1b5a271f12e6"]]},{"id":"13fd98cf850e9d6e","type":"change","z":"b12af926a1d7a0d3","name":"set away","rules":[{"t":"set","p":"payload","pt":"msg","to":"away","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1920,"y":900,"wires":[["c4a87534bc70f858"]]},{"id":"f587d720b37fba06","type":"change","z":"b12af926a1d7a0d3","name":"set home","rules":[{"t":"set","p":"payload","pt":"msg","to":"home","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1920,"y":940,"wires":[["c4a87534bc70f858"]]},{"id":"c4a87534bc70f858","type":"rbe","z":"b12af926a1d7a0d3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","x":2090,"y":920,"wires":[["a80b099909ed0415"]]},{"id":"688d1b5a271f12e6","type":"change","z":"b12af926a1d7a0d3","name":"Set April Home Status","rules":[{"t":"set","p":"AprilStatus","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2260,"y":840,"wires":[["df9480405e03fb25"]]},{"id":"a80b099909ed0415","type":"change","z":"b12af926a1d7a0d3","name":"Set Jeff Home Status","rules":[{"t":"set","p":"JeffStatus","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2260,"y":920,"wires":[["b656fc6a70fc5679"]]},{"id":"cbfb957730f8eefd","type":"switch","z":"b12af926a1d7a0d3","name":"","property":"payload.desc","propertyType":"msg","rules":[{"t":"eq","v":"Home","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":570,"y":800,"wires":[["3e7b631378dffafe"]]},{"id":"bb2f3801924ba9d4","type":"switch","z":"b12af926a1d7a0d3","name":"","property":"payload.desc","propertyType":"msg","rules":[{"t":"eq","v":"Home","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":570,"y":960,"wires":[["c2a11c0c011eb9d1"]]},{"id":"242e7b81f2f1e12d","type":"subflow:fbe411534cd57d36","z":"b12af926a1d7a0d3","name":"April","env":[{"name":"ip","value":"192.168.1.179","type":"str"}],"x":1590,"y":700,"wires":[["10e021319c0c90aa"]]},{"id":"bd929be7b6494f18","type":"subflow:fbe411534cd57d36","z":"b12af926a1d7a0d3","name":"Jeff","env":[{"name":"ip","value":"192.168.1.49","type":"str"}],"x":1590,"y":1060,"wires":[["e859a1ac67b6eac5"]]},{"id":"10e021319c0c90aa","type":"switch","z":"b12af926a1d7a0d3","name":"PingAway","property":"payload","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":1740,"y":700,"wires":[["4ed32763e9b82af8"]]},{"id":"e859a1ac67b6eac5","type":"switch","z":"b12af926a1d7a0d3","name":"PingAway","property":"payload","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":1740,"y":1060,"wires":[["13fd98cf850e9d6e"]]}]

This is an example of the flow that I use to filter connected devices from my unifi. I’ve replaced the real data. you’ll need to connect that in the center where each filter node is. if you use unifi, then grab those nodes and get active devices. If you don’t have unifi, there’s a way to access the tables for the same information, but I don’t have a way to show that, so google will be your friend. Make sure that when your phone is connected to wifi at home, you are using the real mac address and not a random one. You can find that in the connection settings of your phone for that specific connection.

[{"id":"673101c535d11972","type":"function","z":"b12af926a1d7a0d3","name":"Check Presence","func":"const lastSeenSeconds = 20;\nlet presenceCutoff = (new Date() - (lastSeenSeconds * 1000)) / 1000; \nconst people = {\n    \"presence/Her\": \"30:14:67:23:b2:db\",\n    \"presence/Him\": \"30:74:17:b5:3a:29\",\n};\n\nreturn Object.keys(people).map(function(topic) {\n    //let devices = msg.payload[0].filter(device => device.name === people[topic] && device.last_seen > presenceCutoff);\n    let devices = msg.payload[0].filter(device => device.mac === people[topic]);\n    return {\n      topic: topic,\n      retain: true,\n      payload: devices.length > 0\n    };\n});","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":1100,"wires":[[],[]]}]

Even though I’ve posted this out here, you’ll probably still find some confusion in this. If you run into anything, post it here and we’ll help. If you have questions, others probably have the same.

3 Likes

Why do you use both Unifi and OwnTracks? I use only OT and it is solid. And why use Ping at all since it is so unreliable?

Is not unreliable. It makes it check in when I’m on the move. Otherwise unifi doesn’t lose the connection quickly enough. And I want two methods to check in for automations. If it floats then there is a second check

1 Like

Yeah, I am wanting to figure off how to get owntracks to my local mosquito mqtt.
What I am trying to figure out since the documents is sparse…

  1. Add owntracks to mqtt. (Not a Linux person)
  2. Get my mqtt exposed to the world. (Scares me)
  3. Get owntracks to talk to my mqtt. (Seems easy)
  4. (Easier) create node red stuff.

Craig

Many thanks to @april.brandt for helping me get this setup!

1 Like

Rightly so, easy to get MQTT setup the wrong way…

I will begin by saying that on CORE all you need to do is create an MQTT user.

First a NOTICE: This is not a complete guide to setting up Mosquitto, I write this from memory and have NOT tested this setup, please ask if there’s anything not clear or if something doesn’t work as expected.

For a vanilla Mosquitto install you would need to do the following:

  1. Make sure you have a directory named /etc/mosquitto/conf.d, if you don’t already have a file in that directory, create one (if you do, change the current one), eg “10-passwordrequired.conf” and put the following content in there:
listener 1883
allow_anonymous false
socket_domain ipv4
acl_file /etc/mosquitto/default-aclexternal
password_file /etc/mosquitto/default-pwdexternal

The full set of mosquitto.conf options can be found here.

If your distro doesn’t use /etc/mosquitto/conf.d, this is how /etc/mosquitto/mosquitto.conf looks in Debian (change yours to look the same, at least the include_dir part):

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d
  1. Create a BASIC ACL file (/etc/mosquitto/default-aclexternal):
# By default let all users have full acess once authenticated
pattern readwrite #

This is NOT a buttoned-down ACL, but it is good to start with to make sure things are working.

  1. Create the username and password file:
# Run the following if the password file doesn't exist yet:
sudo touch /etc/mosquitto/default-pwdexternal
# To create a user run (replace yourmqttusername with the username you want to use):
sudo mosquitto_passwd /etc/mosquitto/default-pwdexternal yourmqttusername
  1. Restart Mosquitto

You should now have a working and secure-(ish) setup of Mosquitto, setup this way anonymous access is disabled even from your local network, so do keep that in mind. There are ways to split the config to allow local network to be anonymous while requiring a username and password for external access. The above was to get a minimal and functional setup to run and is a good starting point.

2 Likes

Or … they could just wait for CORE. We’ve done this for you. So, Thank YOU @napalmcsr for inspiring us to look at the security of this automation. Now it’s a better and more secure integration. More to come when you get your CORE.

2 Likes

Is there a way to use owntracks and mqtt without exposing ports? I have mqtt setup already, but am not comfortable with exposing any ports.

Without port forwarding the route described by @jchurch would be the way to go until you get your CORE:

Owntracks GPS Presence in Node-RED - Automation Central / Automation Distribution - Oh-La LABS Community

Yes. I have OpenVPN set up and when I leave home I connect my phone to my firewall over cell data and I then have complete access to every-damn-thing at home, just like when I’m sitting in my easy chair. It’s still “exposing a port” but, unlike MQTT, it’s a secure path.