This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Smartthings status update
#12
(02.10.2025, 18:44)Ozturker Wrote: I receıved below 422
Code:
* string: Request body: {"commands":[{"capability":"switch","command":"on","component":"main"}]}

* string: SmartThings response code: 422

* string: SmartThings response body: {"requestId":"391879646930462279","error":{"code":"ConstraintViolationError","message":"The request is malformed.","details":[{"code":"BodyMalformedError","target":"httpRequestBody","message":"The request body is malformed and cannot be processed by server.","details":[]}]}}

Here is the full code
Code:
local https = require('ssl.https')
local json  = require('json')
local ltn12 = require('ltn12')



-- ️ put your smartthings pat here
local token = '*************'

local livingr_cam_id = '**************************'


-- Camera KNX group objects (choose addresses as needed)
local ga_cam_onoff_set     = grp.create({ datatype = dt.bool,    address = '33/4/1', name = 'Camera On/Off' })
local ga_cam_onoff         = grp.create({ datatype = dt.bool,    address = '33/4/2', name = 'Camera On/Off Status' })
local ga_cam_motion        = grp.create({ datatype = dt.bool,    address = '33/4/11', name = 'Camera Motion Detected' })
local ga_cam_sound         = grp.create({ datatype = dt.bool,    address = '33/4/12', name = 'Camera Sound Detected' })


local cam_set = grp.getvalue(ga_cam_onoff_set)  -- Set
local cam_set_status = grp.getvalue(ga_cam_onoff)  -- Status



-- Helper to turn camera on or off using a raw JSON string
local function set_camera_onoff(cam_id, state)
  local cmd = state and 'on' or 'off'
 
local body = require('json').encode({
  commands = {
    {
      component = 'main',
      capability = 'switch',
      command = cmd,
    }
  }
})


  log('Request body: ' .. body)

  local sink_tbl = {}
  local res, code = https.request({
    url     = 'https://api.smartthings.com/v1/devices/' .. cam_id .. '/commands',
    method  = 'POST',
    headers = {
      ['authorization'] = 'Bearer ' .. token,
      ['content-type']  = 'application/json; charset=utf-8',
      ['accept']        = 'application/json',
    },
    source = ltn12.source.string(body),
    sink   = ltn12.sink.table(sink_tbl),
  })


  local response_body = table.concat(sink_tbl)
  log('SmartThings response code: ' .. tostring(code))
  log('SmartThings response body: ' .. response_body)
  return res, code, response_body
end



if cam_set == true then
    if cam_set_status == false then
        set_camera_onoff(livingr_cam_id, true)
  end
elseif cam_set == false then
    if cam_set_status == true then
        set_camera_onoff(livingr_cam_id, false)
    end
end
     


Here is the working postman cURL
Code:
curl --location 'https://api.smartthings.com/v1/devices/***************************/commands' \
--header 'Content-Type: application/json' \
--header 'Authorization: **********' \
--data '{
    
              "commands": [
                {
                  "component": "main",
                  "capability": "switch",
                  "command": "on"
} ]
}'


Thank you for the support

Hi, try to add this in the header, for me works
['content-length'] = #body,
Best regards Cristian
Reply


Messages In This Thread
Smartthings status update - by CristianAgata - 02.01.2025, 11:08
RE: Smartthings status update - by admin - 02.01.2025, 11:35
RE: Smartthings status update - by admin - 02.01.2025, 14:30
RE: Smartthings status update - by admin - 02.10.2025, 11:04
RE: Smartthings status update - by Ozturker - 02.10.2025, 12:56
RE: Smartthings status update - by admin - 02.10.2025, 13:07
RE: Smartthings status update - by Ozturker - 02.10.2025, 18:44
RE: Smartthings status update - by CristianAgata - 03.10.2025, 10:02
RE: Smartthings status update - by Ozturker - 03.10.2025, 11:25
RE: Smartthings status update - by admin - 03.10.2025, 09:53

Forum Jump: