MQTT from ESP32 not registering in OpenHab 3

I am developing my own sensors in for Temperature, Humidity and control of lights using ESP32 over MQTT communicating to an OpenHab3 running on a very old Apple Mini that I had lying around. The MQTT server is running on Mosquito.

I have something wonky configured, and at first I thought it was OpenHab - but something else is going on. As a test, I put in a temp and humidity on a blank page.

If I write to one of those values using a program such as MQTT Explorer, the value on OpenHab updates immediately.

If I write to the same channel using my ESP32, I can see the updated value in MQTT Explorer, but not in OpenHab.

For example… This channel is being displayed in OpenHab
image

If I send 81 via MQTT Explorer, then Openhab shows 81. Approx 2 seconds later, my ESP32 will update it to 38, and I see 38 in MQTT Explorer, but OpenHab continues to show 81 and never updates.

My ESP32 code is simple - it’s written in Arduino and it’s using PubSubClient. The code is literally just
mqttClient.publish(“env2/temperature”, temp, 1);
mqttClient.publish(“env2/humidity”, humd, 1);
delay(1000);

Any ideas where I can look to troubleshoot this?

1 Like

Post your mqtt things config please

what I do routinely when dealing with mqtt is open a shell (ssh or putty on win*) and follow the mqtt messages scrolling by.

mosquitto_sub -v -h mqtt-broker.host -u user -P password -t '#' | xargs -d$'\n' -L1 sh -c 'date "+%d.%m.%Y %T $0"'

That may need a bit explanation:

  • in my setup authentication with user/password is mandatory, if you don’t do that those parameters can be omitted.
  • If you open the shell on the same host where the mqtt boker is running then the -h parameter can be omitted, too.
  • the bit including and after the pipe serves to precede every line with the date/Time which helps me a lot when debugging but if there isn’t much traffic on your broker you can omitt that part, too

So, stripped down the line reads mosquitto_sub -v -t '#'

It shows you the raw content - topic message - of everything going through your broker. Now you could do your tests again and keep an open eye for any differences…

I like the idea of adding timestamp! I wrote a simple script to save me from typing all that because I do this all the time too.

This, and the openHAB log output when messages are flying around.

Great troubleshooting tips! Thank you! MQTT is new to me. Unfortunately, it did not shed much light.

I did not bother with the timestamps since this is a new setup and there is almost nothing going on other than the one sensor that is publishing and my manual attempts. So the log in it’s entirely is:
image

I will reply to the rest with configs in a bit. Work is calling.

and if you manually inject a float? The other thing that surprises is to see 2 spaces proceeding the value sent by the ESP and only one when sending it manually.

I… AM… A… MORON.

Found the reason. It’s because when I was manually entering a number, I was putting in an Integer. My sensor is putting in a float. Thank you all for being my Rubber Duck. :sweat_smile:

NOW… Having said that, I - for the life of me - can not figure out the correct way to configure OpenHab3 for a humidity sensor. I have to make the channel a String. If I try to do it as a number, I have the above problem.

The item… same problem. If I make it a dimensionless number, I have the above. If I make it a string, it shows the value. But ultimately for my rules, I need it to be a number.

What is the ideal method here? I do not see a humidity type number (which I find odd that we have CatalyticActiivty, and not Humidity… But whatever). Is there a way for me to add a new number type for humidity, or do I store it as a string, and convert it in whatever rules I need to make? It would seem to be more efficient to store it as a float - how do I do that?

OORR For Humidity should I just round to the nearest integer and put it as dimensionless ? I guess I do not NEED to distinguish between 54.5% and 54.8% Humidity…?

How are you all setup?

I don’t want to be ‘that guy’ but I’ll post my experience. Please don’t take the wrong way. Developing your own code is super-fun.
I started trying to develop my own sensors for my own use to learn (I’m assuming you are not trying to develop a commercial product to offer) but eventually stumbled upon Tasmota. I feel I’ve learned much more by following their codebase, compiling and modifying it myself, and introducing more capabilities than I imagine I’d ever have time to do on my own.

Check your ESP code - when you think openHAB is receiving a double, your log suggests there is a space in front of the double which effectively converts it into a string. As a result, openHAB will complain if you try grabbing it as anything other than a string.

1 Like

@hafniumzinc You get a gold star (and a solution check). I had a dtostrf in there which was forcing an empty space. :cry: Changing the min to remove the space fixed it. After days of pain.

@alfista2600 thank you for pointing me to Tasmota. I will look into it. It looks pretty cool. I do not feel the need to program these things if there is an easier way to accomplish my goals. :slight_smile:

To all of you. THANK YOU for being so helpful! It’s very much appreciated!

zigbee2mqtt is another good project if you want relatively inexpensive non-diy hardware to communicate to OpenHAB.

In addition to Tasmota I would also suggest looking into ESPHome. I have used both with OpenHAB and MQTT. Solid options with great community support.

The evolution of my adventure with esp8266:
Tasmota → writing my own firmware with platformio + homie → esphome

Esphome is basically a framework with a lot of ready made libraries that most of the time you won’t need to write your own code, but you can still write your own C++ code for your specific requirements. You get all the basic stuff for free: wifi handling, ota upgrade, config storage, mqtt framework, etc.