02.10.2025, 10:55
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
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