Tutorial: Using the new Auth system with tasker

Thank you very much! I have probably always overlooked it.
Now it work’s thank’s!

Hi @bokub - Thank you a ton for putting together this tutorial and associated tasker javascriptlets. Unfortunately, I’ve been really struggling with this for a lengthy amount of time after a change (upgrade to .72, and use of let’s encrypt cert) broke initial success.

Here’s my story, hopefully someone can help provide insight.

  1. Approx. back in November 2018, I switched to the new auth system and had an opportunity to successfully use your tutorial around the middle of December. It worked for a few days until I decided to use a valid let’s encrypt (wildcard) certificate to secure my instance. Then, I upgraded the same day and unfortunately, this broke the tasker integration and I’ve been struggling ever since.
  2. I went down a pretty lengthy path of re-reading this thread over and over, trying iteration after iteration. First - I thought it could be typos (as so many folks in this thread have experienced). I must have copied/pasted and typed in the javascriptlets 5-7 times, each carefully checking, and ultimately with the same result (tasker shows the green “light” as if the script is running but there is no error). I then tried downloading the .tsk file that a user in this thread linked. Same result. I have also tried using the REST plugin for tasker, but have been unsuccessful with that.
  1. Exhausted after days of struggling, I finally started messing with this again, looking for any kinds of errors. I found that if I disabled the “auto exit” switch in Tasker on the call Hass part, I could run the perform task to see an error. Here’s the error I get when trying to toggle a light: "Failed to execute send on XMLHttpRequest: Failed to load ‘https://ha.domain.tld:8123/api/services/light/toggle’. This is (of course) a legitimate address (I’ve my removed domain and tld). Next, I thought… is the REST api working? So, I checked. I was successfully able to curl the address (with the appropriate bearer token, etc.) and also was successful in using postmam as well. So, if the API works for computers, why can’t Tasker work? I began hacking on the javascriptlet code, consolidating the two parts to one (to make sure everything lined up - as another user here did). Still no dice. I then realized that it could be because my (Pixel) phone was using google’s 8.8.8.8 DNS servers (and since I don’t expose my hass.io externally) it just was timing out. I use a wildcard certificate that is valid, but maybe this was a good path to try!

So… I did everything (not simultaneously, obviously) from forcing the use of internal DNS at the router, blocking Google’s DNS, setting up a VPN to force the phone to use my local DNS, and finally settled upon DNS overrides (which have been working very effectively). Then, using an app for my phone, I could successfully ping and of course the Chrome app on my phone can access the address with the override. But… no dice for tasker. I proceeded to enter in both parts of your script 3-5 times, carefully checking. I tried the IP address for the hass.io instance, and then the hostname, then carefully checked that I had the :8123 part perfectly. I ended up having to give up temporarily.

  1. A few more days pass, and I am still missing the functionality I used to get from tasker HomeAssistant and I decide to look again. This time, I’m looking through Hass.io’s logs. I can see a number of things pop-up right after trying to run the tasker command that indicate something’s off:

Strange log entry #1
Platform rest not ready yet. Retrying in 180 seconds.

Strange log entry #2
Error doing job: SSL handshake failed

Strange log entry #3
Error doing job: SSL error errno:1 reason: HTTP_REQUEST

There are a few recommendations in that thread (adding intermediary as part of the hass config, for example) and none of them worked. There was a link to a github issue (link removed) which still didn’t really give any insight and yet was marked as closed.

I’m kind of at my wits’ end, when all I want to do is shake my phone to turn on a light. I’ve blown probably >10 hours on this. So, I guess I wanted to do this write-up to save anyone else from blowing so much time on such a simple thing, but also ask if anyone has any ideas? I can post screenshots, etc. if needed.

Thank you in advance for any advice. Sorry about the multiple posts - the forums wouldn’t let me post >2 links per single post as a new user- Previous

Thanks for the tutorial, simple and it works!!

The JavaScriptlet solution wasn’t working for me. Please see below solution that is working for me.

  1. Create a “Long-Lived Access Token” as described in the initial post. Thanks to @bokub

  2. Setup variables.

1

  1. Create a task named “Call Hass” or similar. Use below code in the “Run Shell” task.
curl -X POST -H "Authorization: Bearer %HA_AUTH" \
       -H "Content-Type: application/json" \
       -d '%par2' \
       %HA_ADDR%par1

2

  1. Call the “Call Hass” task with parameters for API service (%par1) and for example entity_id (%par2) as shown below.

4

Hope this helps.

Br Qxlkdr

1 Like

Thank you for this. I’m getting “Error: 127” when I run this.
Any suggestions?

I have the exact same issue

OMG! It works! Thank you a million @Qxlkdr! I don’t know what it is about the Javascriptlet solution but it just wouldn’t work for me. This opens back up a lot of the automations I have been wanting, so thank you!

Beyond your curl based tasker config, the other changes I made which may have had an affect was to switch away from the wildcard (and ensure the home assistant address was defined in the SAN field of the cert), and also to include the full chain let’s encrypt .pem. rather than just the cert file.

1 Like

I am not able to make this work.
I think these tasks might run “togheter” in some way?

EDIT: Got it to work by reading https://www.reddit.com/r/tasker/comments/7p51ll/task_doesnt_exist_error/

I can get these to work just fine in tasker but i cant export them as apps, shortcuts, or widgets. Anyone else having problems with this?
Here are some of the errors I get after failed attempts:

With tasker you can do it

Consulta “SmartMy Home Assistant”
https://play.google.com/store/apps/details?id=com.gmail.paolo.ironhide.smhbeta

Using the run shell method above I kept getting error 127 on my pixel 2xl running pie. I exported the task and ran it on another phone which has android 8 and it works fine. Seems like it won’t work on some android phones.

Spent a long time getting this to work. Thought I’d put my experience here so it might help

TL;dr - check your ssl chain is properly installed. I used: https://www.sslshopper.com/ssl-checker.html

This showed I was missing the intermediates from my certificate.pem file. Added these in, restarted ha and we’re all good.

To figure it out I used an app called Advanced Rest API Client which is an android version of Postman as I only have my phone at the moment. Took a lot of swapping between apps and faff, but got there. Once I made the API calls from this app I could see an SSL error.

Now I have both the java and shell versions working in tasker, using the java one for now.

Interestingly, I tried to get certbot to work with no joy - on hassbian, one for another day.

Hope that helps, always check the basics…

Hi, could someone tell me how to use this with with the variable.variable_set service? I don’t know how to parse a custom value with this.

Idea
How I could populate an ‘input_text’ ?

In the URL, replace the . with a / and it should work, at least it did for me.

Where is the above code does the setGlobal go? I have the JavaScriptlet method working for Post but not for Get?

And what goes in %par1 and %par2 in the Perform Task task for Get?

Something like par1 = /api/states and par2={“entity_id”:“switch.switch_test”}

Thank you so much for putting this together. Everything is working great with real entities like lights and switches but I’m having problems with getting sensor states. Don’t know if I’m doing this right

Getting a 401 Unauthorized

Obviously that’s what used to work for me but I’m trying to migrate to long lived tokens
https://XXX.duckdns.org/api/states/sensor.broadlink_sensor_temperature?api_password=XXXX

With states you need to put the entity ID in the url:
/api/states/sensor.broadlink_sensor_temperature

See these posts earlier in the thread on modification of the tasks: Tutorial: Using the new Auth system with tasker & Tutorial: Using the new Auth system with tasker