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
#6
Hello all

I am successfully reading the status of individual devices from Smartthings

But when I am trying to set the status via command I am receiving 422.

Same code works from Postman. Is it a string issue?

Thank you in advanced for the comments


Code:
local https = require('ssl.https')
local ltn12 = require('ltn12')
local token = '++++++++'
local cam_id = '++++++++++'
-- Function 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 = string.format([[
{
  "commands": [
    {
      "component": "main",
      "capability": "switch",
      "command": "%s"
    }
  ]
}
]], 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 = {
      ['Content-Type']  = 'application/json',
      ['Authorization'] = 'Bearer ' .. token,
      ['Accept']        = 'application/json',
      ['User-Agent']    = 'Mozilla/5.0',
      ['Content-Length'] = tostring(#body)
    },
    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
-- Usage:
set_camera_onoff(cam_id, true)  -- to turn ON
set_camera_onoff(cam_id, false) -- to turn OFF
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
Smartthings command execution not works - by Ozturker - 02.10.2025, 10:55
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 Ozturker - 03.10.2025, 11:25
RE: Smartthings status update - by admin - 03.10.2025, 09:53

Forum Jump: