I have be thinking about a reliable method of presence detection for a while now, I have found the usual methods of router, ping or owntracks pretty unreliable.
I am now trialling a method using MQTT with the tasker android app. Tasker is a very powerfull event triggered automation application for android.
What it is doing is sending a MQTT message to your broker on connect and disconnect of your local wifi network.
You will need a MQTT broker that is externally accessible. Mine is setup to accept websocket connections that are proxied through a nginx reverse proxy via https (port 443), if you want to know about that setup I will have to do a whole new post.
You need to install (buy) Tasker and a tasker MQTT plugin, links below.
First thing you need to do is setup the MQTT Tasker plugin, it is a separate app to tasker.
Just press the 3 dots on the top right of the MQTT Tasker plugin app and select âSettingsâ. Then select âServerâ.
In there setup the server settings for your mqqt server. Remember your mqtt server must be externally accessible. SSL and password protected highly recommended.
In Profiles you need to select the + at the bottom right.
Then choose âStateâ
Then choose âNetâ
Then choose âWifi Connectedâ
In the âSSIDâ field enter your home wifi ssid. This will then only trigger on the state for this wifi SSID only.
Then go back by selecting the back arrow on the top left.
A new task menu should open.
Choose the â+â to create a new task
Name the task. I used âMqtt Wifi Connectedâ
In the task edit select â+â
Choose âPluginâ and select âMQTT Clientâ
In MQTT Client select the pen icon right of âConfigurationâ
Enter your MQTT Topic, Message and QOS setting you wish to use. I used âoccupancy/homewifiâ as the topic and âConnectedâ as the message, and QOS as 1.
Select the back arrow at the top left to leave the MQTT plugin settings
Then select the back arrow again to leave the âAction Editâ
Then select the back arrow to leave the âTask Editâ
You should be back at the 'Profiles" section
Press and hold the green arrow on the profile you created (if you do not see the arrow just select the profile and the items should show below it), a menu should pop up, select the âAdd Exit Taskâ item. The exit task will fire when the Wifi Connected state changes from True to False (connected to disconnected)
Here you are going to do almost the same again as the âMQTT Wifi Connectedâ task you created previously but it will be the wifi disconnect event you are triggering on.
Select âNew Taskâ
Name it, I used âMQTT Wifi Disconnectedâ
Add an action by selecting â+â
Choose âPluginâ and select âMQTT Clientâ
In MQTT Client select the pen icon right of âConfigurationâ
Enter your MQTT Topic, Message and QOS setting you wish to use. I used âoccupancy/homewifiâ as the topic and âDisconnectedâ as the message, and QOS as 1.
Select the back arrow at the top left to leave the MQTT plugin settings
Then select the back arrow again to leave the âAction Editâ
Then select the back arrow to leave the âTask Editâ
You should be back at the 'Profiles" section and it all should run
I have my phone turn on data temporarily and send a HTTP POST message that Iâve left. Otherwise the server will never get the disconnected MQTT message
I suppose I should have qualified that you will need celular (3g) data on your phone for the disconnect message to get through. I made the assumption that if you had an android phone then you would have data outside your wifi network.
France is lucky to have unlimited data. Places Iâve lived not so much so you have to be a bit stingey with it!
I also use your method to post battery info, if Iâm receiving a phone call or not, alarm info etc to Home Assistant so it opens a lot of potential automation scenarios.
Good work writing up this tutorial. Really well done!
Iâve been having issues with Tasker+MQTT lately, but it looks like you recommended a different plugin than the one Iâm using. Thanks for the write up, Iâm going to give it a shot!
Right now, I use something similar to your method to set my status to âsleepingâ when I plug my phone in between certain hours. HA triggers some scenes, etc when it detects that.
In my case, Iâm trying to get Tasker to report the battery level via MQTT ever 5 minutes, but after 2 or 3 messages, Tasker stops publishing them. Did you not run into that problem when the tablet is in sleep mode?
I have some led strips that Iâve connected to HA through Wemos D1-minis and mqtt.
Using tasker I set a profile for âAlarm Doneâ and then have a task that starts sending messages over mqtt to slowly increase the brightness of the leds. By the time snooze is over (6 mins) the lights have reached full brightness. If you get up before snooze fires thereâs a tasker task to set the lights to max and then turn them off (to kill the prior task as itâs running in a loop).
My server setup is relatively complicated. Everything runs on one computer (host) all in separate docker containers. Including the ngix web server for the reverse proxy. The reverse proxy basically sends the web traffic to my different docker containers depending on the incoming DNS name.
Each docker container responds to is unique port from the same host IP. Docer forwards requests to the internal IP and port of the docker container. i.e.
Host IP is 192.168.178.58
Docker containers run in 172.17.0.x
Request to 192.168.178.58:1884 gets forwarded internally to 172.17.0.2:1883
Docker networking is effectively like setting up port forwarding on your home router.
My router is configured to forward port 443 to my inside computer (docker host) and then on to my ngix reverse proxy docker container
So to try and explain better from an outside my network point of view when you add the ngix reverse proxy:-