DHT22 wrong sensor type in Home Assistant



  • Hi, I’m pretty sure the error is somewhere in the program, I just don’t know where. I want to use an Arduino Uno to read two limit switches and a DHT22 sensor. For this, I combined two programs with the help of ChatGPT, made some small adjustments, and tested it. Everything seemed to work fine in the Serial Monitor. However, in Home Assistant, the limit switches are recognized correctly, but the DHT22 is also detected as a door contact. Where is my mistake here?

    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable only USB, disable radio
    //#define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    //#define MY_RS485
    
    //USB Modus
    #define MY_GATEWAY_SERIAL
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <DHT.h>
    #include <Adafruit_Sensor.h>
    
    // ---------- DHT CONFIG ----------
    #define DHTPIN 4       // <-- DHT Datenpin
    #define DHTTYPE DHT22  // DHT-Typ (z.B. DHT11, DHT22)
    DHT dht(DHTPIN, DHTTYPE);
    
    #define SENSOR_TEMP_OFFSET 0
    static const uint64_t UPDATE_INTERVAL = 30000;  // 30 Sekunden für DHT-Update
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    // ---------- BUTTON CONFIG ----------
    #define PRIMARY_CHILD_ID 2
    #define SECONDARY_CHILD_ID 3
    
    #define PRIMARY_BUTTON_PIN 2     // bleibt auf D2
    #define SECONDARY_BUTTON_PIN 3   // bleibt auf D3
    
    #if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
    #error PRIMARY_BUTTON_PIN and SECONDARY_BUTTON_PIN cannot be the same
    #endif
    #if (PRIMARY_CHILD_ID == CHILD_ID_TEMP || PRIMARY_CHILD_ID == CHILD_ID_HUM || \
         SECONDARY_CHILD_ID == CHILD_ID_TEMP || SECONDARY_CHILD_ID == CHILD_ID_HUM)
    #error Child IDs must be unique
    #endif
    
    MyMessage msgButton1(PRIMARY_CHILD_ID, V_TRIPPED);
    MyMessage msgButton2(SECONDARY_CHILD_ID, V_TRIPPED);
    
    static uint8_t lastButton1 = 2;
    static uint8_t lastButton2 = 2;
    
    void presentation() {
      sendSketchInfo("Combined Sensor Node", "1.0");
    
      // DHT
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
    
      // Buttons
      present(PRIMARY_CHILD_ID, S_DOOR);
      present(SECONDARY_CHILD_ID, S_DOOR);
    
      metric = getControllerConfig().isMetric;
    }
    
    void setup() {
      dht.begin();
      delay(2000); // Sensor bootzeit
    
      pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
      pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
    }
    
    void loop() {
      // ----- BUTTON HANDLING ----- (Ständig überprüfen)
      uint8_t val1 = digitalRead(PRIMARY_BUTTON_PIN);
      if (val1 != lastButton1) {
        send(msgButton1.set(val1 == HIGH));
        lastButton1 = val1;
      }
    
      uint8_t val2 = digitalRead(SECONDARY_BUTTON_PIN);
      if (val2 != lastButton2) {
        send(msgButton2.set(val2 == HIGH));
        lastButton2 = val2;
      }
    
      // ----- DHT SENSOR ----- (Nur alle UPDATE_INTERVAL Millisekunden aktualisieren)
      static uint64_t lastDHTUpdate = 0;
      uint64_t currentMillis = millis();
    
      if (currentMillis - lastDHTUpdate >= UPDATE_INTERVAL) {
        lastDHTUpdate = currentMillis;
    
        float temperature = dht.readTemperature(metric); // true = Celsius
        float humidity = dht.readHumidity();
    
        if (!isnan(temperature) && (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS)) {
          lastTemp = temperature + SENSOR_TEMP_OFFSET;
          send(msgTemp.set(lastTemp, 1));
          nNoUpdatesTemp = 0;
          Serial.print("T: ");
          Serial.println(lastTemp);
        } else {
          nNoUpdatesTemp++;
        }
    
        if (!isnan(humidity) && (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS)) {
          lastHum = humidity;
          send(msgHum.set(lastHum, 1));
          nNoUpdatesHum = 0;
          Serial.print("H: ");
          Serial.println(lastHum);
        } else {
          nNoUpdatesHum++;
        }
      }
    
      // Sleep für Buttons und DHT
      delay(100);  // Schnelle Verzögerung, damit der Button-Check nicht zu oft passiert
    }
    
    

    I should mention that although I tested the DHT22 earlier, I didn’t connect it when testing with Home Assistant—just in case that’s relevant to the issue.


    Log in to reply
     


  • I'm going to make an assumption: You have connected to Home Assistant many times during your development.

    Home Assistant is not very flexible with MySensors. If you change Child_IDs it sometimes ignores the change.

    Try deleting your MySensors device. This is not a friendly operation. Assuming that your Uno is acting as a serial gateway and as your sensor monitory device,

    • disconnect the Uno from your computer. th
    • In HA: settings-->Devices and Services-->n DEVICES in MySensors integration-->click on device your going to delete-->3 vertical dots next to Remove Device from MySensors-->>Remove
      Then restart HA
    • Developer Tools-->restart-->restart
      After HA is restarted, plug your Uno back in

    If your MySensors gateway is MQTT you have to stop the MQTT gateway and delete all vestiges of your device in the MQTT broker (I use MQTT Explore to do this which has to be running befor you do anything to see the topics that will be erased.)

    Let us know if that fixed it.

    OSD



  • @OldSurferDude First of all, thanks for the reply. I tried it that way, but it looks like the third “door” sensor is completely gone. The issue is probably in the code after all.



  • @Commodoreuno I can't see anything obvious at a quick look through, but I would suggest that you change all the delay() functions to wait() functions and comment out the serial print instructions and see how it goes then.



  • Good call @skywatch !

    wait() is a MySensors function that looks for MySensors operations that run asynchronously to loop(). Search for "What is the difference between delay() and wait()"

    I have experienced the symptoms you describe. I put a wait() after functions that send MySensors data: send() and present() (but for some reason not sendSketchInfo()?). If I'm using an MQTT gateway Its 500 and 4000 milliseconds, respectively; Serial, 100 and 500.

    I think this will work for you! 🙂 Letting us know your results helps someone else that is experiencing what you are.

    OSD



  • Okay, I have now taken all the suggestions into account, including deleting the application in Home Assistant. I also worked with the official script for deleting the Arduino, which I hadn't done before, and made changes in the program, especially with the IDs and various names, in order to essentially create a completely new program. Now it works with this script. Thanks so far for the help!

    // Enable debug prints
    #define MY_DEBUG
    
    // Set static node ID
    #define MY_NODE_ID 45
    
    // Enable only USB, disable radio
    #define MY_GATEWAY_SERIAL
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <DHT.h>
    #include <Adafruit_Sensor.h>
    
    // ---------- DHT CONFIG ----------
    #define DHTPIN 4         // Datenpin des DHT22
    #define DHTTYPE DHT22    // Sensor-Typ DHT22
    #define USE_CELSIUS true // Immer Celsius verwenden
    #define SENSOR_TEMP_OFFSET 0
    
    #define UPDATE_INTERVAL 5000           // 5 Sekunden
    #define FORCE_UPDATE_N_READS 10        // nach 10 gleichen Werten senden
    
    #define CHILD_ID_HUM 20
    #define CHILD_ID_TEMP 21
    
    DHT dht(DHTPIN, DHTTYPE);
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp = 0;
    uint8_t nNoUpdatesHum = 0;
    
    // ---------- BUTTON CONFIG ----------
    #define PRIMARY_BUTTON_PIN 2
    #define SECONDARY_BUTTON_PIN 3
    
    #define PRIMARY_CHILD_ID 22
    #define SECONDARY_CHILD_ID 23
    
    MyMessage msgButton1(PRIMARY_CHILD_ID, V_TRIPPED);
    MyMessage msgButton2(SECONDARY_CHILD_ID, V_TRIPPED);
    
    static uint8_t lastButton1 = 2;
    static uint8_t lastButton2 = 2;
    
    // ---------- SETUP & PRESENTATION ----------
    void presentation() {
      sendSketchInfo("Combined Sensor Node", "1.0");
    
      // Sensoren anmelden
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
      present(PRIMARY_CHILD_ID, S_DOOR);
      present(SECONDARY_CHILD_ID, S_DOOR);
    }
    
    void setup() {
      dht.begin();  // DHT starten
    
      pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
      pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
    }
    
    // ---------- LOOP ----------
    void loop() {
      // ----- DHT AUSLESEN -----
      float temperature = dht.readTemperature(); // immer Celsius
      float humidity = dht.readHumidity();
    
      if (!isnan(temperature) && (temperature != lastTemp || nNoUpdatesTemp >= FORCE_UPDATE_N_READS)) {
        lastTemp = temperature + SENSOR_TEMP_OFFSET;
        send(msgTemp.set(lastTemp, 1));
        nNoUpdatesTemp = 0;
        Serial.print("Temp: ");
        Serial.println(lastTemp);
      } else {
        nNoUpdatesTemp++;
      }
    
      if (!isnan(humidity) && (humidity != lastHum || nNoUpdatesHum >= FORCE_UPDATE_N_READS)) {
        lastHum = humidity;
        send(msgHum.set(lastHum, 1));
        nNoUpdatesHum = 0;
        Serial.print("Hum: ");
        Serial.println(lastHum);
      } else {
        nNoUpdatesHum++;
      }
    
      // ----- BUTTONS -----
      uint8_t val1 = digitalRead(PRIMARY_BUTTON_PIN);
      if (val1 != lastButton1) {
        send(msgButton1.set(val1 == HIGH));
        lastButton1 = val1;
      }
    
      uint8_t val2 = digitalRead(SECONDARY_BUTTON_PIN);
      if (val2 != lastButton2) {
        send(msgButton2.set(val2 == HIGH));
        lastButton2 = val2;
      }
    
      // ----- SLEEP -----
      //sleep(PRIMARY_BUTTON_PIN - 2, CHANGE, SECONDARY_BUTTON_PIN - 2, CHANGE, UPDATE_INTERVAL);
      delay(50);
    }
    
    


  • I'm glad that worked!


Log in to reply
 

1 out of 7

Suggested Topics

0
Online

11.5k
Users

11.1k
Topics

112.8k
Posts