⚙️ Using Tasker with Home Assistant (V2)

Thanks Giovanni for the advice! I suspect my problem with Tasker is that the Android 4.4.2 version is just too old for the app. I also tried Automagic on this same phone and it also dies with the error javax.net.ssl.SSLException: Connection closed by peer when trying to connect to HA. However, Automagic was able to detect an alert message and auto-open an app. This further implicates an SSL connectivity issue with Android 4.

In any case, I don’t want the automation to be relying on my personal phone. It has to be done on a dedicated phone or tablet which is why I’m attempting to use an ancient Galaxy S3. My next step will be to head over to XDA developers and try to install a custom Android 7 ROM .

The flow is correct. Json is formatted in the third block. It must work as it is.
Check if tou have api enabled in your HA config
https://developers.home-assistant.io/docs/en/external_api_rest.html
Add api string

Maybe you can find a suitable Tasker version here: https://tasker.joaoapps.com/download.html

Thanks @osmoen ! I only found the paid version on GPlay and had no idea there was not only a version suitable for the old Kitkat, but a trial too. I will definitely give this a try as I have had no luck with Automagic authenticating to HA yet.

I have this entry in my configuration.yaml. It is related to api too. You can check if you have that too. I’m speaking about default config row

Giovanni, We should take all further discussion of AutoMagic issues to your initial post and not hijack this Tasker thread. I will be deleting a few of my posts here and moving them to Automagic & Home Assistant (plus NFC automation example) You might also want to delete your last few replies, but I will answer them in the other thread.

Thanks again for all your assistance!

I did try a Tasker version that was supposed to work for KitKat (Android 4.4x), but unfortunately it failed with the same network error as the latest version. Not a dead-end since I will next try installing a custom ROM to bring the old phone up to Android 7.

EDIT: The network error was due to an SSL handshake incompatibility between Home Assistant and the Android 4.4 client. Instead of rebuilding the client - this configuration.yaml http: option solved the server connection error:

http:
  ssl_profile: intermediate

However, importing the XML templates still crashes Tasker. I was able to work around this by using this Tasker plugin.

Been using this setup for a while now with a couple of tasks to switch stuff in HA from buttons on my phone.

I recently tried to set up a switch to be to turned on every 15mins (it runs a automation that then turns the switch back off)

if i run the task by using the “play button” in the task I have never seen in fail and work great.

When i add it to a Time profile to trigger every 15mins then I keep getting the error

java.net.ConnectionException: Failed to connect to …

and the switch does not toggle.

Any ideas

Edit: Some additional testing has shown that it only fails when the phone screen if off. It still works if the phone is locked but the screen is kept on.

Is there a way to add Tasker variables to the service data (%par2) string? I have AutoVoice set up to change the temperature of my thermostat and create a variable of the temperature as %desiredtemp. In %par2 I have:
{"entity_id": "climate.main_floor", "temperature": %desiredtemp} and that gives me an error.
If I put:
{"entity_id": "climate.main_floor", "temperature": 20} it works fine.

How do I allow it to change the number with a variable so I don’t need a profile for every possible temperature change?

Thanks!

Are you sure %desiredtemp is well defined?
Can you use the Flash action to display the whole body before sending it to home assistant?

Also, have you checked the error log of home assistant?

1 Like

@bokub Thanks for this, its really handy. One note though, when I imported your tasks I noticed that you have the collision handling on these tasks set to the default which is Abort new task. Since these are very generic tasks likely to be called all over from Tasker and don’t attempt to modify any global variables I’d recommend changing that to Run Both Together. Otherwise if two different tasks attempt to do a call-service simulataneously one will just be aborted and lost without the user knowing the call got dropped.

Also I needed a make use of a few more tasks for APIs in my own Tasker so I expanded the list of tasks to include HA create-event and HA set-state. They’re pretty straightforward, %par1 is the name of the event or sensor (for create-event and set-state respectively) and %par2 is the body of the API call.

I also made an HA get-state for convenience. It doesn’t do anything you couldn’t do with HA Template its just a bit easier if you only need the state or one attribute of something. %par1 is the name of the entity you want and %par2 is the name of the field or attribute you want. Note that this one does depend on AutoTools for its JSON Read functionality which may make it less useful to people. It could be pretty easily adjusted to ignore %par2 and just return the JSON and then have no dependency on AutoTools.

One other note, since there’s 5 of these now I refactored error handling into a common task I called HA API Error so anyone using my tasks will need that as well. I tweaked your HA call-service and HA template to switch the collision handling mode and use my common error handling task.

Anyway just wanted to share, thanks again.

[EDIT] Oh I almost forgot, I added one other convenience to HA call service in my copy. If par2 does not start with { (and therefore isn’t JSON) then assume it is an entity_id and set the body to {"entity_id":"%par2"}. Seemed to simplify a few common service calls for me.

2 Likes

Ah, rookie mistake.
My regex was pulling out “20 degrees” instead of just 20.

Thanks for suggesting to use Flash, that made it extremely obvious where the mistake was.

Woah, you took it to another level, for sure!

I’ve edited my tasks so they have the Run Both Together option selected. Thanks for your suggestion :+1:

About your create-event and set-state tasks, I personally never use these features of home assistant, and I preferred to keep it simple, but it’s great that you could adapt this tutorial to your own needs!

1 Like

Yea those two are definitely way less common, I’m sure most people don’t need them.

create-event I use basically as a way to trigger a Node-RED flow from tasker. I do most of my automation in Node RED but I prefer to have everything external go through HA rather then directly to Node-RED HTTP In nodes. I also generally prefer to have all those flows be state driven but there are a few exceptions I can’t avoid.
For example, the Nest x Yale smart lock has no way to integrate with HA at all (don’t buy this thing, learn from my mistake haha). So as a workaround I settled on using AutoNotification to look for the “Door unlocked” notifications and create a door_unlocked event that Node-RED can then use to trigger “someone we know besides us used a code to enter our home” automation.

set-state I only use for one thing, I have my phone report in to HA with its basic state. There’s a sensor in HA with basic details of my phone like is power connected, is bluetooth turned on, is wifi turned on, what’s the battery level, is my phone connected to my car, etc. Everytime a state of my phone I’m interested in changes it reports in to HA and updates this sensor.
I was doing this mostly to workaround issues with the HA android app since it reported only a limited amount of state and was very slow to react to changes (like plugging in the battery charger). That’s been improving quickly though so this one may go away soon. I’d definitely prefer to just let the HA app handle this.

Hi, How can I use this to kill a process on android in case of an automation is triggered in HA? Thank you

So this tutorial is really all about getting Tasker to be able to talk to HA. It doesn’t include anything for having HA talk back to tasker though.

I do have this capability on mine though, I use it for a few things. The easiest way to do it is to get the plugin called AutoRemote. Essentially what that plugin provides is a URL that you can then use to start a tasker task. And since Tasker has a “Kill App” action available for tasks that should give you what you want.

To get this set up, after downloading AutoRemote follow the directions in app to get your personal AutoRemote URL. Then you can make a RESTful command service like this:

rest_command:
  autoremote: 
    method: GET
    url: 'https://autoremotejoaomgcd.appspot.com/sendmessage?message={{ message }}&key=<Insert Your Key>'

With this you can put in a service call in your automation that then sends a message to your phone, whatever message you provide as the ‘message’ parameter.

On the Tasker side you can create a profile that then listens for an autoremote call with a specific message and triggers a task. If you follow the link I provided for AutoRemote, João has a couple of videos that explain it really well you can follow. Since at this point its all just tasker stuff.

Now I do think that AutoRemote costs a couple bucks so hopefully that’s ok. If you were hoping for a solution native within Tasker (no other plugins required) I don’t really have a solution for you there. I did some googling and tasker doesn’t have websockets support so there’s not really any good way to listen for an event in HA. The only thing I can think of here would be to have your automation set a helper (input_boolean, input_text, etc.) and then have tasker poll HA repeatedly calling the HA Template task provided by the author above to watch for changes in this helper. Then you could react to those changes by calling Kill App.

This would work I suppose but its polling so its pretty ugly and there will be a delay. Autoremote I know of to get this to be event-driven.

4 Likes

This is very useful information @CentralCommand ! I am now investigating a way to automatically toggle motion alerts from a video doorbell app via another Tasker plugin - AutoInput. If I can get that to work to virtually tap the app’s icons, then I need a way for HA automation to instruct Tasker to run that task.

AutoRemote looks like the best way to do that, especially since I already have Tasker running to act on doorbell press and motion alerts from the app. (See this post if interested) I agree, polling would be ugly, and websockets much preferred. It is also not great that the Rest command can’t communicate directly with the phone, rather than go through the cloud.

I’ve not used this rest_command in HA before, but I would have thought a POST method to send the message, and not a GET would be needed?

EDIT: Thinking about this some more, I’m also using Telegram integration in HA to push messages for alerts. I could install Telegram alongside Tasker and use that to send message triggers tthrough Tasker’s built-in UI notification.

Could you share your version, cause I would also really like the HA set-state?

hi! forgive me digging up such an old thread, but I just [day ago] found that, while looking for Tasker<>Home Assistant connectivity, and I’d like to say: great job! that’s exactly what I planned to do, but I AM SO GLAD that somebody did that before me - I think I just saved a lot of time :wink:

but I have a “small” [I hope] problem, so maybe you @bokub could help me out :slight_smile:

I use an old android phone [android 6.x, device hard to root/flash custom so it is what it is] as the car gps system, and also as mobile router for my also android based head unit, but it’s irrelevant. I made a two tasks that depend on the chargin status. when ignition is on and phone is charging, task “hotspot on” is started. it turns on mobile data, turns on wifi tethering and with a big help of your tasks - it sends information to my Home Assistant instance [it sends phone battery level and turns on “car active” input_boolean, the rest is done by my HA, but it’s not important now]. when ignition is turned off and phone charging stops, task “hotspot off” is started and it sends info to HA [battery level & turns off “car active” input_boolean], then it turns off tethering and turns off mobile data - so the battery is saved and phone is in “standby” mode [I can change it any time by sending a text message to turn the data on, I can ask for location and get it via text etc. all done with Tasker too].
the problem is, HA not always gets the info from tasker. I thought it is some kind of ping/lag issue, so I’ve added a “wait 20s” commands before and after running your tasks, to be sure they won’t be interrupted by mobile data off command etc. I’ve even cloned the “perform task” command, so now I’m running your tasks twice, to be sure that the info goes out of the phone.
still - it doesn’t work every time. on the other hand, when I run the task by hand - it reports correctly, and status of my car is changed in HA.
I thought that maybe it’s because when task is [or should be] started automatically - screen is off and therefore battery optimization kills tasker or sth, and while I do it by hand - screen is on; but that’s not the case - I made sure tasker is not optimized for battery usage, and also the rest of the task [mobile data/tethering] turns on and off correctly.
I didn’t saw any errors displayed so I’m kinda stuck with no ideas.

can I somehow debug what’s happening with the information sent from your task - and if it is sent correctly?

1 Like

Dears,

When I execute the task, I recieve the following message in the notification bar:

With HA_ADDR set to “https://xxx.duckdns.org:8123

Error: 400
400: Bad request

With HA_ADDR set to “https://xxx.duckdns.org:8123/

Error: 404
404: Not Found

Any idea about how to solve?,

Thanks in advance.

Jose