Postman - Executing Commands on device

devices
samsungsmartthings
api

(Jack) #1

Hi,
I am trying to use the Smartthings Rest API to execute commands, on & off, on my switch. I use this for reference: https://smartthings.developer.samsung.com/develop/api-ref/st-api.html#operation/executeDeviceCommands. For the request body in Postman, I use the below. The response is successful, as it shows ‘{ }’. However, my switch isn’t turning on. What may be wrong with my request?

{
  "commands": [
    {
      "component": "main",
      "capability": "switch",
      "command": "on"
    }
  ]
}

#2

What happens if you just send this as body:

[{"component":"main","capability":"switch","command":"on"}]

That’s what the st-command from the SmartThings CLI package sends as body when I ask it to switch on a light bulb, and that works:
$ /home/ok/cli/st command device --id <id-here> --component main --capability switch --command on

OK

Edited to ensure correct double quotes used in JSON blob.


(Jack) #3

It gives me a 422 error which is shown below.

{
“requestId”: “f85c9971-f0ba-d49c-e94e-e5ba29a561c7”,
“error”: {
“code”: “ConstraintViolationError”,
“target”: null,
“message”: “The request is malformed.”,
“details”: [
{
“code”: “NotValidValue”,
“target”: “[0].capability”,
“message”: “null is not a valid value.”,
“details”: []
}
]
}
}


(Raj Karamchedu) #4

@Jack You probably have seen this doc? https://smartthings.developer.samsung.com/develop/guides/smartapps/working-with-devices.html#Example-with-Postman

If you get a 200 ok then I would look at the actual device itself. When you say “my switch isn’t turning on” do you mean a bulb? If it is actual switch, then how are you ascertaining whether the switch is turning on or not? Looking at your mobile app?


(Raj Karamchedu) #5

Looks like it is not seeing the correct device id?


#6

Note that you can’t cut’n’paste the JSON from my original post (fixed now), as the double quotes (") were prettified and turned into different characters. If you use the wrong quotes, you get your 422. The JSON below should be okay as I’ve added some formatting.

Both of these work fine for me:

$ curl -H 'Authorization: Bearer: MY_AUTH_HERE' -d 
'[{"component":"main","capability":"switch","command":"off"}]'
"https://api.smartthings.com/v1/devices/1ab89e69-1cde-4887-aced-b80daa2d7fbd/commands"
$ curl -H 'Authorization: Bearer: MY_AUTH_HERE' -d 
'{"commands":[{"component":"main","capability":"switch","command":"on"}]}'
"https://api.smartthings.com/v1/devices/1ab89e69-1cde-4887-aced-b80daa2d7fbd/commands"

So it looks like you can skip some of the wrapping.

OK


(JIm) #7

I have looked at the example using postman. I obtain the personal access token. When I execute the command in Postman it returns a list of about 6 devices. None of which are mine. They are all simulated devices called door1, door2, etc.

Should I not be getting my own devices?


(Raj Karamchedu) #8

@Jwwhite I assume you got the personal access token from this page? https://account.smartthings.com/login which is correct. Device list you will get will contain devices that are associated with your Location. In turn, the Location is associated with your Samsung account. In the past, a few of us pulled our hair in confusion about which one is our Samsung account and which one is SmartThings, when we use the same email id, such as raj.karamchedu@smartthigns.com, in my case for both the accounts. Mercifully when you click on the above PAT page, it will ask you to login to the correct account, either Samsung or SmartThings. You will get the listing of the devices that are in the account you use to generate the PAT (actually, more exactly, you will get the listing of the devices that are associated with the Location in the account you use to generate the PAT). So, in a nutshell, it helps to check if you generated your PAT with the correct account, i.e., to which your Location is associated and devices are present. Sorry if my answer felt like short story long.


(JIm) #9

I figured out what is going on, just don’t know how to fix it.

I had configured a second location for testing. Don’t really use it anymore but it has the simulated devices showing up in Postman. I tried creating a new access token selecting all locations, etc. But it still only shows that test location, not my main location.

By the way, using the link you mentioned https://account.smartthings.com/login, just takes me to my main ide. Where do you go from there to get the token? I used the link in the writeup which takes me to a create token page.

I tried deleting the test location then creating a new token. With that one I get nothing in the list.


(Raj Karamchedu) #10

@Jwwhite If it is already taking you to your main IDE, then I suggest this: log out of the IDE. Clear out the browser cache (try an cognito window). The idea is to force it to stay clean and ask you to login. This might help.


(JIm) #11

I tried a different browser. Same result. This is what I get. For some reason it isn’t linking to my location.

{
“items”: [],
"_links": {
“next”: {
“href”: “https://api.smartthings.com/devices?max=50&shard_id=na02&offset=0
},
“previous”: null
}
}


(Raj Karamchedu) #12

@Jwwhite yes yes, you are on the right track. Click on that url. It will take you to the second page where you will see your devices. If you are doing this from Postman, after you click on the url, you may have to make sure that Bearer token field is selected.


(JIm) #13

AHHHH. Now I get it. Thanks for the help. That worked.