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:-