Google Sheets data logging with persistent token

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 :slight_smile:

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();

}

I’d use the SmartThings CLI to periodically fetch the history of the devices and go from there.

USAGE
  $ smartthings devices:history [ID] [-h] [-p <value>] [-t <value>] [--language <value>] [-j] [-y] [-o <value>] [-A
    <value>] [-B <value>] [-L <value>] [-U]

ARGUMENTS
  ID  the device id

FLAGS
  -A, --after=<value>   return events newer than or equal to this timestamp, expressed as an epoch time in milliseconds
                        or an ISO time string
  -B, --before=<value>  return events older than than this timestamp, expressed as an epoch time in milliseconds or an
                        ISO time string
  -L, --limit=<value>   [default: 20] maximum number of events to return
  -U, --utc             display times in UTC time zone. Defaults to local time

COMMON FLAGS
  -h, --help             Show CLI help.
  -j, --json             use JSON format of input and/or output
  -o, --output=<value>   specify output file
  -p, --profile=<value>  [default: default] configuration profile
  -t, --token=<value>    the auth token to use
  -y, --yaml             use YAML format of input and/or output
  --language=<value>     ISO language code or "NONE" to not specify a language. Defaults to the OS locale

DESCRIPTION
  get device history by device

Google sheets is overkill in my opinion.

Ideally PAT generation would be streamlined so you could get a PAT with your required scope in a second, but that still wouldn’t help if you want to run scripts automatically.

You can get create and renew tokens using the processes used in API Access apps but the belated gotcha there is that having an API Access app could soon be the trigger for API subscriptions unless additional usage allowances that haven’t yet been mentioned come into play.

It may be that the SmartThings CLI could be used to do the API accesses for you.

However a trick I use, mostly in manual tasks, is to issue a command using the CLI (e.g. smartthings locations) to make sure the CLI has a valid access token, and then extract the access token from the CLI credentials file where it is in plain text.

Thank you to both of you. It confirms there isn’t an easy (for me) method. I’ll need to learn more … but at a later date.

I’m thinking I’ll go with a SONOFF Zigbee Bridge Pro Gateway and the eWeLink app that retains a lot more data.

On a different note, I’m struggling with an “open box” V2 hub stuck with a solid blue light and 1.0.49-13 app version. Argh! Currently working through various methods to recover it.

If it’s not one thing … it’s another

Solid green now … making progress. Now I wait for it to get its update and hopefully get full functionality.

Closing thoughts for those who stumble upon this. I think the Smartthings hub is kaput, It shows connected to the local network even if the ethernet cable is not. I’ll work that problem later … maybe.

So my data collection plan is for the SONOFF Zigbee Bridge Pro Gateway and the eWeLink app per above. It appears there are good Smartthings alternatives but it also appears that takes more effort on my part. Again, something for a later day.

Thanks all.