@TapioX you need to follow @TAustin instructions following that link and use the script below he provided me.
Currently it only pulls data for one device. I am just learning javascript so am trying to get it to work with multiples.
//
// This app retrieves temperature & humidity values from a SmartThings
// device via the SmartThings RESTful API, and posts them to a Google spreadsheet.
//
// User must be sure the sheet being posted to has three column headings: âDateâ,
// âTemperatureâ, and âHumidityâ
//
// The following constants in this file must be modified by the user:
// KEYFILE, GOOGLEDOCID, GSHEET_SHEETNAME, MYTOKEN, MYDEVICEID, RUNFREQUENCY
//
//GOOGLESHEETS CONSTANTS
const KEYFILE = â./xxxxxxxxxxxxxxxxxxxxxxxx.jsonâ
const GOOGLEDOCID = âxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxâ
const GSHEET_SHEETNAME = âTempTrackerâ
const { GoogleSpreadsheet } = require(âgoogle-spreadsheetâ);
const doc = new GoogleSpreadsheet(GOOGLEDOCID);
//SMARTTHINGS CONSTANTS
const MYTOKEN = âxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxâ
const MYDEVICEID = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxâ
const TEMP = {âcomponentâ: âmainâ, âcapabilityâ: âtemperatureMeasurementâ, âattributeâ: âtemperatureâ}
const HUMIDITY = {âcomponentâ: âmainâ, âcapabilityâ: ârelativeHumidityMeasurementâ, âattributeâ: âhumidityâ}
const STAPIHOST = âapi.smartthings.comâ
// How often to fetch the device values and post in spreadsheet (in seconds)
RUNFREQUENCY = 60
//HTTP CONSTANTS
const HTTP_OK = 200
//REQUIRED NODE MODULES
const https = require(âhttpsâ)
async function update_spreadsheet(tempval, humidityval) {
let sheet = doc.sheetsByTitle[GSHEET_SHEETNAME];
let dateTime = new Date();
let dateTimestr = String(dateTime).replace(/GMT.+/, ââ);
try {
await sheet.addRow({ Date: dateTimestr, Temp: tempval, Humidity: humidityval});
} catch (err) {
console.log (â\tERROR: Unable to add row to sheetâ);
}
}
async function initauth() {
const creds = require(KEYFILE);
await doc.useServiceAccountAuth(creds);
await doc.loadInfo();
}
function get_value(deviceinfo) {
return new Promise((resolve, reject) => {
let options = {};
options.headers = {};
options.host = STAPIHOST;
options.path = `/v1/devices/${MYDEVICEID}/components/${deviceinfo.component}/capabilities/${deviceinfo.capability}/status`;
options.method = 'GET';
options.headers.accept = 'application/json';
options.headers.authorization = 'Bearer ' + MYTOKEN;
try {
const req = https.request(options, res => {
let body = '';
res.on('data', data => {body += data})
res.on('end', () => {
resolve({statusCode: res.statusCode, data: body});
})
})
req.on('error', error => {
reject('SmartThings API request failed: ' + error.message);
})
req.on('timeout', () => {
console.log ('SmartThings API request timed out');
req.destroy();
})
req.end();
} catch(error) {
reject('HTTPS failed: ' + error.message);
}
})
}
async function update() {
try {
let tempresult = await get_value(TEMP);
if (!tempresult) return;
if (tempresult.statusCode != 200) return;
const temp = JSON.parse(tempresult.data);
let humidityresult = await get_value(HUMIDITY);
if (!humidityresult) return;
if (humidityresult.statusCode != 200) return;
const humidity = JSON.parse(humidityresult.data);
update_spreadsheet(temp[TEMP.attribute].value, humidity[HUMIDITY.attribute].value);
} catch(error) {
console.log ('UPDATE FAILED: ' + error);
}
}
// MAIN ----------------------------------------------------------------
console.log(â\nGooglesheet Updater from SmartThings API v1.0\nâ)
initauth()
.then(result => {
update();
setInterval(function() {
update();
}, RUNFREQUENCY * 1000);
})
.catch(error => {
console.log (âError:â, error);
})