My question** …** is there any site that can easily show me how to get a permanent token (or equivalent) and how to get that into my Google Scripts code? Or am I chasing something that does not exist?
Googling this topic shows that lots of folks ask similar questions. But I’m having trouble zeroing into a solution. Seems to me that Smartthings should have a simpler solution to data logging.
My project is, for the next 9’ish months, log 3 ground temperatures under a cabin in northern MN.
I thought I could use a script in Google Sheets to grab the temperature data, twice a day and write it to a Google Sheets spreadsheet.
I’m kinda bad at writing code so I used AI to generate the code. It did a decent job, and after correcting the errors, I got it to run as desired … for one day. As many know, Personal Access Tokens last about 24 hours. Code shown below. Regardless, the code/spreadsheet does exactly what I wanted
The next search was how to get a permanent token. I’ve found no concise answer. It’s one of those things where I want to know the time and the answers are how to build a clock, which leads to how to make the gears, which leads to how to etch metal, which leads to how to mine the metal … and really, all I want to know is the time ![]()
Additionally, my results lead to too many obsolete solutions. Argh!
So my question** …** is there any site that can easily show me how to get a permanent token (or equivalent) and how to get that into my Google Scripts code? Or am I chasing something that does not exist?
My preference is the Google Sheets approach rather than going down the ratholes of spinning up other sites and tools. This is a one-off home project so that I’m not really interested in paying for a service.
For those interested in the Google Script that gathers data from one sensor twice a day (4AM’ish and 4PM’ish), the following works (for one day, then you need another token). It is offered up to those interested as FYI. Don’t ask me how or why it works … I’m not there yet.
// Configuration
const PAT_TOKEN = ‘YourTokenInsideTheParentheses’;
const SENSOR_ID = ‘YourSensorIDInsideTheParentheses’;
const SENSOR_NAME = ‘YourSensorName’;
function fetchAndLogTemperature() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const timestamp = new Date();
// SmartThings endpoint for specific capability status
const url = `https://api.smartthings.com/v1/devices/${SENSOR_ID}/components/main/capabilities/temperatureMeasurement/status\`;
const options = {
'method': 'get',
'headers': {
'Authorization': \`Bearer ${'YourTokenInsideTheParentheses'}\`,
'Accept': 'application/json'
},
'muteHttpExceptions': true
};
try {
const response = UrlFetchApp.fetch(url, options);
const rc = response.getResponseCode();
if (rc === 200) {
const json = JSON.parse(response.getContentText());
// SmartThings structural path
const tempValue = json.temperature.value;
const tempUnit = json.temperature.unit || '°C';
sheet.appendRow(\[timestamp, SENSOR_NAME, SENSOR_ID, tempValue, tempUnit\]);
} else {
sheet.appendRow(\[timestamp, SENSOR_NAME, SENSOR_ID, \`Error: HTTP ${rc}\`, ''\]);
}
} catch (err) {
sheet.appendRow(\[timestamp, SENSOR_NAME, SENSOR_ID, \`Exception: ${err.message}\`, ''\]);
}
}
// Run this function once manually to set up the automated twice-a-day triggers
function setupTriggers() {
// Clear existing triggers to avoid duplicates
const triggers = ScriptApp.getProjectTriggers();
triggers.forEach(t => ScriptApp.deleteTrigger(t));
// Create trigger for 4:00 AM
ScriptApp.newTrigger(‘fetchAndLogTemperature’)
.timeBased()
.everyDays(1)
.atHour(4)
.create();
// Create trigger for 4:00 PM (16:00)
ScriptApp.newTrigger(‘fetchAndLogTemperature’)
.timeBased()
.everyDays(1)
.atHour(16)
.create();
}