Ik zou je graag ter wille zijn maar ik heb geen verstand van Homekit en Homebridge. Maar ik heb het wel ome ChatGPT gevraagd en die komt met het volgende (geen idee of het werkt, maar misschien is er een andere nerd die je op weg kan helpen).
Below is an example Homebridge plugin that polls your sensor’s HTTP endpoint (http://sensor.local/values) and updates HomeKit with the CO₂, temperature, and humidity values. In this example, the accessory exposes three services: a Temperature Sensor, a Humidity Sensor, and a Carbon Dioxide Sensor. You can adjust the polling interval and the sensor URL via the plugin configuration.
Create an “index.js” file with the following code:
'use strict';
let Service, Characteristic;
module.exports = (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-http-sensor", "HTTPSensor", HTTPSensorAccessory);
};
const http = require('http');
class HTTPSensorAccessory {
constructor(log, config) {
this.log = log;
this.name = config.name || "HTTP Sensor";
// URL to pull sensor data from; default provided if not configured
this.url = config.url || "http://sensor.local/values";
// Polling interval in seconds (default 60 seconds)
this.pollInterval = config.pollInterval || 60;
// Create HomeKit services
this.informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, "MyCompany")
.setCharacteristic(Characteristic.Model, "CO2TempHum Sensor")
.setCharacteristic(Characteristic.SerialNumber, "123456");
this.temperatureService = new Service.TemperatureSensor(this.name + " Temperature");
this.humidityService = new Service.HumiditySensor(this.name + " Humidity");
this.co2Service = new Service.CarbonDioxideSensor(this.name + " CO2");
// Initialize characteristic values
this.temperatureService.setCharacteristic(Characteristic.CurrentTemperature, 0);
this.humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, 0);
this.co2Service.setCharacteristic(Characteristic.CarbonDioxideLevel, 0);
// Optionally, use CarbonDioxideDetected characteristic based on a threshold (e.g., 1000 ppm)
const co2Threshold = 1000;
this.co2Service.setCharacteristic(Characteristic.CarbonDioxideDetected, false);
// Begin polling sensor data immediately
this.pollSensorData();
// And poll repeatedly at the defined interval
setInterval(() => {
this.pollSensorData();
}, this.pollInterval * 1000);
}
pollSensorData() {
http.get(this.url, (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => {
try {
let json = JSON.parse(data);
// Extract sensor values from JSON response
let temp = parseFloat(json.Temperature) || 0;
let humidity = parseFloat(json.Humidity) || 0;
let co2 = parseInt(json.CO2) || 0;
// Update HomeKit characteristics
this.temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temp);
this.humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, humidity);
this.co2Service.setCharacteristic(Characteristic.CarbonDioxideLevel, co2);
// Set CO2 detected status if value exceeds threshold
let co2Detected = co2 >= 1000;
this.co2Service.setCharacteristic(Characteristic.CarbonDioxideDetected, co2Detected);
this.log(`Sensor data updated: Temperature=${temp}°C, Humidity=${humidity}%, CO2=${co2} ppm`);
} catch (err) {
this.log("Error parsing sensor data:", err);
}
});

.on('error', (err) => {
this.log("HTTP GET error:", err);
});
}
getServices() {
return [this.informationService, this.temperatureService, this.humidityService, this.co2Service];
}
}
Configuration
In your Homebridge config.json, add an entry like this:
{
"accessories": [
{
"accessory": "HTTPSensor",
"name": "Living Room Sensor",
"url": "http://sensor.local/values",
"pollInterval": 60
}
]
}
Explanation
HTTP GET Request:
The plugin calls http.get to retrieve sensor data from the provided URL. It parses the returned JSON and extracts the "Temperature", "Humidity", and "CO2" values.
HomeKit Services:
The plugin creates three HomeKit services: TemperatureSensor, HumiditySensor, and CarbonDioxideSensor. It then updates their characteristics with the sensor readings.
Polling:
The sensor data is refreshed every pollInterval seconds (default is 60 seconds). You can change this interval in your configuration.
This plugin allows Homebridge to integrate with your sensor that already has server functionality, pulling data via HTTP GET and updating HomeKit accordingly.