Presence detection via Tasker and MQTT (Android only)

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.

  1. Just press the 3 dots on the top right of the MQTT Tasker plugin app and select ‘Settings’. Then select ‘Server’.
  2. In there setup the server settings for your mqqt server. Remember your mqtt server must be externally accessible. SSL and password protected highly recommended.

Next is the tasker setup:

  1. In Profiles you need to select the + at the bottom right.

  2. Then choose ‘State’

  3. Then choose ‘Net’

  4. Then choose ‘Wifi Connected’

  5. In the ‘SSID’ field enter your home wifi ssid. This will then only trigger on the state for this wifi SSID only.

  6. Then go back by selecting the back arrow on the top left.

  7. A new task menu should open.

  8. Choose the ‘+’ to create a new task

  9. Name the task. I used ‘Mqtt Wifi Connected’

  10. In the task edit select ‘+’

  11. Choose ‘Plugin’ and select ‘MQTT Client’

  12. In MQTT Client select the pen icon right of ‘Configuration’

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

  14. Select the back arrow at the top left to leave the MQTT plugin settings

  15. Then select the back arrow again to leave the ‘Action Edit’

  16. Then select the back arrow to leave the ‘Task Edit’

  17. You should be back at the 'Profiles" section

  18. 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)

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

  20. Select ‘New Task’

  21. Name it, I used ‘MQTT Wifi Disconnected’

  22. Add an action by selecting ‘+’

  23. Choose ‘Plugin’ and select ‘MQTT Client’

  24. In MQTT Client select the pen icon right of ‘Configuration’

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

  26. Select the back arrow at the top left to leave the MQTT plugin settings

  27. Then select the back arrow again to leave the ‘Action Edit’

  28. Then select the back arrow to leave the ‘Task Edit’

You should be back at the 'Profiles" section and it all should run

To debug you can enable a run log:

  1. To do this select the 3 dots at the top right and choose ‘More’ then ‘Run Log’
  2. Just turn it on and then you will see a log of all tasks.
  3. Then just turn your wifi on and off to see the events being triggered.

Anyway this is just another method to possibly use to add to your presence detection options. I hope this helps someone.

Here is an XML that can be used to import into tasker to ease the setup.
https://gist.github.com/coza73/60c6b602fc28c8244398dda4c4564e45

8 Likes

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.

1 Like

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!

1 Like

Lets be honest here. Android without Tasker is a waste. HA/MQTT/Tasker is a glorious union.

Nice use of Tasker. I currently have the morning alarms fire off lights over MQTT.

2 Likes

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.

Yes, please!!! 6

2 Likes

Does the android phone with tasker need to be rooted?

That’s my problem. I use duckdns.org with letsencypt and works fine for HASS, but I don’t know how to do it for my mqtt server

I also use 3 different WiFi networks in my house (6 if you count 2,4 and 5). I named each differently

How you do this ?

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

thanks for the tip

No the app does not need the phone to be rooted

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

I have a letsencrypt certificate for each domain name in the nginx docker container.

Clear as mud?

1 Like

coza73 did you use a more automations with tasker? if yes please share.

Just started to utilize tasker in my automation’s so no, no others yet.

1 Like

Why mosquito double passage?

You use duckdns.org?

In letsencypt how you choose sub domains?

Like
mynane-mosquitto.duckdns.org
Abd
mynane-hass.ducjdns.org

Or?