# Qingping Air monitor lite homeyscript

**URL:** <https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222>\
**Category:** Devices\
**Tags:** homeyscript\
**Created:** [March 7, 2021, 12:13pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222 "2021-03-07T12:13:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjaadvd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tjaadvd/32/16677_2.png) [@tjaadvd](https://community.homey.app/u/tjaadvd)\
**Post date:** [March 7, 2021, 12:13pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/1 "2021-03-07T12:13:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![WillemB](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/willemb/32/39161_2.png) [@WillemB](https://community.homey.app/u/WillemB)\
**Post date:** [August 30, 2021, 8:20am UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/2 "2021-08-30T08:20:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tjaadvd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tjaadvd/32/16677_2.png) [@tjaadvd](https://community.homey.app/u/tjaadvd)\
**Post date:** [September 7, 2021, 7:24pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/3 "2021-09-07T19:24:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wout\_van\_den\_Dool](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wout_van_den_dool/32/391_2.png) [@Wout\_van\_den\_Dool](https://community.homey.app/u/Wout_van_den_Dool)\
**Post date:** [September 20, 2021, 8:12pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/4 "2021-09-20T20:12:24Z")

</div>

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

And can you share some inspirational flows?

---

<div class="post-metadata">

**Author:** ![Wout\_van\_den\_Dool](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wout_van_den_dool/32/391_2.png) [@Wout\_van\_den\_Dool](https://community.homey.app/u/Wout_van_den_Dool)\
**Post date:** [September 22, 2021, 8:30pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/5 "2021-09-22T20:30:50Z")

</div>

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

I edited the script accordingly:

```auto
'Authorization': 'xxxxxxxxxx', or
'Authorization': 'yyyyyyyyyy',

```

This is the output of the script

❌ Script Error  
⚠ 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)

---

<div class="post-metadata">

**Author:** ![ALhomey](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/alhomey/32/47883_2.png) [@ALhomey](https://community.homey.app/u/ALhomey)\
**Post date:** [December 21, 2021, 1:11pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/6 "2021-12-21T13:11:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tjaadvd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tjaadvd/32/16677_2.png) [@tjaadvd](https://community.homey.app/u/tjaadvd)\
**Post date:** [December 21, 2021, 2:37pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/7 "2021-12-21T14:37:18Z")

</div>

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/](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].

---

<div class="post-metadata">

**Author:** ![tjaadvd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tjaadvd/32/16677_2.png) [@tjaadvd](https://community.homey.app/u/tjaadvd)\
**Post date:** [December 21, 2021, 2:46pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/8 "2021-12-21T14:46:34Z")

</div>

If you’ve created the homeyscript (through [https://my.homey.app](https://my.homey.app)) and it works, you can use it with a flow like this: [Gedeelde Flow | Homey](https://homey.app/nl-nl/flow/AbIrtT/). 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.

---

<div class="post-metadata">

**Author:** ![ALhomey](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/alhomey/32/47883_2.png) [@ALhomey](https://community.homey.app/u/ALhomey)\
**Post date:** [December 22, 2021, 6:34am UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/9 "2021-12-22T06:34:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [December 22, 2021, 7:05am UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/10 "2021-12-22T07:05:51Z")

</div>

You can use the Virtual Devices app for sensors.

---

<div class="post-metadata">

**Author:** ![ALhomey](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/alhomey/32/47883_2.png) [@ALhomey](https://community.homey.app/u/ALhomey)\
**Post date:** [December 22, 2021, 11:49am UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/11 "2021-12-22T11:49:54Z")

</div>

That works. Thanks!

---

<div class="post-metadata">

**Author:** ![cautje](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/cautje/32/328_2.png) [@cautje](https://community.homey.app/u/cautje)\
**Post date:** [March 18, 2023, 4:33pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/12 "2023-03-18T16:33:01Z")

</div>

@tjaadvd

Hi, great you have figured this out!  
I could not use my Qingping on Homey 2023 any more.

I have one question, my script is running fine, i got the richt values.

{  
timestamp: { value: 1679155200 },  
temperature: { value: 21.49 },  
humidity: { value: 52.39 },  
co2: { value: 789 },  
pm25: { value: 1 },  
pm10: { value: 2 }  
}  
{ value: 1679155200 } { value: 21.49 } { value: 52.39 } { value: 789 } { value: 1 }

———————————————————  
✅ Script Success  
↩ Returned: undefined

But when using it in a flow i got an error back, see screenshot.

Maybe you can help me with this?

THX!!!

 ![IMG_5317](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/7/1/715e97decbb2073d54672ed848ec0e1cf41b6cd0.png)

---

<div class="post-metadata">

**Author:** ![Ramon\_Zuidgeest](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ramon_zuidgeest/32/16845_2.png) [@Ramon\_Zuidgeest](https://community.homey.app/u/Ramon_Zuidgeest)\
**Post date:** [March 26, 2023, 3:16pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/13 "2023-03-26T15:16:04Z")

</div>

I Have the same problem. Don’t know what the problem is

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [March 26, 2023, 7:03pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/15 "2023-03-26T19:03:48Z")

</div>

It looks like the script give an error. I can’t test a new script, but i asked ChatGPT to rewrite the old script. Maybe, just maybe it works;

```auto
const { OAuth2 } = Homeyapi('oauth2');
const { Http } = Homeyapi('http');

const oauth2 = new OAuth2({
  client_id: 'CHANGE_THIS_KEY',
  client_secret: '',
  scopes: ['device_full_access'],
  authorization_uri: 'https://oauth.cleargrass.com/oauth2/auth',
  token_uri: 'https://oauth.cleargrass.com/oauth2/token'
});

const http = new Http();

(async () => {
  try {
    const { access_token } = await oauth2.getToken();

    // Store the access token as a tag
    await Homey.flowToken.set('qingping_access_token', access_token);

    // Get device data
    const { devices } = await http.get('https://apis.cleargrass.com/v1/apis/devices', {
      headers: {
        'Authorization': `Bearer ${access_token}`
      }
    });

    // Extract the data from the first device
    const { data } = devices[0];

    // Store the data values as tags
    await tag('qingping_timestamp', data.timestamp.value);
    await tag('qingping_temperature', data.temperature.value);
    await tag('qingping_humidity', data.humidity.value);
    await tag('qingping_co2', data.co2.value);
    await tag('qingping_pm25', data.pm25.value);

    // Log the data values
    console.log(data.timestamp.value, data.temperature.value, data.humidity.value, data.co2.value, data.pm25.value);
  } catch (error) {
    console.error(error);
  }
})();

```

---

<div class="post-metadata">

**Author:** ![Ramon\_Zuidgeest](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ramon_zuidgeest/32/16845_2.png) [@Ramon\_Zuidgeest](https://community.homey.app/u/Ramon_Zuidgeest)\
**Post date:** [March 30, 2023, 7:50pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/16 "2023-03-30T19:50:28Z")

</div>

This is the result of the script:

❌ Script Error  
⚠ ReferenceError: Homeyapi is not defined  
at Qingping3.js:1:20  
at processTicksAndRejections (node:internal/process/task\_queues:96:5)

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [March 31, 2023, 3:57am UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/17 "2023-03-31T03:57:27Z")

</div>

Ok, was worth a try😰

---

<div class="post-metadata">

**Author:** ![Wout\_van\_den\_Dool](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wout_van_den_dool/32/391_2.png) [@Wout\_van\_den\_Dool](https://community.homey.app/u/Wout_van_den_Dool)\
**Post date:** [January 12, 2024, 1:51pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/18 "2024-01-12T13:51:59Z")

</div>

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…

```auto
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](https://developer.qingping.co/main/specification) (mentioned in topic start)
- Created virtual Device with this [Device Capabilities](https://homey.app/nl-nl/app/nl.qluster-it.DeviceCapabilities/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:

```auto
[tef:AVD:"H4sIAAAAAAACA42Wa4+iSBSG/4tfnQ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rv/VXQrglZIxTCPJPSuSaEPxZQgEZLMEnCQNVv/f2037tHeQVLge/djFWX92HHZgVvQqp699yW1iAPgYVUAAPpYgg2EuEIK3gbU+ML5Nn4QyCqi7hd/JLqYQ3JaWEvY6a4osCymceUtY3DRuGKfT7Zze4VUyhjzLwjMy8U3FA9K7+Ja7lFYpi8k6cfTW4qDMUINK+dWC/4I794C7+IHPXGvuJDfYPNth7NrhXG8qKfWvNfcEB98GBj39TuAvP3eDpdxUf4Ll78PwrfJGxwltv/gv0/Af6/0vcxef/gMrfQxXeUBn6rY/wBVThPqpwF1X4A6rwGWp/dL38N7yibvoDqejvvhcYPP79z89/AXaXpZlYCAAA":/tef]

```

---

<div class="post-metadata">

**Author:** ![Ramon\_Zuidgeest](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ramon_zuidgeest/32/16845_2.png) [@Ramon\_Zuidgeest](https://community.homey.app/u/Ramon_Zuidgeest)\
**Post date:** [January 16, 2024, 7:44pm UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/19 "2024-01-16T19:44:19Z")

</div>

I can confirm that is works for me,  
Thanks

---

<div class="post-metadata">

**Author:** ![LukasT](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/lukast/32/83692_2.png) [@LukasT](https://community.homey.app/u/LukasT)\
**Post date:** [August 22, 2024, 7:43am UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/20 "2024-08-22T07:43:33Z")

</div>

Hi,  
thank you very much for manual 🙂 It works fine for me.

```auto
{
  timestamp: { value: 1724311800 },
  battery: { value: 98 },
  temperature: { value: 25.3 },
  humidity: { value: 50.1 },
  co2: { value: 511 },
  pm25: { value: 3 },
  pm10: { value: 3 }
}
{ value: 1724311800 } { value: 25.3 } { value: 50.1 } { value: 511 } { value: 3 } { value: 3 }

———————————————————
✅ Script Success
↩️ Returned: undefined

```

But please could you help me with Flow ? I am not able to update it ☹

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/b/bb58541d1cdfd6cb63e50d77fd1634268a776599.png)

Thank you very much for any help.  
Lukas

---

<div class="post-metadata">

**Author:** ![Wout\_van\_den\_Dool](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wout_van_den_dool/32/391_2.png) [@Wout\_van\_den\_Dool](https://community.homey.app/u/Wout_van_den_Dool)\
**Post date:** [August 22, 2024, 8:12am UTC](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222/21 "2024-08-22T08:12:15Z")

</div>

I don’t know why, but I have it as an and statement:

 ![IMG_2213](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/8/189ce9d39a5571b91f087d84e349228fa8f48681.png)

[https://homey.app/f/Sj99V5](https://homey.app/f/Sj99V5)

[Next page](https://community.homey.app/t/qingping-air-monitor-lite-homeyscript/44222.md?page=2)
