I wrote this script for a Nest thermostat. It has to store the current set heating temperature (settemp
), heat for 6 seconds at 20°C then heat again to the previous set temperature (settemp
that was defined prior to the heating value change).
This is (part of) my code:
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);
}
Logger.log(settemp)
always displays the right value in the console, but I get that error every time:
{ "error": { "code": 500, "message": "Internal error encountered.", "status": "INTERNAL" } }
May it be caused by a conflict between settemp
and 'command' : 'sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat'
?
Would you know how to solve that error please? I tried many many things, including moving const data1
and const options1
in different places, but nothing worked.
A huge thanks in advance for your help!