Use cloudmqtt for last state persistence

Hi,

I want to be able to query the last item state via MQTT. The app that will use the last item state will subscribe to the topic and will get the last state. I thought of using cloudmqtt as persistence service when I set the retain flag to true.

Has anyone tried this option?

( I thought of using ngrok so I can use REST API to access my home openhab instance but ngrok change the url each reboot unless you use the paid version :frowning: )

Thx

To anyone who is interested,

I’ve just checked the option of using last item state with cloudmqtt. This seems to work even when I don’t set the retain flag (although now it is set).

If anyone has better way for getting item state, I will be happy to know.

Thx

MQTT persistence was a way to do EventBus like integrations before the MQTT Event Bus was implemented.

It is write-only. There is no way to query MQTT persistence so there is no way to get the last state through MQTT Persistence.

Just about any other persistence engine.

@rlkoshak

I wasn’t aware on the MQTT Event Bus, seems I can use it instead of the MQTT persistence. I will check it.

My assumption is that when the state is retained in the mqtt service, I can subscribe to the topic and get the state on demand. Seems to work for me 


OK, that would work but it is definitely not doing persistence and restoreOnStartup in the standard way.

The standard approach is to use the restoreOnStartup strategy with a queriable database and you are done. No need to separately subscribe to anything, no extra binding configs, etc.

If all you need is the most recent value, frankly you should probably be using the MapDB persistence and save yourself a lot of extra work.

Event Bus really doesn’t buy you anything if you are truly just trying to use MQTT to persist data.

What I need is a way to get item state from openhab but the app who needs the state cannot access my openhab (behind my home router). Ngrok is a solution but using fix domain is only in the paid version so I am using the MQTT broker as the place to get the last item state. I am not using it as persistence.

I hope there was simple way to do it (Maybe there is 
). I don’t want to open port in my router.

OK, that is something completely different.

Yes, you probably want to use the Event Bus configuration. This will automatically publish state changes to a topic and automatically subscribe for updates on a separate topic. It is usually used to connect multiple OH instances together or with other systems like Node-Red.

Assuming you use something like CloudMQTT you don’t have to open any ports.

Another alternative includes accessing OH’s REST API through MyOpenhab.org which doesn’t require opening a port.

@rlkoshak Thx for you inputs!!!

I tried the event bus now and it works but the advantage of MQTT persistence is that it can update the state every N hours / minutes so if for every reason the broker lost the state, it will be updated.

I thought “I wish I can use openhab org for that” and from your answer it seems I can do it. I will search how to do it, seems better approach.

(Will appreciate if you can point me to documentation / example how to access my openhab org to get item state)

@rlkoshak

I have succeeded to use REST API to access item state vi myopenhab.org. Thx for the advise!

BTW, I’ve got a response from cloudmqtt which says that they retain the data 30 minutes so if someone will want to use this technique for any reason, this is the time to referesh the state (via the persist config file)