Qingping Air monitor lite homeyscript

Okiedokie, I have this now completely up and running… I improved the script with the help and inspiration of this forum, ChatGPT and some apps…

const params = new URLSearchParams();

params.append('grant_type', 'client_credentials');
params.append('scope', 'device_full_access');

const client_id = 'FILL_IN_APPKEY';
const client_secret = 'FILL_IN_APPSECRET';
const credentials = Buffer.from(client_id + ':' + client_secret).toString('base64');

var result = await fetch("https://oauth.cleargrass.com/oauth2/token", {
  method: 'post',
  headers: {
    'Authorization': 'Basic ' + credentials,
  },
  body: params,
});

if (!result.ok)   throw new Error(result.statusText);

const body = await result.json();

console.log(body)

var access_token = body.access_token;

await tag("qingping_access_token", access_token);

// get device data

var result = await fetch("https://apis.cleargrass.com/v1/apis/devices?timestamp=" + Date.now().toString(), {
  method: 'get',
  headers: {
    'Authorization': 'Bearer ' + access_token,
  },
});

if (!result.ok)   throw new Error(result.statusText);

const body2 = await result.json();

var data = body2.devices[0].data

console.log(data)

var {timestamp,temperature,humidity,  co2,pm25, pm10} = data;

console.log(timestamp,temperature,humidity,  co2,pm25, pm10);

await tag("qingping_timestamp", timestamp.value);
await tag("qingping_temperature", temperature.value);
await tag("qingping_humidity", humidity.value);
await tag("qingping_co2", co2.value);
await tag("qingping_pm25", pm25.value);
await tag("qingping_pm10", pm10.value);

Used apps:

  • HomeyScript (see above)
  • Qingping installed NOT via Homekit (topic start)
  • Portal Qingping (mentioned in topic start)
  • Created virtual Device with this Device Capabilities
  • Created this flow

Note in the script, change this:
const client_id = ‘FILL_IN_APPKEY’;
const client_secret = ‘FILL_IN_APPSECRET’;

To import the app into Device Capabilities app, use this TEF:

[tef:AVD:"H4sIAAAAAAACA42Wa4+iSBSG/4tfnQ53hU72g6AoCKiIF3qzmRRQQMmlEAoUJvPfl97uaWe7e0yTkErOqfOe5wVOhR+DADbIh5qP88HjYMFX2uTlUiYMs2tZ3jM3nage1LGK2KG+PGuT8UaSLkGrCC2twImYLO3ruqSmRztQFQ4JRy1LVubKNphxGAoeN5QahxnBNRXoy+XV4w14PP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