Skip to content

Instantly share code, notes, and snippets.

@tmathmeyer
Last active April 17, 2024 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmathmeyer/9841c3734685d0b6fa1341ba612cdda7 to your computer and use it in GitHub Desktop.
Save tmathmeyer/9841c3734685d0b6fa1341ba612cdda7 to your computer and use it in GitHub Desktop.
// See formulae and constants here:
// https://www.teos-10.org/pubs/Wagner_and_Pruss_2002.pdf
function f2k(f) {
return (f - 32) * 5 / 9 + 273.15;
}
var specific_gas_constant = 461.5; // J/(kg * K)
var critical_pressure = 22.064; // MPa
var critical_temperature = 647.096; // K
var dot = (X, Y) => X.map((_, i) => X[i] * Y[i]).reduce((m, n) => m + n);
// K -> MPa
function saturation_pressure(temp_k) {
if (temp_k > critical_temperature) {
return -1;
}
var r = 1 - (temp_k / critical_temperature);
var exponents = [r, r**1.5, r**3, r**3.5, r**4, r**7.5];
var constants = [-7.85951783, 1.84408259, -11.7866497, 22.6807411, -15.9618719, 1.80122502];
return critical_pressure * Math.exp(critical_temperature * dot(exponents, constants) / temp_k);
}
// %,K -> g/m^3
function absolute_humidity(rel_humidity, temp_k) {
var num = rel_humidity * saturation_pressure(temp_k) * 10000000;
var den = specific_gas_constant * temp_k;
return num / den;
}
var bed_temp = f2k(msg.masterbed_airthings_temp);
var bed_relative = msg.masterbed_airthings_humidity;
var bath_temp = f2k(msg.masterbath_thxs_temp);
var bath_relative = msg.masterbath_thxs_humidity;
var bed_abs_humidity = absolute_humidity(bed_relative, bed_temp);
var bath_abs_humidity = absolute_humidity(bath_relative, bath_temp);
// If the sun is down and the bathroom lights are off, its sleep time,
// which means the fan should _not_ start.
var allowed_to_start = msg.masterbath_showerlight == "on" ||
msg.sun == "above_horizon";
// Start when the absolute humidity is 20% higher than ambient.
var start_threshold = bed_abs_humidity * 1.2;
// Stop when absolute humidity is below 120% of ambient.
var stop_threshold = bed_abs_humidity * 1.2;
if (allowed_to_start && (bath_abs_humidity > start_threshold)) {
node.send({
"action": "turn_on",
"area": "masterbath_extractor",
"types": ["switch"],
"statistics": msg,
"watermass": {
"bedroom": bed_abs_humidity,
"bathroom": bath_abs_humidity
}
});
}
else if (bath_abs_humidity < stop_threshold) {
node.send({
"action": "turn_off",
"area": "masterbath_extractor",
"types": ["switch"],
"statistics": msg,
"watermass": {
"bedroom": bed_abs_humidity,
"bathroom": bath_abs_humidity
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment