Another presence method - Android specific

Tags: #<Tag:0x00007f616b9cb700> #<Tag:0x00007f616b9cb5c0> #<Tag:0x00007f616b9cb480>

Android as it requires tasker -
Telegram account -
Telegram-cli -

So a little back story, for a few months now I’ve been helping a co-worker with his openHAB implementation, and like most Android phones he had some phones switch to newer versions of Android causing a lot of problems with being properly identified when he or his family was home (also lots of random lights ON/OFF events).
Like me he has a Raspberry Pi and I shared the python bluetooth detection scripts that I use on my Pi and don’t have issues; however while this helped it still was not a stable solution for him.

Back in Sept I posted how you can use keywords/codes in a telegram message to a group chat or a saved message; reference Controlling OpenHAB by Telegram?. This will require two scripts and a custom virtual switch to represent your phone.

Fantastic, but get to the point!

As I had also talked my co-worker into purchasing tasker for his phone I had the idea to use tasker to send a message via telegram with keywords based on him being connected on his home WiFi.

  1. Within tasker:
    Set a state profile and search for Wifi Connected
    Put in your home SSID (case sensitive)
  2. Add a task
    Setup a variable for your chat ID and other long character values that telegram requires.
    Setup a HTTP Post action and use the following blog to complete the task (covers the variable setup too, which can be optional) -
  • Alternative
  1. Add a task
    Setup a HTTP POST action and set the server port to your
    Call your REST API item in the Path
    For Data / File the command supported by that item (if a light switch ON or OFF)
  • End Alternative
  1. Duplicate the task you created in step 2 and name it differently.
    Change the text value to the other keyword you setup within telegram-cli and add this to the profile you setup for the WiFi connection as an Exit task. Also put a wait in place on the exit task, so your phone has enough time to flip on the mobile data radio (perhaps even a ping to something on the Internet to ensure you have connectivity) or tasker will throw a error, basically stating it can’t do the HTTP Post as you aren’t connected to the Internet.

You should have two tasks assigned to the profile, the first will send the telegram command to tell openHAB you have arrived home, the other task is used for when you leave your WiFi and should change your status to OFF.
Also keep in mind you will need to disable any items that are configured for network pings or bluetooth detection as the only trigger should be tasker.

That should be all that is needed to start using this method to inform openHAB if you are home or away.

1 Like

Thanks for the tutorial.

Seems to me though that you could skip the whole dependence on Telegram and have Tasker just command the Item directly through the REST API. Since you more than likely already have my setup you can post and put through that address.

Also, you don’t necessarily need to disable the other presence sensors if you use something like Generic Presence Detection to aggregate multiple sensors, though if this approach is reliable enough you wouldn’t need them any more anyway.

1 Like

sounds just right (I would also skip Telegram and use the REST API) - but I would guess, this would increase battery drain and decrease the available mobile time? Is there some significant change in that?

Tasker will only do something when it sees the wifi near or when you move away from the wifi. So from a battery perspective, you need to have wifi on all the time to make this work. Many do this anyway to get better geolocation without the worse battery drain of running the GPS all the time and it provides more precise geolocation.

Tasker itself is reactive, not polling for state changes so it does not have a significant impact on battery life in my experience, at least when measured by apps like Greenify.

You get nothing for free, but in this case I don’t think the cost is all that much.

1 Like

Excellent point, neither my co-worker or I have setup a configuration (he might though as he picked up an echo dot). We leveraged telegram as we both leverage it for notifications when we are away from home, so in his case it was nothing additional to setup.

From a network security perspective though (former CCSP, mainly Cisco ASA work) the option requires ports to be opened from the Internet to some part of an internal network; telegram-cli listens on your account and in most scenarios routers/firewalls always allow outbound access unless changed not to do so.
Some other factors in my co-worker’s case is his ATT provider DSL/router combo box had to be factory reset a couple months ago as it wouldn’t let us save any changes, so the telegram method just happened to be easier at this point in time.

We leverage the detection rule as he has a habit of plagiarising information from my openhab, a few things are tied to a group switch for his phone virtual switch along with his wife’s phone virtual switch. What we disabled was the network thing/item and the bluetooth item as it was failing to detect the phones correctly and flipping switches to an OFF status.

I’m going to edit the original post and add an alternative to mention with a REST API call, so future readers aren’t having to scroll through the conversations.

As @rlkoshak mentioned tasker itself is not a battery or network usage hog; its an app that allows you to define conditions and take actions if those conditions are met (similar to openHAB rules).

If you are concerned about mobile time and tasker sending HTTP POST commands, you can have your task first enable your mobile data, run the HTTP POST, disable mobile data (I would also throw in some wait times to give your phone enough time to bring mobile data online). Also keep in mind that newer versions of Android prevent access to enabling/disabling mobile data without having root access, so you would have to rely on Auto Input to handle the mobile data enabling/disabling.

This is completely incorrect. You install an add-on which establishes a connection to the server and proxies OH’s REST API. The whole point is to NOT have to open a port on your firewall. I’m a current CISSP btw and use and promote my’s use.

If you don’t want to trust the openHAB foundation’s servers, you can deploy your own instance of on AWS or something. But there is still no need to open a port.

In short, it works the same as telegram firm a network security perspective.

REST calls to are just standard HTTP PUT, POST, and GET so it should work exactly the same as telegram from that perspective as well. The only real difference is instead of an API key you have to use basic auth in the request. I can see how that might be a deal killer for some.

Well snap, once again @rlkoshak you have educated me…were open ports needed at one point, or has the cloud add-on always been an option?

This info also comes at a great time as I have another co-worker who isn’t technical and I won’t have the option of telegram-cli and tasker.

I will run a test with my tasker when I get this second co-worker setup with, currently I haven’t been successful with running telegram-cli as a service, so if putty dies so do the remote commands.

It has been an option for as long as I’ve been using OH, at least as long as version 1.7, though it has gone through some name changes along the way.

This post might be useful.

You can still go the “direct” way of opening up ports. That is always an option :slight_smile:
As we all agree, a service like myopenHAB offers various advantages (and brings a few restrictions) in comparison to the port forwarding option.

1 Like

Personally I would deal with a few restrictions vs having some jokester connecting over a open port and do things with my setup, while an annoyance it would be small potatoes compared to the wrath of my wife! :wink:

I’ve been using Tasker for presence detection for well over a year. It mostly works well, except that my router has a bug where it won’t redirect my domain to my internal network. I have a few profiles on Tasker to correct the issue.

  1. Home - This is location based. It sets a variable in Tasker to True/False based on my phone’s location.
  2. WiFi SSID - This compares the current SSID (or lack of) to my home SSID and sets a variable to True/False accordingly.

When “Home” changes, it triggers several tasks to run. One sends an update to OH via the Rest API. If Home is set to True and SSID is set to True, then it turns a switch in OH to ON via the local IP. If Home is set to False and SSID is set to False, then it turns the same switch to OFF via the domain name. (And various combinations of those values, accordingly.)

I tried several other options using bindings and rules in OH, but they were constantly wrong. Since I switched to using Tasker, it has worked very well.