RESTful Call from ST?

SmartThings noob here. I’ve spent several hours searching but haven’t yet found what I’m looking for.

Very simply, I need to make a REST call (outbound from the ST hub) when a switch is turned on. Can someone point me to some step by step instructions for a beginner to set this up?

I already have a device/switch set up. It happens to be a virtual switch, but that shouldn’t matter for this.

Most or all of the info I have found relates to setting up the ST hub to do something when receiving a REST call. I need to do the opposite. I need to send a REST call. It seems it must be really simple and everyone knows how to do it! :slight_smile:

I already have this working by using IFTTT and the Maker Channel, but I think I should be able to eliminate this step and make the REST call directly from SmartThings hub.

Thanks

1 Like

Never assume that SmartThings is “simple” … though some features are easier than others.

It is easier, for example, generally, to do REST-API calls “cloud to cloud” (which may require opening a port to the device on your LAN if it is not already connected to a vendor’s cloud…) instead of directly on the LAN via the Hub. SmartThings implements them entirely differently.

For LAN: Lookup “hub action”. Trying to think of a simple example, but a rather comprehensive one is the Hue Connect SmartApp and Device Type Handler. I think it still is in the SmartThings Community Public GitHub. https://github.com/SmartThingsCommunity/SmartThingsPublic/blob/330b41941a20b2d489e26a166427a24a0a425c5e/devicetypes/smartthings/hue-bridge.src/hue-bridge.groovy

For Cloud to Cloud… Hmmm… tons of examples. Perhaps the “Smart Weather Station Tile” (available right in Templates … I think).

Be sure to get familiar with the sections of: http://docs.smartthings.com

2 Likes

Do a search for celcus tv dth. I wrote a dth to do this very thing so u can copy the code and change the xml.

1 Like

Thank you both for your replies. I dug into both of your suggestions and have come to the conclusion that this is well above my pay grade. I’m sure it all makes perfect sense to those of you who have done this a million times, but the learning curve seems extreme and it’s very foreign and intimidating to me. I only plugged in and tried my hub for the first time yesterday after all. :slight_smile:

I was able to get working what I wanted very quickly with IFTTT, but the process here seems incredibly involved. So I think I will just stick to my IFTTT solution, unless someone volunteers to get this going for me.

tgauchat mentioned the possibility of having this REST-API call directly on the LAN via the ST hub. This is what I would want, as the goal would be to avoid the cloud entirely The IFTTT solution I have working has a definite lag in the response and this was the basis of my desire to have the ST hub make the call directly. I was hoping that this would reduce or eliminate the lag by eliminating both the IFTTT step and the cloud entirely.

1 Like

I agree this can be done confusing stuff but I also think that what you are trying to do is actually about as simple as you could hope for. I think the IFTT lag is going to drive you crazy eventually. I say that from experience. :slight_smile:.

You didn’t say what exactly what your rest call was trying to do or if it was a LAN or outside request. If all you need is to hit an endpoint is can be as simple as one line if code in your virtual switch DTH. I can further point you in the right direction if you want to try.

1 Like

Currently SmartThings does not provide any way of “avoiding the cloud entirely” because all custom Device Type Handlers and SmartApps are not sent down to the Hub V2 for local execution.

So even a local “hubAction()” command, is first processed in the SmartThings Cloud, then relayed to the Hub to execute.

1 Like

Good point on the cloud piece. No matter what you do it is going to hit the ST cloud. Nevertheless, at least in my experience that’s faster and more reliable than IFTT.

1 Like

I sense some confusion here. You talk about making a REST call from the hub and at the same time mention IFTTT. The hub can only make REST calls on your local network (LAN), while IFTTT cannot access anything on your local network unless you setup port forwarding in your web router.

If you want to make REST calls to some web service outside your network, there’s a set of HTTP API in the smart app to do that. It’s not that complicated.

http://docs.smartthings.com/en/latest/smartapp-developers-guide/calling-web-services-in-smartapps.html

2 Likes

I was wondering the same thing which is why I asked for more info. But I think we all agree it’s really a pretty easily doable thing depending on what he’s trying to accomplish.

Wow. So many replies so quickly!

I was coming back to revise my last post.

In thinking about it a bit, I realized I could not avoid the cloud as the REST call I want to make is to my ISY Portal, which is not local. I wasn’t thinking about this right and was thinking the call was to the (local) ISY hub. Sorry about that.

So, reading the comments, and because of what I just said, avoiding the cloud is not possible. Ok, fine. Forget that idea.

njschwartz said that the IFTTT lag is going to drive me crazy. I realize this and eliminating IFTTT from the process is the goal.

This is the URL I want to POST (as an example):
https://my.isy.io/api/ifttt/Family_Room_On/key/my_secret_key
This is what I have in the Maker Channel request in IFTTT. I just need to do the same from within ST and then I could skip IFTTT all together.

http://docs.smartthings.com/en/latest/ref-docs/device-handler-ref.html?highlight=httppost#httppost is the documentation for a post request. Basically:

httpPost(
uri: “https://my.isy.io/api/ifttt/Family_Room_On/key/my_secret_key”,
body: “Some value if needed”,
) {response -> log.debug (response.data)}

This basically just makes the request and then sends any response to log.debug should you need to see it but you could do something else with it as well if you needed to. That URI is going to change since it won’t be using IFTT I assume so that is something you’ll have to figure out.

If you put something like that into the on() method of your virtual switch it will run every time it is called.

Actually thinking about it some more, your ISY hub is on your LAN so you could probably send a hubAction request instead which is how LAN requests are made. You need to figure out what the REST endpoint is to do what you wan though.

When I plug the URL in my example into http://requestmaker.com/ it works, even with the IFTTT reference. So I don’t think I HAVE to change it if I’m not actually making the call from IFTTT. Having said that, there is no doubt there are better ways. I’m just going with what (little) I know works.

You make a good point though, and made me think of something. The only reason I went with the ISY Portal (cloud) method was to utilize IFTTT. Again, it’s a method that I know how to do and make it work. But since I’m now talking about eliminating the IFTTT step, there may indeed be (and probably is) a REST API for the ISY hub itself that could be utilized and make this process much more direct. Hopefully on my LAN. Let me look into that.

Well if it works great go with it. Even a hubAction is going to the cloud but just to ST cloud then back to your hub. I’ve had good luck with it though.

Thank you Nate for your help.

Okay, a bit of research and I have this:

http://userid:password@192.168.xx.xx/rest/nodes/scene_id/cmd/DON

If I put this in the address bar of my Firefox browser (with, of course, the appropriate info substituted), the light turns on.

So, I guess I just need some instruction from a clean slate on how make this call when my ST Virtual Switch is turned on. Remember, all of my experiance with ST started yesterday. :slight_smile:

I think worked with someone on something similar to this using CoRE, a community smart app. It was also to trigger an ISY installation.

Of course. While ST can be crazy frustrating at times theres a great community that can help with most things. As far as what you’re trying to do, since it’s a LAN request you will use hub action. http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/building-lan-connected-device-types/building-the-device-type.html

Basically you’d format the hubAction as needed and add place it in the on () method of your device handler which you can find in the IDE.

All they being said I had totally forgotten about CoRE for something like this. @eibyer has built a great smartapp/rules engine that can very likely do what you want and much much much more. It’s a great tool that you should have even if you don’t use it for this. Either way it’s totally doable. :slight_smile:

@ady624 built it, I’m just a fan :grin:

My bad!! Didn’t mean to steal @ady624’s thunder. I think I’ve seen your name so much on the CoRE threads I just assumed. Lol. Either way it’s a great app that’s worth checking out. :smile:

Okay, from what I’ve been able to figure out, I need to:

Create a Device Handler. I know where to do this, but I have no idea what to do. I see three choices: “From Form”, “From Code”, and “From Template”. I’m guessing I need to use “From Code”, but I have no idea what “code” to put in there. I tried putting my ISY REST call in there (http://userid:password@192.168.xx.xx/rest/nodes/scene_id/cmd/DON), but of course this did not work. Can someone PLEASE just spell it out for me?

Then I need to “publish the Device Handler to your account and pair your device with your SmartThings hub.” No idea what any of that means.

Then something about certification by someone on the “certification team”. No idea what that means either.

Found this thread when searching for Smartthings REST calls. I am just starting my Smartthings journey and intend to buy one soon. I have one question before I actually order.

I currently run another home automation hub that provides function to execute REST calls to get data from external devices. For example I have a thermostat that exposes a REST interface on my LAN and in my home automation hub I use GET and PUT calls to read and write data to/from the thermostat. I presume once I read all the documentation I can do this with Smartthings as well?