Nest恒温器恢复原设定温度脚本500错误排查求助
Nest恒温器脚本恢复设定温度时触发500内部错误排查
问题描述
我为Nest恒温器编写了一段脚本,需求是:存储当前加热设定温度(settemp),先以12℃加热6秒,再恢复至之前的设定温度。控制台中Logger.log(settemp)始终能显示正确数值,但执行恢复温度的命令时,总是返回500内部错误:
{ "error": { "code": 500, "message": "Internal error encountered.", "status": "INTERNAL" } }
怀疑settemp与sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat命令存在冲突,调整data1和options1位置等方法均未解决问题。
涉及代码
try { // url fetch to call api const response = UrlFetchApp.fetch(url + endpoint, params); const responseCode = response.getResponseCode(); const responseBody = JSON.parse(response.getContentText()); // log responses console.log("Response code: " + responseCode); console.log(responseBody); // get devices const devices = responseBody['devices']; //console.log(devices); devices.forEach(device => { if (outside_temp < 15) { var tempCelcius = device['traits']['sdm.devices.traits.Temperature']['ambientTemperatureCelsius'].toFixed(1); //Logger.log('MODE' + JSON.stringify(data)); //if (sdm.devices.commands.ThermostatMode.SetMode == OFF) return const settemp = device['traits']['sdm.devices.traits.ThermostatTemperatureSetpoint']['heatCelsius'].toFixed(1); Logger.log('settemp: ' + settemp); // set the endpoint const endpoint1 = '/enterprises/' + PROJECT_ID + '/devices/' + DOWNSTAIRS_THERMOSTAT + ':executeCommand'; const data = { 'command' : 'sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat', 'params' : { 'heatCelsius': 12, } } const options = { 'headers': headers, 'method': 'post', 'payload': JSON.stringify(data), 'muteHttpExceptions': true } Logger.log(JSON.stringify(data)); try { // try calling API const response = UrlFetchApp.fetch(url + endpoint1, options); Logger.log(response.getContentText()); } catch(e) { console.log('Error: ' + e); } Utilities.sleep(6000); try { // try calling API const data1 = { 'command' : 'sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat', 'params' : { 'heatCelsius': settemp, } } const options1 = { 'headers': headers, 'method': 'post', 'payload': JSON.stringify(data1), 'muteHttpExceptions': true } Logger.log(JSON.stringify(data1)); Logger.log('settemp: ' + settemp); const response1 = UrlFetchApp.fetch(url + endpoint1, options1); Logger.log(response1.getContentText()); } catch(e) { console.log('Error: ' + e); } } }); } catch(e) { console.log('Error: ' + e); }
排查与修复方向
- 数据类型不匹配:
toFixed(1)返回的是字符串类型,但Nest SDM API要求heatCelsius为数值类型。控制台打印字符串看似正常,但API接收时会解析失败触发内部错误。
修复方法二选一:// 方法1:存储时保留数值类型,打印时再格式化 const settemp = device['traits']['sdm.devices.traits.ThermostatTemperatureSetpoint']['heatCelsius']; Logger.log('settemp: ' + settemp.toFixed(1)); // 方法2:若需格式化后存储,恢复时转为数值 const settemp = device['traits']['sdm.devices.traits.ThermostatTemperatureSetpoint']['heatCelsius'].toFixed(1); // ... const data1 = { 'command' : 'sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat', 'params' : { 'heatCelsius': parseFloat(settemp), } } - API调用间隔过短:6秒的等待时间可能不足以让设备完成状态同步,尝试延长
Utilities.sleep()至10000毫秒(10秒)。 - 参数格式冗余:移除
data1中heatCelsius值末尾的多余逗号,部分API对JSON格式的冗余符号敏感。 - 单独验证恢复命令:手动传入固定数值(如20)替换
settemp,若命令执行正常,则可确认是settemp的类型问题。
内容的提问来源于stack exchange,提问作者Permafrost
相关产品推荐
相关产品推荐

