Tasker and SmartThings (reposting of project from build site)

GETTING ACCESS TO SMARTAPP ENDPOINTS

In order to get SmartThings working with Tasker on Android, I will assume you’ve completed Danny’s Endpoint API tutorial here. (old link sorry) You will need to be able to host your own PHP file as per Danny’s instructions, because at the time of this post, Danny’s hosted PHP file is down.

PREPPING TASKER

Once you have finished Danny’s tutorial, the first thing we need to do on our Android smartphone is allow External Access, which will allow apps like NFC Task Launcher (see Part 2: NFC) to initiate Tasker tasks.

Step 1: Open Tasker, tap the Menu button and go to Preferences.

Step 2: Check the “Allow External Access” option and click the X in the top right corner.

AUTHORIZING ACCESS TO YOUR DEVICES

Now it’s time to get the Endpoint URL(s) that we’ll use to send commands to our devices. We’ll do this through the Android smartphone, so that we can copy/paste the long URLs instead of having to type them out.

Step 3: Open the browser on your phone and navigate to your hosted PHP file. Login to your ST account.

Step 4: Select your Hub from the dropdown, check the devices you want to create URLs for, and tap “Authorize”.

Step 5: Find the first device you’d like to control with Tasker and tap-and-hold the “Toggle” button. I’ll be using my living room light as an example.

Step 6: Tap “Copy Link Address” to copy the full URL to the clipboard.

CREATING TASKER TASKS

This is where the magic happens. We’re now going to create the task in Tasker that can be used for an infinite number of purposes: Location-based triggering, Phone state-based triggering, NFC triggering, Voice control, and so on…the possibilities are endless. (I’ll be talking about NFC more in Part 2 and Voice control in Part 3, coming soon.)

Step 7: Open Tasker again, go to the Tasks tab and tap the “+” to create a new task.

Step 8: Name the task something memorable, like “Living Room Toggle” and tap the Check mark to save.

Step 9: Tap the “+” at the bottom of the page to add a new Action.

Step 10: Tap “Net”.

Step 11: Tap “HTTP Get”.

Step 12a: In the “Server:Port” field, type graph.api.smartthings.com (leave off the http://).

Step 12b: Tap-and-hold on the “Path” field and tap “Paste” to paste the URL we copied earlier.

Step 13: Drag the cursor up to the beginning of the URL and delete the “http://graph.api.smartthings.com/” portion of the URL. IMPORTANT: Make sure to remove the “/” before “api/…”

Step 14a: Tap the back arrow at the top-left to save the Action.

Step 14b: I also add a Flash (+ > Alert > Flash) to let me know what action is taking place. You can also hit the “Play” button in the bottom left to test out the task. Each time you tap Play, your device should turn on/off.

THAT’S IT!

That’s all there is to creating a Tasker task that will toggle your device on and off each time it’s triggered. You can now use this task with any type of Tasker Profile you’d like.

If you would rather have a task that simply turns ON or OFF your device, rather than TOGGLING it, feel free to copy/paste the On or Off URL, rather than the Toggle URL.

Next up…Using NFC Task Launcher with your newly-created Tasker task to bring NFC control to SmartThings!!

4 Likes

So we’ve learned how to create Tasker tasks for each of our devices. Now we’re going to learn how to use NFC tags around our house to trigger these tasks, giving us super-awesome NFC control of SmartThings!

 

SETTING UP TRIGGER (Formerly NFC TASK LAUNCHER)

We’ll be using Trigger, formerly known as NFC Task Launcher on our Android device to read our NFC tags. Here’s how to set it up.

Step 1: Tap the “+” icon to add a new NFC tag to the app.

Step 2: Choose “NFC” from the pop-up list.

Step 3: Give your NFC task a memorable name, like “Living Room Light”, then tap the “+” icon to add a task to this NFC tag.

Step 4: Scroll to the bottom, expand Tasker and check the “Tasker Task” box.

Step 5: Type in your Tasker task name EXACTLY as it appears in Tasker, then tap “Add to Task”.

Step 6: Tap the right arrow “->”.

Step 7: Get your NFC tag ready and scan it with your phone to send the task to it.

Step 8: Tap the Check mark to save everything.

 

THAT’S IT!

That’s all there is to it. Now, every time you tap this NFC tag with your phone, your device will toggle (assuming you used the Toggle URL in the previous post).

This post was intended to outline the steps involved in setting up AutoVoice for Tasker in order to create some really powerful voice recognition triggers that work fantastically with SmartThings.

I decided that, rather than re-inventing the wheel, I’d just post one of the best YouTube tutorials I’ve seen for setting up AutoVoice with Tasker. Armando Ferreira posts really good Tasker and Android-related tutorials, so I highly recommend checking out his Tasker Tutorials YouTube channel.

AUTOVOICE TUTORIAL

Below is an example of some of the super-powerful voice control options you now have at your disposal with AutoVoice:

 

One thing to keep in mind as you’re setting this up is that you don’t actually have to specify the exact sentence you’ll say to activate a task. For example, the guy in the video above says “Turn on the bar lights” but his AutoVoice Command Filter is very likely only “bar lights”.

The great thing about this is that, because we use the “Toggle” URLs in my previous post, any time you say a sentence that contains “bar lights”, Tasker will simply send a Toggle command. Think about it, why would you say “turn off the bar lights” if they’re already off? That’s why the demo above makes the system seem so smart is because you’re actually the smart one.

Now, if there is an AutoVoice command that you really need to specify On or Off, you can absolutely do that. For example, let’s say you’re away from your house and you want to create an AutoVoice command to recognize the phrase “Shut it all down” and turn off every light in the house. Easy:

Step 1: Create an AutoVoice Recognize profile with the command filter “shut it all down”

Step 2: Attach that profile to a new Task that hits every light switch’s “Off” URL

This will send “off” commands, even if some lights are already off, so you can ensure everything will turn off. It’s a little odd because the lights will turn off one after the other, in the order that you setup the task, but it works.

Hope you’ve found awesome uses for Tasker, NFC and Voice control of your SmartThings. If you have ideas for how this stuff can be used, post them in the comments!

1 Like

Just for full transparency, I originally wrote this tutorial on the old community. @Ben is just reposting it from the archives.

yes - thank you so much for the hard work of this. May it now evolve!

The link above directs to the old forum which is no longer available. Can we update it to the new site?

[old]http://build.smartthings.com/blog/tutorial-creating-a-custom-rest-smartapp-endpoint/[/old]

EDIT: Here is the new link.

Is there any way to re-post or supply a new link to the PHP script listed above, the “Danny’s Endpoint API”; is there any way to get a hold of this again? With the community/forum updates looks like a lot of these links are dead and some searching isn’t producing great results for me!

Yeah we’ll get that up here somewhere. @matthewnohr ?

So close to getting this working without that aforementioned PHP script. I think that’s the missing link I’m seeing though.

Right now I’ve used the Sample Web Service SmartApp (https://support.smartthings.com/hc/en-us/articles/200901780-Sample-Web-Services) and have a GET URL I can hit to toggle and/or Turn on/Off (with a few modifications of this to support GET URL’s for controlling the lights, namely something like this added to the mappings section:

path("/switches/:id/on") {
	action: [
		GET: "toggleOnSwitch"
	]
}
path("/switches/:id/off") {
	action: [
		GET: "toggleOffSwitch"
	]
}

Then functions that look like this to the body:

  void toggleOnSwitch() {
	log.debug "toggleOnSwitch: id is $params.id, available devices $devices, switches are $switches... params are $params"
	def device = switches.find { it.id == params.id }
	if (!device) {
		httpError(404, "Device not found")
	}
	else {
		device.on()
	}
}

Of course these need to be cleaned up, but just to understand the concepts for me this seems to work. However this needs a login to access the URL directly when accessing from a new device…So when I pop this URL into Tasker doesn’t seem to know what to do with it.

Maybe a more generic question here, if anyone knows, maybe @csader is how we can access the rest API’s without logging in? I presume theres some way to pass a secret or token of some sort?

Salvaged the tutorial:

Thank you for this. I was able to get the php file setup with the endpoint smart app as well. When I copy the link address to Tasker, careful to remove the appropriate part of the link. But when I click on play in Tasker nothing happens. I setup the flash alert to get the toast notification and this displays correctly. I tried a few different switches with on/off/toggle commands. I made sure allow external access was still checked.

Any suggestions.

Edit: figured it out. Your instructions said to leave off the http://. I noticed that the site was https://. So when I added that to graph.api.smartthings.com for the server: port it worked

1 Like

Thanks Ben for this. And Thanks Korban_hadley for last post. added https:// it worked

2 Likes

Is there a way to get it to set dimmer levels. It only sees the dimmer switch but does not control the brightness level.

You’re going to have to modify the SmartApp yourself. A vague direction would be to add another endpoint to the map, say value, and then use param.value in the update method.

path("/dimmerSwitches/:id/:value") {
	action: [
		GET: "updateDimmerSwitch"
	]
}

Thanks @Ben for getting this re-posted!!!

For what it’s worth I was able to integrate this Smart Things REST endpoint with Tasker directly. I manually went through the Token request and just saved that off in my files, then you can create the GET request directly from within tasked for your automation without the need for the extra hosted PHP file (which is really just automating the getting of your access token and providing an easier way to retrieve the URL’s). This has been kind of mentioned elsewhere in pieces, but maybe this will help some others!

You can do these things manually:

Once you’ve enabled OATH on your app:

Go to: https://graph.api.smartthings.com/oauth/authorize?response_type=code&client_id=YOURCLIENT_ID&redirect_uri=https%3A%2F%2Flocalhost&scope=app

You’ll get redirected to localhost blah blah, just copy the last part off the URL. This is your code.

Then go to: https://graph.api.smartthings.com/oauth/token?grant_type=authorization_code&client_id=YOURCLIENT_ID&client_secret=YOURCLIENT_SECRET&redirect_uri=https%3A%2F%2Flocalhost&scope=app&code=THECODE_FROM_LAST_STEP

This will respond with some stuff, the important part of this is your ACCESS_TOKEN. Save that.

Once you get this you’re good to go! Now you can make requests to your REST APP. For example, you can list things like:

https://graph.api.smartthings.com/api/smartapps/endpoints/YOURCLIENT_ID?access_token=YOUR_ACCESS_TOKEN

This will list out your URL you can use to access the Rest endpoints. This will respond with some JSON that you can pull out the “url”. Now use this URL, coupled with YOUR_ACCESS_TOKEN to your heart’s content!

Also, if you used the above mentioned Smart App to include Switch functions like Toggle, On, Off, etc… you can now run commands like:

https://graph.api.smartthings.com/api/smartapps/installations/YOUR_INSTALLATION_URL/switches/?access_token=YOUR_ACCESS_TOKEN

This will list the switch ID’s that you can then run commands on.

So what I did is take tasker, coupled with the Google Now voice integration and created a match for “living room lights” and put the URL above that toggles the lights.

Voice commands in Smart Things! Woohoo!

2 Likes

Not sure where it came from or how I got it but I just use http://23.23.13.21/exampleOauth.php to get my on/off/toggle links

I always assumed this was ST hosted.

+1 to this. If you don’t include https:// in the Server:Port field in tasker, this will not work. The SmartThings API likely started enforcing SSL somewhere along the way. :stuck_out_tongue: :grimacing:

That said, the full Server:Port field should be: https://graph.api.smartthings.com

@terigox can you post all of this in one tutorial. I am getting confused on how to set up the SmartApp and then access it directly. An example with the simple Groovy code for the SmartApp would be great. I am not really sure what goes in the SmartApp to just do something simple like turn on a light. Wow, this stuff gets very complicated.