Skip to content

Commit

Permalink
publish dew point approximation to mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
micampe committed Apr 19, 2021
1 parent 721fe04 commit 9911ce9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
"type": "string",
"title": "Humidity Topic"
},
"dewPointTopic": {
"type": "string",
"title": "Dew Point Topic"
},
"batteryTopic": {
"type": "string",
"title": "Battery Topic"
Expand Down Expand Up @@ -169,6 +173,7 @@
"mqtt.url",
"mqtt.temperatureTopic",
"mqtt.humidityTopic",
"mqtt.dewPointTopic",
"mqtt.batteryTopic"
]
},
Expand Down
26 changes: 26 additions & 0 deletions lib/accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HygrothermographAccessory {

this.temperatureMQTTTopic = undefined;
this.humidityMQTTTopic = undefined;
this.dewPointMQTTTopic = undefined;
this.batteryMQTTTopic = undefined;
this.mqttClient = this.setupMQTTClient();

Expand All @@ -37,6 +38,23 @@ class HygrothermographAccessory {
this.log.debug("Initialized accessory");
}

get dewPoint() {
if (
this.hasTimedOut() ||
this.latestTemperature == null ||
this.latestHumidity == null
) {
return;
}
// Magnus dew point approximation
const t = this.temperature;
const h = this.humidity;
const a = 17.27;
const b = 237.7;
const alpha = (a * t) / (b + t) + Math.log(h / 100);
return (b * alpha) / (a - alpha);
}

setTemperature(newValue, force = false) {
if (newValue == null) {
return;
Expand All @@ -51,6 +69,9 @@ class HygrothermographAccessory {
.updateValue(newValue);
this.addFakeGatoHistoryEntry();
this.publishValueToMQTT(this.temperatureMQTTTopic, this.temperature);
if (this.dewPoint) {
this.publishValueToMQTT(this.dewPointMQTTTopic, this.dewPoint);
}
}

get temperature() {
Expand All @@ -74,6 +95,9 @@ class HygrothermographAccessory {
.updateValue(newValue);
this.addFakeGatoHistoryEntry();
this.publishValueToMQTT(this.humidityMQTTTopic, this.humidity);
if (this.dewPoint) {
this.publishValueToMQTT(this.dewPointMQTTTopic, this.dewPoint);
}
}

get humidity() {
Expand Down Expand Up @@ -268,12 +292,14 @@ class HygrothermographAccessory {
temperatureTopic,
humidityTopic,
batteryTopic,
dewPointTopic,
url,
...mqttOptions
} = config;

this.temperatureMQTTTopic = temperatureTopic;
this.humidityMQTTTopic = humidityTopic;
this.dewPointMQTTTopic = dewPointTopic;
this.batteryMQTTTopic = batteryTopic;

const client = mqtt.connect(url, mqttOptions);
Expand Down

0 comments on commit 9911ce9

Please # to comment.