DHT11 MQTT humity not show , but on log is ok

hello i have running nodemcu with this code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

#define wifi_ssid "Casa"
#define wifi_password "xxxxx"

#define mqtt_server "192.168.1.89"
#define mqtt_user "xxxx"
#define mqtt_password "xxxx"

#define humidity_topic "sensor/humidity"
#define temperature_topic "sensor/temperature"

#define DHTTYPE DHT11
#define DHTPIN  4

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266

void setup() {
  Serial.begin(115200);
  dht.begin();
  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

bool checkBound(float newValue, float prevValue, float maxDiff) {
  return !isnan(newValue) &&
         (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;

    float newTemp = dht.readTemperature();
    float newHum = dht.readHumidity();

    if (checkBound(newTemp, temp, diff)) {
      temp = newTemp;
      Serial.print("New temperature:");
      Serial.println(String(temp).c_str());
      client.publish(temperature_topic, String(temp).c_str(), true);
    }

    if (checkBound(newHum, hum, diff)) {
      hum = newHum;
      Serial.print("New humidity:");
      Serial.println(String(hum).c_str());
      client.publish(humidity_topic, String(hum).c_str(), true);
    }
  }
}

my log:

17-02-20 22:20:19 DEBUG (Thread-13) [homeassistant.components.mqtt] Received message on topicName/humidity: 20.00
17-02-20 22:20:19 INFO (MainThread) [homeassistant.core] Bus:Handling <Event mqtt_message_received[L]: qos=0, topic=topicName/humidity, payload=20.00>

- platform: mqtt
  name: "teste"
  state_topic: "topicName/humidity"
  qos: 1
  unit_of_measurement: "%"
  value_template: "{{ value_json.Temp }}"

my home assistant:

what is wrong?

Shouldn’t this be

value_template: “{{ value_json.humidity }}” ?

yea, is was my mistake, i changed it, but still not show.

from the docs…

value_template: ‘{{ value_json.humidity }}’

try single quotes ’ not double "

ok thanks again, i changed it too.
but the same… my log:

17-02-21 00:35:50 DEBUG (Thread-13) [homeassistant.components.mqtt] Received message on sensor/humidity: 34.00
17-02-21 00:35:50 INFO (MainThread) [homeassistant.core] Bus:Handling <Event mqtt_message_received[L]: topic=sensor/humidity, qos=0, payload=34.00>
17-02-21 00:35:52 DEBUG (Thread-13) [homeassistant.components.mqtt] Received message on sensor/humidity: 31.00
17-02-21 00:35:52 INFO (MainThread) [homeassistant.core] Bus:Handling <Event mqtt_message_received[L]: topic=sensor/humidity, qos=0, payload=31.00>
17-02-21 00:35:54 DEBUG (Thread-13) [homeassistant.components.mqtt] Received message on sensor/humidity: 23.00
17-02-21 00:35:54 INFO (MainThread) [homeassistant.core] Bus:Handling <Event mqtt_message_received[L]: topic=sensor/humidity, qos=0, payload=23.00>
17-02-21 00:35:56 DEBUG (Thread-13) [homeassistant.components.mqtt] Received message on sensor/humidity: 21.00
17-02-21 00:35:56 INFO (MainThread) [homeassistant.core] Bus:Handling <Event mqtt_message_received[L]: topic=sensor/humidity, qos=0, payload=21.00>


- platform: mqtt
  state_topic: 'sensor/temperature'
  name: 'Temperature'
  unit_of_measurement: 'C'
  value_template: '{{ value_json.temperature }}'

- platform: mqtt
  state_topic: 'sensor/humidity'
  name: 'Humidity'
  unit_of_measurement: '%'
  value_template: '{{ value_json.humidity }}'

and not show any value.

Are you loading the mqtt as a sensor?

That’s the only other thing I can think of, maybe someone else with more knowledge can chime in :slight_smile:

yes, i am loading as sensor

- sensor.temperature
- sensor.humidity

grrrrrrrrrrrrr it is very strange

I’ve made an example for the DHT22 sensor. You can find the code here.

@mertenats
i try your code.

on serial monitor says:

INFO: Connecting to Casa
..
INFO: WiFi connected
INFO: IP address: 
192.168.1.75
INFO: Attempting MQTT connection...INFO: connected

ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!
{
  "temperature": "19.00",
  "humidity": "21.00"
}
INFO: Closing the MQTT connection
INFO: Closing the Wifi connection

but i not connected it
DHT22 leg 2 - D1/GPIO5 - Resistor 4.7K Ohms - GND
D0/GPIO16 - RST (wake-up purpose)

its important???

but now shows me the temperatura and humidity on HA, but the serial monitor close the connection

You are trying to decode the sensor as a JSON object, but it is being published as a plain number. Try removing the value_template line altogether.

@gpbenton thank you so much for try help me.
now it works like a charm.