Tasker driven HTTP Owntracks via Nabu Casa webhooks

Had the opportunity for a quick play pre-Xmas with the new Nabu Casa Owntracks webhook, so I decided to move from an MQTT based Owntracks setup using CloudMQTT to avoid opening router ports.
Being my usual awkward self I decided to avoid using a separate Owntracks client and instead took a crack at using Tasker to call the webhook instead.

Here then there is a redacted Tasker task I’m using to report location intermittently; it took me about 2 to 3 hours on a sleepless night to figure out (I’m an insomniac with a low boredom threshold).

Overview:
This task “ReportLocation” is designed to obtain a location fix within 60 seconds (either by Network or GPS) and provide it along with battery, fix accuracy and fix time to the OwnTracks component via HTTP through Nabu Casa.

ReportLocation (3)
A1: Variable Set [ Name:%fixtype To:0 Recurse Variables:Off Do Maths:Off Append:Off ]
A2: Variable Set [ Name:%starttime To:%TIMES Recurse Variables:Off Do Maths:Off Append:Off ]
A3: Get Location [ Source:Any Timeout (Seconds):60 Continue Task Immediately:Off Keep Tracking:Off ]
A4: Variable Set [ Name:%fixtype To:1 Recurse Variables:Off Do Maths:Off Append:Off ] If [ %LOCTMS > %starttime ]
A5: Variable Set [ Name:%fixtype To:2 Recurse Variables:Off Do Maths:Off Append:Off ] If [ %LOCNTMS > %starttime ]
A6: If [ %fixtype > 0 ]
A7: Variable Set [ Name:%locat To:%LOC Recurse Variables:Off Do Maths:Off Append:Off ] If [ %fixtype = 1 ]
A8: Variable Set [ Name:%locat To:%LOCN Recurse Variables:Off Do Maths:Off Append:Off ] If [ %fixtype = 2 ]
A9: Variable Set [ Name:%fixtime To:%LOCTMS Recurse Variables:Off Do Maths:Off Append:Off ] If [ %fixtype = 1 ]
A10: Variable Set [ Name:%fixtime To:%LOCNTMS Recurse Variables:Off Do Maths:Off Append:Off ] If [ %fixtype = 2 ]
A11: Variable Set [ Name:%url To:https://hooks.nabu.casa/ Recurse Variables:Off Do Maths:Off Append:Off ]
A12: Variable Set [ Name:%path To:MyUniqueWebHookID Recurse Variables:Off Do Maths:Off Append:Off ]
A13: Variable Set [ Name:%user To:username Recurse Variables:Off Do Maths:Off Append:Off ]
A14: Variable Set [ Name:%device To:devicename Recurse Variables:Off Do Maths:Off Append:Off ]
A15: Variable Set [ Name:%locaccuracy To:%LOCACC Recurse Variables:Off Do Maths:Off Append:Off ] If [ %fixtype = 1 ]
A16: Variable Set [ Name:%locaccuracy To:%LOCNACC Recurse Variables:Off Do Maths:Off Append:Off ] If [ %fixtype = 2 ]
A17: JavaScriptlet [ Code:var uri = url+path;
var lat = locat.split(“,”);
var lon = parseFloat(lat[1]);
lat = parseFloat(lat[0]);
var data = {“_type”: “location”, “acc”: parseInt(locaccuracy), “batt”: parseInt(global(“%BATT”)), “lat”: lat, “lon”: lon, “tid”: “ABC”, “tst”: parseInt(fixtime)};
var xhttp = new XMLHttpRequest();
xhttp.open( “POST”, uri, false );
xhttp.setRequestHeader(‘X-Limit-U’, user);
xhttp.setRequestHeader(‘X-Limit-D’, device);
xhttp.setRequestHeader(‘Content-Type’, ‘application/json’);
xhttp.send(JSON.stringify(data));
var rtresponse = xhttp.response;
var rtcode = xhttp.status; Libraries: Auto Exit:On Timeout (Seconds):10 ]
A18: End If

As the above example stands, you will need to replace:

  • MyUniqueWebHookID in step A12 with your Nabu Casa hook id
  • username in step A13 with the user part of owntracks (referred to as Identification : Username in the Owntracks app setup information)
  • devicename in step A14 with the device id part of owntracks (referred to as Identification: Device ID in the Owntracks app setup information)
  • ABC” in the javascriptlet to a unique short identifier for the user in question; I usually use initials for this “tracker id”.

I’ve also added this redacted version to Taskernet.

The task should then work if the Owntracks component is up and running successfully - the HTTP response can be checked by adding a flash step for %rtresponse and %rtcode; %rtcode should be 200 if successful. Note that I switched from MQTT so devices and zones were already in place.

The biggest hassles I had debugging it were:

  • understanding the need to pass user and device id as request headers for a valid request.
  • providing the JSON data in the correct datatypes - this led to a lot of errors appearing in my HomeAssistant logs until i got my head around what was required. You may find the Owntracks Booklet useful, particularly the section on JSON.
3 Likes

You’re a crazy guy :wink:
The task is running, now I will play around defining profiles…
Thank you!

1 Like

Many thanks for this. :star_struck:
I have searched and struggled with this to find this solution. Works perfectly

With euphoric greeting
Andreas