Add libraries on ESPHome

Hi everybody …

I try to integrate the library Adafruit AHTX0 fot my temperature and humidy sensor aht2x.
When I write the yaml file for my ESP8266 Wemos D1 R2 board, I can’t use this library. So I tried with aht10 library but when I install it on the ESP, the board cannot read the sensor because it’s not the good library. So I searched on internet and I found that I need to create a custom sensor.

I don’t know how to create this for my sensor.
If anyone can help me please.

Thank you.

PS : I have HA os on a rasperry pi3 and ESPHome as an add-on.

#ifndef AHT2X_SENSOR_H
#define AHT2X_SENSOR_H

#include <Wire.h>
//#include <Adafruit_Sensor.h>
#include "Adafruit_AHTX0.h"
#include <esphome.h>

class Aht2xCustomSensor : public esphome::Component, public esphome::PollingComponent {

 public:
  Aht2xCustomSensor() : PollingComponent(60000) {} // Lire toutes les 60 secondes

  void setup() override {
    Serial.begin(9600);
    while (!Serial)
      delay(10); // attendre la connexion série

    Serial.println("Démarrage du capteur AHT...");
    if (!aht.begin()) {
      Serial.println("Erreur lors de l'initialisation du capteur AHT!");
      while (1);
    }
    Serial.println("Capteur AHT20/AHT21 prêt !");
  }

  void update() override {
    // Lire les données d'humidité et de température
  sensors_event_t humidity, temperature;
  aht.getEvent(&humidity, &temperature);
  delay(2000);
  }
};

#endif  // AHT2X_SENSOR_H

This is what I tried.

esphome:
  name: capteur-ch
  friendly_name: °C_%
  includes:
    - aht2x-sensor.h


esp8266:
  board: d1

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "......

ota:
  password: "....."

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Capteur-Ch Fallback Hotspot"
    password: "....."

captive_portal:

# Configuration du capteur AHT2x
i2c:
  sda: D2
  scl: D1

sensor:
  - platform: custom
    lambda: |-
      auto myAht2xCustomSensor = new Aht2xCustomSensor();
      App.register_component(myAht2xCustomSensor);
      return {myAht2xCustomSensor};



    sensors:
      name: "Custom_AHT2X"

Perhaps start by telling us what went wrong with the aht10 component.

I don’t have the aht10 component but the aht2x sensor. So on ESPHome, the library for aht10 doesn’t work with the aht2x. Finally, when I upload the script on the ESP8266 Wemos D1 R2, an error message tell me that the aht10 component couldn’t be found.

According to ESPHome aht2x is suppported as an existing platform

This is i2c communication protocol so your logs should show that the device has been found on the initial scan. I can’t see that in your logs at all.