Hi all!
Trying to build a widget with Scriptable that authenticates into Netatmo and grabs the air quality from my Healthy Home Coach sensors - mostly looking for CO2 levels.
I’m completely new to JavaScript and having a lot of trouble getting the expected responses from my POST requests. I have an iOS shortcut that works perfectly but can’t translate this into JavaScript myself.
The code I have so far (forgive the messiness) is:
let clientId = "*insert own ID*"
let clientSecret = "*insert own secret*"
let username = "*insert own ID*"
let password = "*insert own password*"
let homeId = "*insert own ID*"
let roomId = "*insert own ID*"
let macId = "*insert own ID*"
let scope = "read_homecoach"
let authURL = "https://api.netatmo.com/oauth2/token"
let r = new Request(authURL);
r.method = "POST"
r.body = JSON.stringify({
"grant_type": "password",
"client_id": clientId,
"client_secret": clientSecret,
"username": username,
"password": password,
"scope": scope,
"Content-Type": "application/json; charset=utf8"
})
let json = await r.loadJSON();
let accessToken = json['access_token'];
log(json)
let coachURL = "https://api.netatmo.com/api/gethomecoachsdata?device_id=*ADD ID*"
let r2 = new Request(coachURL);
r2.headers = { 'Authorization': 'Bearer ' + accessToken};
let json2 = await r2.loadJSON();
let time = json2['body.devices.[0].dashboard_data.time_utc'];
let co2 = json2['body.devices.[0].dashboard_data.CO2'];
log(co2)
let widget = createWidget();
if (config.runsInWidget) {
let widget = createWidget();
Script.setWidget(widget);
Script.complete();
}
function createWidget() {
let w = new ListWidget();
let widgetText = w.addText(co2);
widgetText.textSize = 15;
let widgetSubText = w.addText(time);
widgetText.textSize = 13;
return w
}
Their developer documentation can be found here.