Fix state issue on change reporting, add override sample ini

This commit is contained in:
Michael Bisbjerg 2024-05-15 21:26:30 +02:00
parent f51da4f0c4
commit f9467ceaf1
4 changed files with 20 additions and 5 deletions

2
.gitignore vendored
View File

@ -9,7 +9,7 @@
.vscode
esp01-update.sh
platformio_override.ini
/platformio_override.ini
replace_fs.py
wled-update.sh

View File

@ -33,5 +33,4 @@ build_flags =
lib_deps =
${esp32.lib_deps}
enjoyneering/AHT10@~1.1.0
Wire
```

View File

@ -0,0 +1,9 @@
[env:aht10_example]
extends = env:esp32dev
build_flags =
${common.build_flags} ${esp32.build_flags}
-D USERMOD_AHT10
; -D USERMOD_AHT10_DEBUG ; -- add a debug status to the info modal
lib_deps =
${esp32.lib_deps}
enjoyneering/AHT10@~1.1.0

View File

@ -32,6 +32,11 @@ private:
float _lastHumidity = 0;
float _lastTemperature = 0;
#ifndef WLED_MQTT_DISABLE
float _lastHumiditySent = 0;
float _lastTemperatureSent = 0;
#endif
AHT10 *_aht = nullptr;
float truncateDecimals(float val)
@ -74,7 +79,7 @@ private:
mqttCreateHassSensor(F("Humidity"), topic, F("humidity"), F("%"));
}
void mqttPublishIfChanged(const __FlashStringHelper *topic, float lastState, float state, float minChange)
void mqttPublishIfChanged(const __FlashStringHelper *topic, float &lastState, float state, float minChange)
{
// Check if MQTT Connected, otherwise it will crash the 8266
// Only report if the change is larger than the required diff
@ -83,6 +88,8 @@ private:
char subuf[128];
snprintf_P(subuf, 127, PSTR("%s/%s"), mqttDeviceTopic, (const char *)topic);
mqtt->publish(subuf, 0, false, String(state).c_str());
lastState = state;
}
}
@ -163,10 +170,10 @@ public:
// We can avoid reporting if the change is insignificant. The threshold chosen is below the level of accuracy, but way above 0.01 which is the precision of the value provided.
// The AHT10/15/20 has an accuracy of 0.3C in the temperature readings
mqttPublishIfChanged(F("temperature"), _lastTemperature, temperature, 0.1f);
mqttPublishIfChanged(F("temperature"), _lastTemperatureSent, temperature, 0.1f);
// The AHT10/15/20 has an accuracy in the humidity sensor of 2%
mqttPublishIfChanged(F("humidity"), _lastHumidity, humidity, 0.5f);
mqttPublishIfChanged(F("humidity"), _lastHumiditySent, humidity, 0.5f);
#endif
// Store