Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculation of dewpoint #12

Open
dirstel opened this issue Nov 27, 2024 · 1 comment
Open

calculation of dewpoint #12

dirstel opened this issue Nov 27, 2024 · 1 comment

Comments

@dirstel
Copy link

dirstel commented Nov 27, 2024

The dewpoint is a value for deciding weather air should/could be exchangened in order to lower/raise relative humidity. Unfortunatly most thermometer/hygrometer-combinations do not report this value. As dewpoint my be calculated by knowing temperature and relative humidity an extension would be nice. Having basic coding skills this should be doable for me, but I do not have any clue where to start. Basicly the code should do the following:

  • listen for messages publishing tempreature and humidity
  • perform calculation
  • enrich message with calculated value

code for calculation in JavaScript:

        var tempC = msg.payload.temperature;
        var humidRel = msg.payload.humidity;
        
        // Konstanten
        var molekulargewicht = 18.016; // des Wasserdampfes in kg/kmol
        var gaskonstante = 8214.3; // in J/(kmol*K)
        var tempK = tempC + 273.15;
        
        var a, b;
        if (tempC >= 0) {
            a = 7.5;
            b = 237.3;
        } else {
            a = 7.6;
            b = 240.7;
        }
         
        // Sättigungsdampfdruck (hPa)
        var saettigungsDampfDruck = 6.1078 * Math.pow(10, (a * tempC) / (b + tempC));
         
        // Dampfdruck (hPa)
        var dampfDruck = saettigungsDampfDruck * (humidRel / 100);
         
        // Wasserdampfdichte bzw. absolute Feuchte (g/m3)
        var humidAbs = Math.pow(10,5) * molekulargewicht / gaskonstante * dampfDruck / tempK;
         
        // v-Parameter
        var v = Math.log10(dampfDruck / 6.1078);
         
        // Taupunkttemperatur (°C)
        var dewpointC = (b * v) / (a - v);

Any help would be apreciated!

@dirstel
Copy link
Author

dirstel commented Dec 9, 2024

I manged to build a working extension. Only part missing is the enriching of the message published by Z2M via MQTT:

[ok] listen for messages publishing tempreature and humidity
[ok] perform calculation
[ ] enrich message with calculated value

Is this the correct binding to "intercept" and enrich a MQTT-Message? How is this done?
PS: I did not find any class diagram or sth similar - getting through all this classes was quite hard ;)

class DewpointCalculator {
    constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
        this.eventBus = eventBus;
        this.logger = logger;
        this.eventBus.on('stateChange', this.onStateChange.bind(this), this.constructor.name);
        logger.info('Loaded DewpointCalculator');
    }

    async onStateChange(data) {
        const { entity, update } = data;
        if ((data.to.hasOwnProperty('temperature')) & (data.to.hasOwnProperty('humidity'))) {
            if (!data.to.hasOwnProperty('dewpoint')) {

                // this should be added to the mqtt-message published 
                this.logger.info(this.calculateDewpoint(data.to['temperature'], data.to['humidity']));
                
            }
        }
    }

    calculateDewpoint(tempC, humidRel) {
        var molecularWeight = 18.016; // of water vapor in kg/kmol
        var gasConstant = 8214.3; // in J/(kmol*K)
        var tempK = tempC + 273.15;

        var a, b;
        if (tempC >= 0) {
            a = 7.5;
            b = 237.3;
        } else {
            a = 7.6;
            b = 240.7;
        }
         
        // saturation vapor pressure (hPa)
        var saturationVaporPressure=6.1078*Math.pow(10,(a*tempC)/(b+tempC));
        // vapor pressure (hPa)
        var vaporPressure = saturationVaporPressure*(humidRel/100);
        // Wasserdampfdichte bzw. absolute Feuchte (g/m3)
        var humidAbs = Math.pow(10,5)*molecularWeight/gasConstant*vaporPressure/tempK;
        // v-Parameter
        var v = Math.log10(vaporPressure/6.1078);
        // Taupunkttemperatur (°C)
        var dewpointC = (b*v)/(a-v);

        return(dewpointC.toFixed(2));
    }

    async onMQTTMessage(topic, message) {
        // console.log({topic, message});
    }

    async stop() {
        this.eventBus.removeListeners(this.constructor.name);
        this.logger.info('Unloaded DewpointCalculator');
    }
}

module.exports = DewpointCalculator;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant