Postman - Executing Commands on device - Can't set arguments

Hi,
I am following SmartThings API doc: API | SmartThings Developers and I am trying to to send a POST request to my device. I have a token etc… and it works fine for switch but I can’t find a way to put arguments correctly for switchLevel. Maybe someone can share a working example?

THIS WORKS IN POSTMAN
POST: https://api.smartthings.com/v1/devices/{deviceId}/commands
BODY:

[
{
“component”:“main”,
“capability”:“switch”,
“command”:“off”,
“argument”:
}
]

THIS DOESN"T WORK IN POSTMAN
POST: https://api.smartthings.com/v1/devices/{deviceId}/commands
BODY:

[
{
“component”:“main”,
“capability”:“switchLevel”,
“command”:“setLevel”,
“arguments”: [{
“level”: {
“integer”: 40
}
}]
}
]

Error response:
{
“requestId”: “389BF36E-CB1B-49E2-9CAF-9512A050115A”,
“error”: {
“code”: “ConstraintViolationError”,
“message”: “The request is malformed.”,
“details”: [
{
“code”: “UnprocessableEntityError”,
“target”: “[0].arguments.[0]”,
“message”: “invalid NUMBER type”,
“details”:
}
]
}
}

There is definitely something wrong with the arguments part but I can’t find a way to construct that JSON correctly. I don’t find any examples in the documentation that would show how to construct arguments.

Side note: This page is not helpful and I think it should include examples: Capabilities Reference | SmartThings Developers

I always get tied in a knot with the arguments. Given the error message, I’d suggest you probably don’t need the “level” key. Have you tried just "arguments": [ "integer": 40 ] ?

Thank you for the suggestion but it’s also not working :confused:

I think I lost over an hour on it, trying to find a different way. If only there would be a documentation for this API :slight_smile:

Have you tried without the braces as I suggested? [ "integer": 40 ] not [{ "integer": 40 }]. Or maybe leave out "integer":?

Sometimes the examples in the API page are useful but I can’t expand them on my mobile. Not sure if they are also broken on desktop browsers.

2 Likes

I tried. Since “arguments” is an array, I think brackets are needed inside it for the proper JSON formatting.

But it seems like just the number, without anything, works!!! :slight_smile: Nice!!! Thank you!

If someone who owns the API doc reads this, please consider adding few examples.

Thank you for your feedback, I’ll create a request for the engineering team asking for more samples on how to send the command requests to the API.

However, the syntax needed when we work with the Rules API is similar to the one you used before:

{
    "command": {
        "devices": [
            "deviceID"
        ],
        "commands": [
            {
                "component": "main",
                "capability": "switchLevel",
                "command": "setLevel",
                "arguments": [{
                    "integer": 20
                }]
            }
        ]
    }
}

There’s already a request to provide more samples for the Rules API documentation and also, we have this Sample Rules repository where you can see the usage of other capabilities.

There seems to be a particular issue in the docs where payload samples are presented but can’t be expanded to see the content. It seems to particularly apply to the Devices endpoint.

Mmmm…maybe no sample has been assigned there yet. Let me check with the team. Thanks!

Thank you for sharing additional links. I don’t work with rules, not for now at least, but I will try to use these samples if I hit another issue.

Regarding the syntax you shared, it’s not working unfortunately:

Still, the only way to make it work is what @orangebucket shared (thanks again!)

No, the one I shared is used when we create a Rule that sends a command to the switchLevel capability.
For a direct command to the device through the API, the syntax shared by Graham is correct.

This is due to the Rules API grammar design. Rules are polymorphic and can accept many different payloads, hence, they go through a translation that parses the device commands.
Conversely, direct commands are specific to the device and don’t need a translation.

Another example is:
For devices that use the colorControl capability, the syntax for a direct command is the following:

{
   "commands":[
      {
         "component":"main",
         "capability":"colorControl",
         "command":"setColor",
         "arguments":[
            {
               "hue":"decimal",
               "saturation":"decimal"
            }
         ]
      }
   ]
}

But, when working with Rules, it changes to:

{
   "command":{
      "devices":[
         "deviceId"
      ],
      "commands":[
         {
            "component":"main",
            "capability":"colorControl",
            "command":"setColor",
            "arguments":[
               {
                  "map":{
                     "hue":{
                        "decimal":"x"
                     },
                     "saturation":{
                        "decimal":"x"
                     }
                  }
               }
            ]
         }
      ]
   }
}
1 Like