snowdd1 / homebridge-knx

KNX platform shim for homebridge
https://github.com/nfarina/homebridge
GNU General Public License v2.0
97 stars 56 forks source link

AirQualityCO2 sensor handler #85

Closed matevzg closed 7 years ago

matevzg commented 7 years ago

A lot of us might have a humidity+CO2 sensor in one KNX device.

Since HomeKit does not allow to have one AirQuality sensor with both those values, I implemented a standalone CO2 sensor handler. You might use it together with HumiditySensor, which is already supported as a standalone sensor.

Maybe @snowdd1 might include it in the next build.

/* Module - Simple handler for a CO2 sensor
 * AirQualityCO2.js
 * Matevz Gacnik, @matevzg
 */

'use strict';

/**
 * @type {HandlerPattern}
 */
var HandlerPattern = require('./handlerpattern.js');
var log = require('debug')('AirQualityCO2');

/**
 * @extends HandlerPattern
 */
class AirQualityCO2 extends HandlerPattern {

    /*******************************************************************************************************************
     * onKNXValueChange is invoked if a bus value for one of the bound addresses is received
     * 
     */
    onKNXValueChange(field, oldValue, knxValue) {
        console.log('INFO: onKNXValueChange(' + field + ", "+ oldValue + ", "+ knxValue+ ")");

        if (field === "CarbonDioxideLevel") {
            // CarbonDioxideLevel is DPT9.001

            if (knxValue > this.myAPI.getLocalConstant("PoorAirQuality")) this.myAPI.setValue("AirQuality", 5);
            else if (knxValue >= this.myAPI.getLocalConstant("InferiorAirQuality")) this.myAPI.setValue("AirQuality", 4);
            else if (knxValue >= this.myAPI.getLocalConstant("FairAirQuality")) this.myAPI.setValue("AirQuality", 3);
            else if (knxValue >= this.myAPI.getLocalConstant("GoodAirQuality")) this.myAPI.setValue("AirQuality", 2);
            else if (knxValue >= this.myAPI.getLocalConstant("ExcellentAirQuality")) this.myAPI.setValue("AirQuality", 2);
            else if (knxValue < this.myAPI.getLocalConstant("ExcellentAirQuality")) this.myAPI.setValue("AirQuality", 1);

            // inform HomeKit
            this.myAPI.setValue("CarbonDioxideLevel", knxValue);
        }
    }
}

module.exports = AirQualityCO2;

Config goes like this:

{
    "ServiceType": "AirQualitySensor",
    "ServiceName": "CO2 Sensor",
    "Handler": "AirQualityCO2",
    "Characteristics": [
        {
            "Type": "AirQuality"
        },
        {
            "Type": "CarbonDioxideLevel",
            "Listen": [
                "0/2/12"
            ],
            "DPT": "DPT9"
        }
    ],
    "KNXReadRequests": [
        "0/2/12"
    ],
    "LocalConstants": {
        "ExcellentAirQuality": 900,
        "GoodAirQuality": 1000,
        "FairAirQuality": 1100,
        "InferiorAirQuality": 1300,
        "PoorAirQuality": 1500
    }
}
snowdd1 commented 7 years ago

Very cool! If you put that into an CR (make that PR as in "Pull request"), GitHub will list you as contributor, too! You earn (except from the kudos!) the fancy Contributor tag on all your posts in homebridge-knx issues and comments...

matevzg commented 7 years ago

No need to.

matevzg commented 7 years ago

It's just a simple thing based on shoulders of giants. :)

Push it in.