Skip to content

Commit 3e03efd

Browse files
committed
Add option to use compensated values
1 parent c1f0876 commit 3e03efd

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

config.schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
"type": "number",
2222
"default": 60000,
2323
"minimum": 1000
24+
},
25+
"useCompensatedValues": {
26+
"title": "Use Compensated Values",
27+
"type": "boolean",
28+
"default": false
2429
}
2530
}
2631
}
@@ -43,6 +48,11 @@
4348
{
4449
"key": "sensors[].pollingInterval",
4550
"placeholder": 60000
51+
},
52+
{
53+
"key": "sensors[].useCompensatedValues",
54+
"type": "checkbox",
55+
"title": "Use Compensated Values"
4656
}
4757
]
4858
}

src/index.ts

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ interface AirGradientData {
1515
locationId: number;
1616
pm01: number;
1717
pm02: number;
18+
pm02Compensated?: number;
1819
pm10: number;
1920
pm003Count: number;
2021
atmp: number;
22+
atmpCompensated?: number;
2123
rhum: number;
24+
rhumCompensated?: number;
2225
rco2: number;
2326
tvoc: number;
2427
wifi: number;
@@ -37,8 +40,10 @@ interface AirGradientData {
3740
interface SensorConfig {
3841
serialno: string;
3942
pollingInterval?: number;
43+
useCompensatedValues?: boolean;
4044
}
4145

46+
4247
class AirGradientPlatform implements DynamicPlatformPlugin {
4348
public readonly log: Logging;
4449
public readonly api: API;
@@ -94,13 +99,15 @@ class AirGradientSensor {
9499
private readonly serviceTemp: Service;
95100
private readonly serviceCO2: Service;
96101
private readonly serviceHumid: Service;
102+
private readonly useCompensatedValues: boolean;
97103

98104
constructor(platform: AirGradientPlatform, accessory: PlatformAccessory, sensorConfig: SensorConfig) {
99105
this.platform = platform;
100106
this.accessory = accessory;
101107
this.log = platform.log;
102108
this.serialno = sensorConfig.serialno;
103109
this.pollingInterval = sensorConfig.pollingInterval || 60000; // Default to 1 minute
110+
this.useCompensatedValues = sensorConfig.useCompensatedValues || false;
104111

105112
// Construct the local API URL using the serialno
106113
this.apiUrl = `http://airgradient_${this.serialno}.local/measures/current`;
@@ -123,32 +130,72 @@ class AirGradientSensor {
123130
}
124131

125132
private setupCharacteristics() {
126-
this.service.getCharacteristic(hap.Characteristic.AirQuality)
127-
.on('get', this.handleAirQualityGet.bind(this));
133+
if (this.data) {
134+
// Use compensated values if enabled and available, otherwise fallback to the default values
135+
const pm2_5 = this.useCompensatedValues && this.data.pm02Compensated !== undefined
136+
? this.data.pm02Compensated
137+
: this.data.pm02;
138+
const temp = this.useCompensatedValues && this.data.atmpCompensated !== undefined
139+
? this.data.atmpCompensated
140+
: this.data.atmp;
141+
const humidity = this.useCompensatedValues && this.data.rhumCompensated !== undefined
142+
? this.data.rhumCompensated
143+
: this.data.rhum;
144+
145+
// Other values remain the same
146+
const pm10 = this.data.pm10;
147+
const tvoc = this.data.tvocIndex;
148+
const nox = this.data.noxIndex;
149+
const co2 = this.data.rco2;
128150

129-
this.service.getCharacteristic(hap.Characteristic.PM2_5Density)
130-
.on('get', this.handlePM2_5DensityGet.bind(this));
151+
if (typeof pm2_5 === 'number' && isFinite(pm2_5)) {
152+
this.service.updateCharacteristic(hap.Characteristic.PM2_5Density, pm2_5);
153+
} else {
154+
this.log.warn('Invalid PM2.5 value:', pm2_5);
155+
}
131156

132-
this.service.getCharacteristic(hap.Characteristic.PM10Density)
133-
.on('get', this.handlePM10DensityGet.bind(this));
157+
if (typeof pm10 === 'number' && isFinite(pm10)) {
158+
this.service.updateCharacteristic(hap.Characteristic.PM10Density, pm10);
159+
} else {
160+
this.log.warn('Invalid PM10 value:', pm10);
161+
}
134162

135-
this.service.addCharacteristic(hap.Characteristic.VOCDensity)
136-
.on('get', this.handleVOCDensityGet.bind(this));
163+
if (typeof tvoc === 'number' && isFinite(tvoc)) {
164+
this.service.updateCharacteristic(hap.Characteristic.VOCDensity, tvoc);
165+
} else {
166+
this.log.warn('Invalid TVOC value:', tvoc);
167+
}
137168

138-
this.service.addCharacteristic(hap.Characteristic.NitrogenDioxideDensity)
139-
.on('get', this.handleNitrogenDioxideDensityGet.bind(this));
169+
if (typeof nox === 'number' && isFinite(nox)) {
170+
this.service.updateCharacteristic(hap.Characteristic.NitrogenDioxideDensity, nox);
171+
} else {
172+
this.log.warn('Invalid NOx value:', nox);
173+
}
140174

141-
this.serviceTemp.getCharacteristic(hap.Characteristic.CurrentTemperature)
142-
.on('get', this.handleCurrentTemperatureGet.bind(this));
175+
if (typeof temp === 'number' && isFinite(temp)) {
176+
this.serviceTemp.updateCharacteristic(hap.Characteristic.CurrentTemperature, temp);
177+
} else {
178+
this.log.warn('Invalid Temperature value:', temp);
179+
}
143180

144-
this.serviceCO2.getCharacteristic(hap.Characteristic.CarbonDioxideDetected)
145-
.on('get', this.handleCarbonDioxideDetectedGet.bind(this));
181+
if (typeof co2 === 'number' && isFinite(co2)) {
182+
this.serviceCO2.updateCharacteristic(hap.Characteristic.CarbonDioxideDetected, this.calculateCO2Detected(co2));
183+
this.serviceCO2.updateCharacteristic(hap.Characteristic.CarbonDioxideLevel, co2);
184+
} else {
185+
this.log.warn('Invalid CO2 value:', co2);
186+
}
187+
188+
if (typeof humidity === 'number' && isFinite(humidity)) {
189+
this.serviceHumid.updateCharacteristic(hap.Characteristic.CurrentRelativeHumidity, humidity);
190+
} else {
191+
this.log.warn('Invalid Humidity value:', humidity);
192+
}
146193

147-
this.serviceCO2.getCharacteristic(hap.Characteristic.CarbonDioxideLevel)
148-
.on('get', this.handleCarbonDioxideLevelGet.bind(this));
194+
this.service.updateCharacteristic(hap.Characteristic.AirQuality, this.calculateAirQuality(pm2_5));
149195

150-
this.serviceHumid.getCharacteristic(hap.Characteristic.CurrentRelativeHumidity)
151-
.on('get', this.handleCurrentRelativeHumidityGet.bind(this));
196+
this.log.info(`Updated characteristics - PM2.5: ${pm2_5}, PM10: ${pm10}, TVOC: ${tvoc}, ` +
197+
`NOx: ${nox}, TEMP: ${temp}, CO2: ${co2}, Humidity: ${humidity}`);
198+
}
152199
}
153200

154201
private async fetchData() {

0 commit comments

Comments
 (0)