Created
May 21, 2023 17:01
-
-
Save alvesvaren/54e26a23f881b9914e0f29bdb29ef6ee to your computer and use it in GitHub Desktop.
ESPHome AHT20 Temperature and humidity custom component config using Adafruits AHTX0 library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Put this in the ESPHome folder in your home assistant configuration | |
#include "esphome.h" | |
#include <Wire.h> | |
#include "Adafruit_AHTX0.h" | |
#define TAG "ahtx0" | |
class AHTX0Sensor : public PollingComponent { | |
public: | |
Adafruit_AHTX0 aht; | |
Sensor *temperature_sensor = new Sensor(); | |
Sensor *humidity_sensor = new Sensor(); | |
AHTX0Sensor() : PollingComponent(15000) { } | |
void setup() override { | |
ESP_LOGCONFIG(TAG, "Setting up AHTX0Sensor..."); | |
if (!aht.begin()) { | |
ESP_LOGCONFIG(TAG, "Failed to start AHTX0Sensor..."); | |
} | |
LOG_SENSOR(" ", "Temperature", this->temperature_sensor); | |
LOG_SENSOR(" ", "Humidity", this->humidity_sensor); | |
} | |
void update() override { | |
sensors_event_t humidity, temp; | |
aht.getEvent(&humidity, &temp); | |
temperature_sensor->publish_state(temp.temperature); | |
humidity_sensor->publish_state(humidity.relative_humidity); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
esphome: | |
# ... | |
includes: | |
- ahtx0.h | |
libraries: | |
- SPI | |
- Wire | |
- adafruit/Adafruit BusIO@^1.14.1 | |
- adafruit/Adafruit Unified Sensor@^1.1.9 | |
- adafruit/Adafruit AHTX0@^2.0.3 | |
# ... | |
i2c: | |
sensor: | |
# ... | |
- platform: custom | |
lambda: |- | |
auto aht20 = new AHTX0Sensor(); | |
App.register_component(aht20); | |
return {aht20->temperature_sensor, aht20->humidity_sensor}; | |
sensors: | |
- name: "Temperature" | |
unit_of_measurement: °C | |
accuracy_decimals: 2 | |
- name: "Humidity" | |
unit_of_measurement: "%" | |
accuracy_decimals: 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on the code from esphome/feature-requests#1398 (comment)
Should work for everything supported by Adafruit AHTX0 (AHT10 and AHT20)