Simple Tasker Plugin

I am new to The Home Remote, and have been searching for methods to control the device I am running THR on. Have you ever wanted to have a simple way to turn on or off the screen from within THR? A simple way to say something on the device’s speakers? To run another app from within THR? To be able to initiate a Tasker Task from THR, and unleash the awesome capabilities of Tasker? Me too. The THR’s JavaScript scripting language is running within a sand box, making it difficult to do this kind of stuff. But JavaScript can do “HTTP Get” commands. This plugin will send messages to Tasker via HTTP, using the Loopback IP Address. The message is then received by the AutoRemote Tasker plugin, which in turn will pass the command onto Tasker. This method does not require any cloud services, any servers, or any additional hardware. Unfortunately this method only allows for one way communication from THR to Tasker, and of course only works on Android devices that have Tasker and the AutoRemote Tasker plugin installed. This also means that the plugin will not work in the Designer (you most likely will see a “Failure to connect” error message).

plugin.Name = "TaskerPlugIn";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = -1;
var VERSION = "220714A";
var http = new HTTPClient();
var preurl = "http://127.0.0.1:1817/?message=";
var posturl = "=:=HomeRemote";

function onChangeRequest(device, attribute, value) {
  var buff = "";
  var response;
  console.log("onChangeRequest: " + attribute + "=" + value);
  if (device.Id == "1") {
    if (attribute == "Switch") {
      if (value == "Off") {
       buff = "ScreenOff";
      }
      if (value == "On") {
       buff = "ScreenOn";
      }
    }
    if (attribute == "Scene") {
      buff = value;
    }
    if (attribute == "Task") {
      buff = "Task " + value;
    }
    if (attribute == "App") {
      buff = "App " + value;
    }
    if (attribute == "Say") {
      buff = "Say " + value;
    }
  }
  if (buff != "") {
    buff = preurl + encodeURI(buff) + posturl;
    console.log(buff);
// Due to a bug in AutoRemote, the get sometimes aborts with erc 500
// Just resend the request when this happens.
    try {
      response = http.get(buff);
    }
    catch (err) {
      response = http.get(buff);
    }
  }
}

function onConnect() {
}

function onDisconnect() {
}

function onPoll() {
}

function onSynchronizeDevices() {
  console.log("onSynchronizeDevices called");
  var virtualSwitch = new Device();
  virtualSwitch.Id = "1";
  virtualSwitch.DisplayName = "Tasker";
  virtualSwitch.Capabilities = ["Switch", "Scene"];
  virtualSwitch.Attributes = ["Task", "App", "Say"];
  plugin.Devices[virtualSwitch.Id] = virtualSwitch;
}

I have also attached two Tasker Profiles to enable Tasker to understand what is being requested from THR. The “HomeRemote” profile runs when the Home Remote app is started, and enables AutoRemote to receive messages from THR (it is normally disabled). The second profile “HRemote” is the profile that actually receives messages, and does the processing to perform the desired Tasker command. Just import these two profiles into your Tasker environment.

This Tasker plugin will create a device named “Tasker”. To pass requests/commands to Tasker, use a DataAction.

To turn the screen off or on:

Tasker.Switch = Off
Tasker.Switch = On

To run another app on the device (“My App” being your apps name, case sensitive):

Tasker.App = My App

Example:

Tasker.App = Night Clock

To say something on the device’s speaker:

Tasker.Say = Use the force, Luke

To run a specific Tasker set of instuctions, use the Scene command. Currently only “Exit” is implemented, which will disable the AutoRemote Wifi reception. It is a good idea to disable AutoRemote Wifi Reception when not in use, but be careful as AutoRemote will no longer be able to receive commands until it is re-enabled.

Tasker.Scene = Exit

All of the above can also be done by creating a Tasker Task, and then running that Task.

Tasker.Task = My Task

You can also pass one or two parameters to that task, by using the vertical bar delimiter:

Tasker.Task = My Task|Parameter 1
Tasker.Task = My Task|Parameter 1|Parameter 2

Some of the things one can do within a Tasker Task is to set or lock the Screen Rotation, send SMS messages, make custom Notifications, and much, much more. Also, if running another app that Tasker can communicate with (such as “Homeseer”), then you can now use Tasker as the intermediator between THR and that app.

Notes: AutoRemote by default will display a Toast as it receives messages. You can disable those Toasts by explicitly running AutoRemote, and going to Settings, Alerts. You might want to leave Toasts on at first so that you can see that everything is working. Also, Tasker may require special Android permissions to run certain functions (like turning the screen off). Refer to the Tasker Documentation to find out how to grant those permissions to Tasker.

Tasker.plugin (1.7 KB)

HomeRemote.prf.txt (3.8 KB)

HRemote.prf.txt (17.1 KB)

Rename the two profile text uploads to have a suffix of “.xml” instead of “.txt” before you import them into Tasker (this site does not allow .xml type files).

2 Likes

@aa6vh WOW. Thank you for this. My brain is going a million miles an hour - the possibilities are so vast! I’m excited to dig in, I’ll report back when I have something cool to share. Thanks again!

Your welcome. My first project using the plugin was to create a screen saver scene in Tasker (black background with a floating digital clock). The advantage of using Tasker for this is that THR remains running underneath. So there is no lag when bringing back THR. I trigger the screen saver via a button and also by using the Content page’s idle event. Running both THR and Tasker together does cause some battery drain, so I have Tasker turn off the screen instead when not on external power.

@aa6vh Was going to see if you could help…I got it set up but it is not executing the Tasker task from HR. There is no toast message. If I hit the play button on the task from Tasker app, it works.

When this occasionally happens to me, it usually means that AutoRemote is not properly running (or configured). I wrote a short Tasker task to test AutoRemote, and to remove HomeRemote from the mix. The description of that task follows. I just run the task from inside the Taser Designer using the Run Task button. As I note in the plugin, there is a bug somewhere that will return a 500 error code, I just run the task again when that happens.

Task: HRTest

A1: If [ %PENABLED !~ *,hremote,* ]

A2: Profile Status [
Name: HRemote
Set: On ]

A3: AutoRemote Wifi [
Configuration:
Starting Wifi Service
Persistent Notification: true
Timeout (Seconds): 0
Structure Output (JSON, etc): On ]

A4: Wait [
MS: 0
Seconds: 5
Minutes: 0
Hours: 0
Days: 0 ]

A5: End If

A6: Variable Set [
Name: %URLIP
To: http://127.0.0.1:1817
Max Rounding Digits: 3 ]

A9: Variable Set [
Name: %urll
To: Task Toast
Max Rounding Digits: 3 ]

A10: JavaScriptlet [
Code: urll=encodeURI(urll);
Auto Exit: On
Timeout (Seconds): 45 ]

A11: Variable Set [
Name: %URL
To: ?message=%urll=:=HomeRemote
Max Rounding Digits: 3
Structure Output (JSON, etc): On ]

A12: Variable Set [
Name: %buff
To: %URLIP
Max Rounding Digits: 0 ]

A13: Variable Set [
Name: %buff
To: /%URL
Append: On
Max Rounding Digits: 0 ]

A14: HTTP Request [
Method: GET
URL: %buff
Timeout (Seconds): 30
Trust Any Certificate: On
Automatically Follow Redirects: On
Continue Task After Error:On ]

A15: Variable Set [
Name: %URLDATA
To: %http_data
Max Rounding Digits: 3 ]

A16: Variable Set [
Name: %URLRESP
To: %http_response_code
Max Rounding Digits: 3 ]

A17: Flash [
Text: %URLRESP
Continue Task Immediately: On
Dismiss On Click: On ]

Run the above task and see what kind of specific errors you are getting.

Is there a way to import this? It’s not in xml format. Forgive me, I’m somewhat of a Tasker beginner.

I still would like the answer to the above question, but more importantly I got it to work! I had not added the second profile. I’m pumped to report back when I wrap this little project up - it’s going to be cool.

Okay, here is the export file. Remember to rename the file from *.txt to *.xml.

HRTest.tsk.txt (8.8 KB)

1 Like

Wanted to document my first project with the Tasker plugin. I used it to create media buttons in my phone notifications when that device gets powered on.

Great work! This Tasker plugin could be extremely useful and powerful !

I’ve been using the TNES web server plugin for Tasker for years, to be able to send HTTP commands to TNES to then run Tasker tasks.

I wrote about some of the things I had done with it and my Vera HA controller here.