OpenHAB displays old MQTT values

Hello, I just started this week using openHab for displaying temperature and humidity published by ESP8266(without Arduino board) each minute.

Everything is going ok, but since I am still in developing phase of the project, the ESP8266 module is not on all the time.

And now I have some data displayed in openHAB which are from last day. I would like to have a rule or something which is reporting if the data is older than one minute (not being refreshed by ESP8266).
In this way I can easily know if something is wrong with the sensor.

Thank you

Please have a look at the MQTT concept of a “last will and testament” (LWT). The broker can be configured to publish a certain message to a certain topic if an MQTT client (the ESP8266) is no longer connected. If you subscribe to this topic in openhab, then you will know when your device dropped off line.

Hello and thank you for reply.

I made as you said, read and implement these LWT messages, I added these parameters in connect function.
In my project I am using PubSubClient library.

There is also a keep_alive define.
I “played” a little bit with this value and I haven’t managed to make it work as I expected.
It seems that this is useful if the connection drops during the time that sensor is on.
But broker is not sending the LWT message if the client is not on. Is sending the message only when the sensor is back on and this is not helping at all.

The “keep alive” interval controls the maximum period that the MQTT broker will accept between each message from the MQTT client to still consider the client as “alive and kicking”. The default interval is 15 seconds, but as you point out, this can be changed. If there are no messages published by your code within the “keep alive” interval, then the PubSubClient library will publish an internal message (ping) to keep the connection alive - thereby the name, I guess.

If the MQTT broker does not hear from the MQTT client within the interval it will consider the client disconnected, and if configured to do so, it will publish the LWT message.

Now, this is how it is supposed to work (as far as I can tell) - and also what seems to work OK in my setup (which is a NodeMCU/ESP8266 with PubSubClient library publishing to an mosquitto MQTT broker running on an ODROID C1+).

If by “sensor is on” you mean that you ESP8266 is running and connected to the MQTT broker, then yes this is true - and the whole point of the LWT concept. It all starts with a client connecting to the broker, indicating that it wants to use “establish” a LWT. Thereafter, if the client disappears, the LWT will be automatically published at the end of the “keep alive” interval.

Yes it does, or at least it is supposed to do - and does so in my setup. BUT, again, this is provided that the client has been on and connected to the broker (so that the broker knows about the LWT).

things in my ESP8266 are like this:

  • connect to wifi router
  • connect to mosquitto broker (configuring the LWT message and keepalive interval)
  • measure temperature and humidity
  • publish them to broker
  • disconnect from the broker
  • go to sleep for 60 seconds
    and things are starting all over again.

As far as I understood from the documentation, LWT messages are sent if:
1.An I/O error or network failure is detected by the server.
2.The client fails to communicate within the Keep Alive time.
3.The client closes the network connection without sending a DISCONNECT packet first.
4.The server closes the network connection because of a protocol error.

If I understood it correctly keepalive interval starts when the connection has been made.
If the default value is 15seconds, for sure all my steps are made in this interval. All other steps I am sure is not happening, at least not now.

So, if I understood all correctly, is no possible way that the LWT message to be sent by the broker.

I changed the keepalive interval to 1second and inserted a 1.1second delay after the connect function.

Indeed the LWT message was sended but this is not my point.
I just want to see if the time between 2 temperature publishes is longer than 60s(the time which ESP8266) is sleeping.

Right! Now I understand your problem. I am sorry, but I did not realize before that you did the sleep-wake up thing.

If you publish temperature and/or humidity every 60 seconds (or so) then I suggest simply monitoring an update to these items with a timer of maybe 120 seconds. If you start the timer on each update, then if the timer ever expires this means your device did not wake up to publish data.

Where that timer should be? On the sensor or on openHAB.

I made last night a test like this:

after Publishing everything I needed, made a disconnect from Broker and then made another connect to Broker and sensor went to sleep.

The Keep alive interval is 63 seconds. if the sensor is not waking up in this interval, Broker will send the LWT message, if the sensor is waking up and is working correctly then LWT is not sent anymore.
Of course the “will” Topic is updated at the first Connection with “online” and LWT message is “offline”, so if the sensor is waking up once at 60 seconds i should have only “online” message, if not I should see “offline” after timeout.

Please let me know if there is a better and more easy method.

The following requires you have persistence setup and configured on your items to save every change.

rule "Stale temp data?"
when
    Time cron "0 0 * * * ?" // every minute
then
    if(!Temperature.updatedSince(now.minusMinutes(1)) {
        // Temperature hasn't received an update in the past minute
    }
end

I was thinking something like this:

var Timer tAlive = null

rule "Sensor alive?"
when
	Item Temperature received update
then
	tAlive?.cancel()
	tAlive = createTimer(now.plusMinutes(2)) [|
		logWarn("Sensor", "Sensor appears to be dead")
		// Do whatever
	]
end

This would probably be more flexible as its timing will exactly follow the receipt of data and not a set polling period.

I setup a nodeMCU ESP8266 12E with a temp and pressure sensor to report every 10mins, I notice that after circa 19 hours it stops sending, then 5 then 1 (hours) lol. I know the keep alive is set to 2 mins, but not checked the LWT I setup to see if its getting irrtated. Would I need to change the keep alive to >10Mins?

from my point of view is better to have this timer on openHAB side because the sensors runs on battery and every byte in the communication counts for battery saving. in this way sensor is sending less bytes.

@woodenstuart if you know that the time between 2 messages is 10 minutes, i would put a timeout of 11minutes.
But be noticed, keepalive interval is helping only if you want to detect communication issues.

If you want to use it for sensor status (between reports), you have to initiate a new connection before ending the cycle.
my sequence, now, is like this:

  1. connect to wifi
  2. connect to broker setting the LWT message and keepalive interval (currently 60s)
  3. publish a topic “sensor/status” with “online” message
  4. measure temperature, humidity and battery
  5. publish temperature
  6. publish humidity
  7. publish battery
  8. disconnect from mqtt broker
  9. connect to mqtt broker setting the LWT message and keepalive interval
  10. disconnect from wifi
  11. sent to deepsleep.

So in order to have a good status of the sensor state using LWT messages you have to have the step 9. in that position.

I’m not hugely fussed about power saving at the moment as most will have access to power as opposed to battery, but its handy to think about for properly remote sensor bricks.

I’ll try the timeout at 11mins and see if that stabilises things. Thanks

I don’t understand this comment. Both my and @KjetilA’s suggestions are openHAB rules to implement the timer.

Currently I am using the LWT Messages, this imvolves more data to be transmitted by sensor, which leads to less battery life.
But I am willing to implement those rules on openHAB side and sensor will send less data.