NOTE: THIS TUTORIAL IS OLD AND DEPRECATED
PLEASE CHECK OUT THE NEW VERSION HERE
Since the last update, I’ve had issues calling the Home Assistant API using my legacy password. It looks like a bug, but I thought it was time to switch to the new Auth system in my Tasker tasks.
After a bit of struggle, here is the best solution I’ve found, I hope it will help some people
1. Create a token
First of all, create a new “Long-Lived Access Token” at the bottom of the /profile
page of your home assistant.
Save your Home Assistant address as well as your token in your global variables, like this:
2. Create a generic task
Create a new Task (I called it “Call HASS”) with a single action in it: JavaScriptlet
In the code
part of the JavaScriptlet, paste the following
const url = global('%HA_ADDR') + local('par1');
const token= 'Bearer ' + global('%HA_AUTH');
const xhttp = new XMLHttpRequest();
xhttp.open('POST', url, false);
xhttp.setRequestHeader('Authorization', token);
xhttp.send(local('par2'));
if( xhttp.status != 200 ) {
console.error(xhttp.status + ' - ' + xhttp.responseText);
}
Your task should look like this:
3. Call your task
Calling the Home Assistant API is now super easy!
In a new task, use the Perform Task Action, and fill the following:
- The name of the previously created task in
name
(Call HASS in my example) - The URL to call in
%par1
- The JSON to send in
%par2
That’s it!
I could have used a plugin such as RESTask to send HTTP requests with headers, but it seems a little bit overkill compared to these few lines of Javascript code.
Anyways, I hope my little tutorial will help people struggling with the new Auth system.
Have fun