Possibility of processing string MQTT responses

A bit of history:-
I’m still fairly new to Openhab, but I have an installation that’s working well.
I’ve a custom controller that’s looking after my legacy home alarm, and various infrared beam sensors. For the past 15 years it’s been running on a Microchip device and was written in assembler. Not one hang or failure over all that time. Anyhow, recently I’ve moved to a MQTT device (initially a Raspberry Pi Zero hard wired into the original Microchip controller as a proof of concept), and now to a custom designed unit based around a ESP8266 - all written in Micropython.
It’s all working really well, very robust with WDT, and the ability to survive both Broker and wifi outages.

The published status from the ESP8266 is in the form of MQTT strings (alarm_en, alarm_dis, gate_open etc).
I can then easily display these, or push them to Telegram.

What I really want is the ability for the MQTT status string to contain some dynamic content (such as local timestamp).
String would be in the form of:-
06:04:23 17-11-2019:alarm_en
Rather than how I have it now which is just the static status string:-

Question is what would be the best method to process this string to strip the timestamp and actual status? My current rules are triggering when a specific string comes in “Item xxx received update alarm_en”?. Could I strip process the string before the rule runs.

I have an item like this.

String Status “Status [%s]” {mqtt="<[mosquitto:/x/y/z/status:state:default]"}

And the rule like this.

rule “status”
item Status received update “alarm_en”
send Telegram xxxxx

I’m just getting to grips with the Jython scripting, in order to give a bit more custom control and the same question applies with regard to the helpers.

@when(item Status received update alarm_en")

Sorry for the rambling description, hope it makes sense what I’m asking.



Why don’t you create the time stamp when the message is received in openHAB?

To your query, I would use a different separator:
06:04:23 17-11-2019#alarm_en for example and then use a string.split("#") method in the rule:

rule0 "status"
     item Status received update
    val String content = Status.state.toString()
    if content.contains("alarm_en") {
        val String timeStamp = content.split("#").get(0)
        vat String alarmCode = content.split("#").get(1)
send Telegram xxxxx

That’s really helpful, thanks. Didn’t realise you could have a generic update.
Let me investigate.

btw, reason for wanting the remote timestamp is that this unit carries on even if it looses network, then once back online it will send it’s locally stored status.



Consider posting the values in a JSON format then you can extract the values easily when received in OH with the JSONPATH transform

You can use a Rule like Vincent describes. If you change the format of the message you can make it easier in yourself as he describes.

But you can also use a transformation on your Items directly. You can have more than one Item configured to listen to the same topic (e.g an alarm Switch Item and a DateTime Item). Then using a transform extract the appropriate value for reach item from the same message.

I strongly second the recommendation to use JSON formatting for this whether you use a rule or do it all in the items.

Thanks or the advice. I’m learning.
What I have, and prefer is to have the Python based rule do all the work.
I have this working well now, and it gives me great flexibility in coding the handling of the messages.

btw, somewhat related. Is it valid to have items defined that are only used to hold strings that are displayed as part of the sitemap? I have a Python rule that processes the messages, then posts updates to these items for display in the UI as strings.


Absolutely. These “virtual” or “proxy” items are useful for display, persistence and other usages

PS, I maintain that you should try to get your values published from your ESPs ina json format.
For example:

{ "timestamp": "06:04:23 17-11-2019", "code": "alarm_en" }