Anyone have experience with the Netatmo documentation?

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.

Would be nice if @simonbs could add HomeKit support to Scriptable…

(in Shortcuts it takes a single action to read the CO2 value of my Healthy Home Coach thanks to HomeKit)

I’d love to add HomeKit support one day! :smiley:

2 Likes

Does anyone know why my first authentication request isn’t working? Or know a good guide on this?

Shortcuts made this so simple; my lack of JavaScript knowledge makes this quite a challenge.

Can you provide any details on the response you get as the failure? They can often, but admittedly not always, provide a clue as to what the issue is.

I’ve managed to get it working!

Code snippet here.

Requires BetterRequest module.