Qingping Air monitor lite homeyscript

Hi all,

I made a quick integration to get data from Qingping+ for the Qingping Air Monitor Lite device. Be sure to use the Qingping+ app on your phone and use an account. The device must be set in qingping mode instead of homekit mode.
Be sure to adjust the Authorization token as described here: https://developer.qingping.co/main/oauthApi.

const params = new URLSearchParams();

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

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

var result = await fetch("https://oauth.cleargrass.com/oauth2/token", {

  method: 'post',

  headers: {

    'Authorization': 'Basic CHANGE_THIS_KEY',

  },

  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} = data;

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

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);
3 Likes

Hello Thomas,

Thanks for the integration.
Could you help me how to get the Authorization token? I cant really figure it out.

And do i use this code with HomeyScript?

Yes! Do you have a app key and app secret from https://developer.qingping.co/personal/permissionApply?

I have a key and secret, do I need to do anything else?

And can you share some inspirational flows?

I retrieved this from developer page
Apkey:xxxxxxxxx
App secret: yyyyyyy

I edited the script accordingly:

'Authorization': 'xxxxxxxxxx', or
'Authorization': 'yyyyyyyyyy',

This is the output of the script

:x: Script Error
:warning: Error: Bad Request
at Qingping.js:21:25
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async HomeyScriptApp.runScript (/app.js:312:22)
at async Object.runScript (/api.js:24:18)

Hi @tjaadvd , thanks for your script. I used it for my Homey except I inserted my app key from Qingping, where you typed ‘change_this_key’. I get the same script error output as Wout in the last message. Not sure where my app secret key goes.

Can you help? I’m afraid I’m new to scripts so am quite confused by how they work! Appreciate your time.

Hi guys,

The trick to generate the Authorization key is to combine the client_id (App Key) and client_secret (App Secret) and ‘encode’ them with base64. Base64 is an encoding originally build for email attachments.

  • Go to https://www.base64encode.org/
  • paste the App Key, then the ‘:’ character, and then the App Secret. For example “_GoSI9xxx:c2ba57537b5311eb9e7fxxxxxxxxxxxx”.
  • Click on the convert button
  • Copy the resulting encoded string (for example “X0dvU0k5eU…”)
  • Then replace CHANGE_THIS_KEY with the string that you copied. The line now looks like this: ‘Authorization’: ‘Basic X0dvU0k5eU…’,

That should do the trick! This script uses the first qingping device in your account. If you have multiple you can create a new script and change devices[0] to devices[1].

If you’ve created the homeyscript (through https://my.homey.app) and it works, you can use it with a flow like this: Gedeelde Flow | Homey. That flow runs every 10 minutes, and uses the homeyscript in the ‘And’ (conditions) section. The first actions that set Logic aren’t important. I’ve created a virtual device in Homey of type ‘sensor’ and this script sets the values for that virtual device. In order to create a virtual device in Homey you have to go to Settings in the app → Experiments → Virtual devices.

Great! thank you, I can now pull air quality data via homeyscript. unfortunately it seems that the Homey app now no longer includes virtual sensors, only some other virtual types e.g. switch. the functionality might be suspended for now.

You can use the Virtual Devices app for sensors.

1 Like

That works. Thanks!

1 Like