How to ask OpenHab for item status through mqtt

Hi mates,

I’m using openhab for logging some mqtt espeasy temperature sensors.
Now I’m developing a mqtt client for showing some inhouse sensor data (like a small dashboard) and I want to retrieve the temperature sensor data store in openhab using mqtt, is this possible?

My sensors are reporting each 30 minutes so waiting for their publication is not a possibility. I need to retrieve the last data stored in openhab anytime.

I have also configured the event bus but don’t how if it’s possible to ask for a item status… It seems is not possible.

Any idea about how to solve this? Thanks in advance

MQTT-items can’t be “consumed” as you’d like it to have

I can think of three possible Solutions:

  1. make a rule, which uses MQTT action to publish states either periodically (every minute) or via a switch (item changed to ON)
  2. use “retain”-mode in your broker (if yours is capable of configuring topics for retain on the broker configuration)
  3. use “retain”-mode for your openHAB mqtt-configuration

ad 1)
simple - the rule either publishes automatically every xxx seconds/minutes the state (it’s easy to setup, just minor network traffic involved) or your client activates an item, which in turn triggers the rule to send the state of the item. (a bit more complex, but minimum network traffic)

ad 2)
you configure your broker to “retain” the topic for that items (more on that: https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages), so if you subscribe to a topic, the broker send the last value of that topic immediately.

ad 3)
if your Broker (it seems, only the HiveMQ is capable of that as it seems not to be MQTT-standard), you can configure your mqtt-binding to send the messeges with “retain”-flag on.
But beware: this is systemwide - meaning ALL of your mqtt-items will be retained, which could lead to some confusion at startup of OH2, as it will (depending on your configuration) update the items on that last retained message. So if you have some items in openHAB, which get updated via MQTT, they will get the last MQTT-command again at Startup.
a possible way out is described here: MQTT sub to obtain default value

2 Likes

I agree with all that Thomas said. I can add option 4.

Configure your client to publish a message to a will known topic and have an item subscribe to that topic. When the item received a message trigger a rule to publish the current state of all the relevant Items to the relevant topics.

I use this approach in the other direction. When my OH starts up it publishes a message that causes my sensors to report their current status.