Tutorial: Using the new Auth system with tasker

Thanks for the response.

I’m having problems with the script though.

I created a new task with a javascript:

const url = global('%HA_ADDR') + local('par1'); const token= 'Bearer ' + global('%HA_AUTH'); const xhttp = new XMLHttpRequest(); xhttp.open('GET', url, false); xhttp.setRequestHeader('Authorization', token); xhttp.send(); if( xhttp.status != 200 ) { console.error(xhttp.status + ' - ' + xhttp.responseText); }; if( xhttp.status == 200 ) { //successful http request var response = xhttp.responseText; }

and created a global variable %RESP : setGlobal('RESP', xhttp.responseText);

When I play the new task it gives me
javascriptlet: line 2: Uncaught SyntaxtError: Unexpected token finally

I tried it myself without any luck. I’m using the javascript option for POSTs but I’m still using RESTask for GETs.

I do have RESTask installed, could you share your setting for the GET commands please?

Thank you very much

Here you go. Keep Custom Body unchecked on the settings tab.

1 Like

Thank you very much!
Your global variables were different to the ones in this OP tutorial which threw me off a little lol

Here is mine for people who are following this tutorial

I just wanted to say thanks to the OP for sharing this. It is, by far, the best solution I have found for integrating tasker to Home Assistant. I like the fact that it is secure and works whether you are connected to your home network or not. Also, since you are calling the existing API it gives you access to a great deal of functionality without any special setup on the home the assistant side.

1 Like

OP: Fantastic! Thank you

FYI looks like Tasker added HTTP Auth and is removing HTTP GET/POST. Going to mess around with it and see if I can get it working.

1 Like

My task keeps running. So my light switch keeps going on when I switch it off via Home Assistant.
I want to run it only once. I did exactly what’s in the OP. Do I miss something?

Solved it:
I made the action to turn on the light inside th task “CALLHA”. But it has to be a task . So on the same level as “CALLHA”.
Now it works great. Thanks!

With the newer Tasker versions it’s become very easy to run POSTs and GETs through the HTTP Requests action:


For GET, change the Method and remove the variable from content.

Perform Task remains the same as in the original post of this thread:


For GET, change Parameter 1 to /api/states etc. and clear the Parameter 2 field.

%HA_path and %HA_token are my variable names for %HA_ADDR and %HA_AUTH in the original post.

Hi, with GET request, do you know if a variable is created with the output?
I currently try to create tasks to control my harmony hub which is connected to home assistant. The tasks will switch activities and also if the activity “watch tv” is currently active and I start the task “watch tv” again, everything will power off. So each task will also act as on/off button.
To archive this I need to know the current activity.
Harmony component has sensor.harmony_activity that always has the current activity stored.
So I tried api/states/sensor.harmony_activity to get that.
Do you know if output gets stored in a variable or how I could do that?

The output is stored in the local variable %http_data. I use the AutoTools plugin’s JSON read action to easily get the current state.

Thx for the reply, I’m no pro in Tasker and home assistant :smiley: so I have to ask:
So %http_data is a local variable which means it’s only existing in the http request task, right?
I’d need it in the “perform task” task I guess, because executing it and then sending a notification popup with “%http_data” the current activity isn’t in there.
Can you elaborate on how you get the current activity with json read? I also have the autotools plugin but never used json read.

Yes, local variables are limited to the task you’re currently running. In your case, could it be an issue with backslashes? It depends on what your URL is and whether it’s followed by a /. Does /api/states/sensor.harmony_activity work?

In AutoTools JSON read I keep simple mode checked, input %http_data as the Json string, and enter whatever fields I want in Fields (state). An Alert -> Flash -> %state should then show you the state.

I only now understood how it works :grin:
I thought it just returns the state in normal text. So sth. like autotools json read is mandatory for it to work.
One problem left though:
How can I get everything into one Task? http_data is a local variable again, so I guess it’s just there in the “http request” task. In the “perform task” task it’s not existent.
Perform task needs a task name to work but I cannot rename the “single line task http request”.

Great! While AutoTools isn’t required it does simplify getting the field you want.

Getting it all into one task is relatively straight forward but could you clarify what you’re trying to achieve in detail? The task setup depends on what states you’re dealing with and whether you need to specify an entity_id.

:smiley: I’m embarassed to share the task, I’m sure it’s overly complicated.
Basically for every harmony activity I’ll create one task. Then I will create a homescreen shortcut to the task. Tapping on it will switch to the activity. Tapping again will send PowerOff.
So in each task I use if and else.
Example Watch Tv button/shortcut: If %state is “play xbox” or “listen to radio” or “PowerOff” then send the POST command to start the activity “Watch Tv”
else send POST command “PowerOff”.
…Of course I could leave that away and just add a “PowerOff” button/shortcut but for me it’s much more convenient this way.

Looks like this:

Right now this is done with the help this Plugin: Harmony Tasker Plugin but since now I run home assistant server and it can do this and much more I think it’s better to do it via home assistant.

Ok. Eventually you may consider having Home Assistant push state changes to your Android device but for now you can go the route of getting them via HTTP GETs.

Action 1:
Perform Task (GET)
Set %par1 = /api/states/sensor.harmony_activity

Action 2:
AutoTools JSON Read
Simple Mode checked
Fields = state

Action 3:
Perform Task (POST)
%par1 = /api/services/harmony/watch_tv (or whatever the actual service call is)
%par2 = {“entity_id”: “entity here”}
If %state !~ Watch TV

Action 4:
Perform Task (POST)
%par1 = /api/services/harmony/PowerOff (or whatever the actual service call is)
%par2 = {“entity_id”: “entity here”}
If %state ~ Watch TV

Whether Action 3 or 4 triggers depends on what the returned state is.

Pretty good plugin for Tasker available here, no need to mess around with the HTTP requests:

Tip: Auto tools can build your JSON for you

1 Like

Thank you for the guide, I have tried this and have also double checked all settings as per your screens and it is not working for me. No errors even. Are you able to help point me in the right direction?