Display temperature and humidity in Openhab2

Hello guys;
I am currently working on Temperature and Humidity ‘Data loger’ using Raspberry PI 3 and NodeMCU ESP8266 with DHT11.
in the item folder :

i
    Number  home_temp "Living Room [%.1f °C]" <temperature> {mqtt="<[mosquitto:MK-SmartHouse/security/temp:state:JSONPATH($.BME280.Temperature)]"}

As shown in the attached files: the code on screenshots and shows the problem of not displaying the values of temperature.
But when we display in CLI it works as shown in this screenshot


We are looking forward to hearing from you soon because it is so important for us.
Thanks in advance.

Look in openhab.log and events log for info

Welcome to the openHAB Community :heart:

Your topic here is defined as
MK-SmartHouse/security/temp

But in the cli your topic is names

topicName/temperature

:thinking:

So in conclusion your item should could look like this

Number  home_temp "Living Room [%.1f °C]" <temperature> {mqtt="<[mosquitto:topicName/temperature:state:default)]"} 
1 Like

It was a tuping mistake.
I actually updated the topic.
Thanks for answering.

Yes I did it.But it is still not displaying.

Hey @Hinda could you please share that line of code of your esp8266 where you define the mqtt topic and where you set the values?

I know from my sonoff that the topic and response looks like this

electricity/coffee/SENSOR = {"Time":"1970-01-06T22:59:59","ENERGY":{"Total":17.556,"Yesterday":0.000,"Today":17.556,"Period":0,"Power":16,"Factor":0.63,"Voltage":214,"Current":0.121}}

Which would result in an item like this this for example Voltage

Number testitemforyou "Wattage [%.1f W]"  (myGroup) {mqtt="<[mosquitto:electricity/coffee/SENSOR:state:JSONPATH($.Power)]"}


Translation: Coffeemaker - current usage

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

#include "DHT.h"

#define DHTPIN D4     // what pin we're connected to
#define wifi_ssid "PI"
#define wifi_password "bomarepi2020"

#define mqtt_server "192.168.2.169"
#define humidity_topic "topicName/humidity"
#define temperature_topic "topicName/temperature"
String temp_str;

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println("DHTxx test!");
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  dht.begin();
}

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("changeMe")) {
      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 newValue < prevValue - maxDiff || newValue > prevValue + maxDiff;
}

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

void loop() {
  // Wait a few seconds between measurements.
 // delay(10000);
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 1000) {
    lastMsg = now;
    
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float newHum = dht.readHumidity();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float newTemp = dht.readTemperature(true);

    if (checkBound(newTemp, temp, diff)) {
      temp = newTemp;
      //***************************************************************************************************
//        temp_str = String(newTemp); //converting ftemp (the float variable above) to a string 
//        temp_str.toCharArray(temp, temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...
//        client.publish(temperature_topic, temp);  
      //***************************************************************************************************
      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);
   }
 }
}

If this is the code, that is currently flashed on your esp8266, your topic are still

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

I also see no json usage in your code, so why you tried to use JSONPATH?

if this command still delivers values

mosquitto_sub -v -t 'topicName/temperature'

Your item would look like this

Number  home_temp "Living Room [%.1f °C]" <temperature> {mqtt="<[mosquitto:topicName/temperature:state:default]"}

this is our openhab log :

2019-04-26 19:04:18.251 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:04:18.251 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:04:18.391 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:04:18.391 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:04:26.778 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:04:41.088 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:04:43.799 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:04:53.674 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:05:20.751 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:05:24.054 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:39:47.770 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:39:49.817 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:39:59.313 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:42:27.675 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 19:48:28.097 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 20:30:41.820 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 21:43:52.615 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 21:53:00.480 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:40:26.628 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'home.items'

2019-04-26 22:42:15.615 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:42:19.138 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:42:20.140 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:42:20.808 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:07.576 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:08.147 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:08.642 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:09.116 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:30.253 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:30.564 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:30.884 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:45:31.218 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:46:04.344 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:46:04.857 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:46:52.921 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:46:53.247 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:46:53.615 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:46:53.928 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:48:25.600 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:48:25.897 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:48:26.211 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

2019-04-26 22:48:26.618 [WARN ] [sitemap.internal.SitemapProviderImpl] - Filename `home.sitemap` does not match the name `TestSiteMap` of the sitemap - please fix this as you might see unexpected behavior otherwise.

Okay okay okay, maybe I should ask something else first :wink:

  • Which version of the MQTT Binding did you install? 1.X or 2.X?
  • Did you setup your broker correctly in your openHAB settings?

Yes I’ve done the same thing that you have mentionned but it is still not displaying

Please answer these question:

  • Which openHAB Version do you use?
  • Which platform you are working on?
  • Which version of the MQTT Binding are you using?

The Version of openHAB that we use is Openhab2.
the platform I am working on is : Openhab2
The version of the MQTT Binding I am using: binding-mqtt - 2.4.0

This unrelated error can stop your UI updating. When you do get your MQTT Item working, this might hide the fact.
Best to fix this too.

the file mqtt.cfg is as following :

broker.url=tcp://192.168.2.169:1883
broker.clientId=openhabian

I am showing you this file in order to check which syntax will work either moosquito or broker ?

Would you please tell me how to fix it

2 what? there is 2.0, 2.1, 2.2, 2.3, 2.4, 2.5

I meant something like raspberry pi, Ubuntu, Windows, a calculator…

Well okay, the your whole config is wrong as it could be.

You did configurations for the 1.X binding.
As can be seen here in the docs

But you installed the 2.4 MQTT Binding so you need to read, understand and follow the 2.X docs

To fix the sitemap, see here about sitemap name and filename

Thanks for the information.
Now I get rid of the warning but still not working.