How to import datetime from mqtt with transformationPattern?

Tags: #<Tag:0x00007fc910c2d810> #<Tag:0x00007fc910c2ced8> #<Tag:0x00007fc910c2cc08>

Hello MQTT-Experts,

hopefully you can help me with receiving one simple mqtt-message: I get a mqtt-message with a time information in format like “2018-01-01 12:14:00” and setupped a thing as followed:

Type datetime : lastTimeSync “MAX_034c92 lastTimeSync” [
stateTopic=“tele/FHEM/HeatingThermostat/MAX_034c92/lastTimeSync”,
transformationPattern=“REGEX:(s/\s/T/g)” ]

Since the mqtt-binding only accept a datime in format like “2018-01-01T12:14:00”, I have to insert the “T” in between instead of the white-space.

How can I do this?
Can I use REGEX, JSONPATH or similar for this substitution while reading the mqtt-message, or do I really have to do an post-transformation with a rule or something for this stupid exchange of ONE character?
As I mentioned before, I have also tried
REGEX:(s/\\s/T/g)
without a chance…

THANK YOU VERY MUCH FOR YOUR SUGGESTIONS!!

You could also leave the transformation out and use a state description (OH3) to change how the date is shown. If that’s sufficient i would go that way.

Example: %1$tA, %1$tB %1$td, %1$tY - %1$tl:%1$tM %1$tp

Hello Chris,
thank you for your suggestion!
The problem is not the presentation, but be able to get this datetime in a calculate-able datetime-format.

The idea is to send an alert/ doing a diagnosis if the last received mqtt-message is “old”. So my hope is to use the transformationPattern for this transformation task.
(A transformationPattern just for cutting out some values - as the transformationPattern seems to be mainly used - is - in my opinion - a oversized naming…).

Hopefully anybody else have another idea?
I am sure, that this kind of solution will also help in other transformation usecases.

You can do this indirectly with a profile: check out this magic

1 Like

Thank You for this hint!
In my optinion this Design Pattern could be a possible workaround for my mentioned transformationPattern-problem.
But: sorry, in this way I am a little bit “old-fashioned”: I do not want to create a new DateTime-item. I just want to transform a datetime mqtt-message nearly “untouched” into a datetime.
I mean: I want to import the datetime exactly with the time-information in the message, not the timestamp of the receiving of any message…

Any further suggestions, please?

There’s no timezone information in that, you may have to decide if you want to treat it as UTC or as local zone.

You’ve already identified what you need to do; convert your non-standard date-time into ISO format to be parsed into a DateTime type Item. Not an MQTT issue.
But I think you’ll also need to append your chosen timezone info.

One way is to use the openHAB REGEX Transformation Service. This is not exactly regular REGEX, so the online help doesn’t always help.

I don’t understand REGEX, and would do this with a JS transform myself like -

(function(inputData) {
    return  inputData.replace(" ", "T") + "Z"
})(input)

You could append your fixed time zone. Or, it should be possible to elaborate the javascript to interrogate the openAB host system for locale, and hope that remote device is reporting in same zone.

Thank You for your transformation script.
I have tried to use it in several ways (as well as transformationPattern in my thing

Type datetime : lastTimeSync “MAX_034c92 lastTimeSync” [ stateTopic=“tele/FHEM/HeatingThermostat/MAX_034c92/lastTimeSync”, transformationPattern=“JS:lastTimeSync.js” ]

as well as an item (of course imported as an string in thing)

DateTime MAX_034c92_lastTimeSync “MAX_034c92_lastTimeSync” { mqtt="<[mqtt:tele/FHEM/HeatingThermostat/MAX_034c92/lastTimeSync:JS(lastTimeSync.js)]" }

as well as in format

DateTime MAX_034c92_lastTimeSync “MAX_034c92_lastTimeSync[%1$td.%1$tm.%1$ty %1$tH:%1$tM:%1$tS]” { channel="mqtt:topic:MAX_034c92:lastTimeSync::JS(lastTimeSync.js)]" }*

but my mqtt-message does not be read.

I will restart my raspberry and hope that there is done magic over night (my startup on RPi3 takes 4 hours…) and will answer. Maybe I have also to switch on the demaging log-files again.

Beside working or not: the documentation of the mqtt-binding explains, that REGEX should work (no information about js…). And the documentation of REGEX describes how to substitute an white-space with a “T”.
So: do you mean, that there is a bug in the mqtt-binding regarding transformationPattern or is the bug simply anywhere between display and chair??

These are just nonsense, this is the old version 1 MQTT binding syntax, and then something you just made up. Don’t just throw random stuff at it.

Perhaps you should confirm which binding version you are using?

The JS transformation is an optional add-on that needs installing.

Your openhab.log may contain useful messages.

We’re not stopping you using that at all, go right ahead. Don’t forget you might need to append something as well.

What bug? This is about how-to-configure.

EDIT - I tested the little script in a rule, it’s a good way to develop new transforms because you can easily inspect and alter things.

/transform/test.js

// function to massage date to string etc.
// variable "input" contains data passed by OpenHAB binding
(function(inputData) {
    return  inputData.replace(" ", "T") + "Z"
})(input)

test rule fragment, with a DateTime type Item ‘testtime’

val datestring = "2018-01-01 12:14:00"
logInfo("test", "in " + datestring)
val isostring = transform("JS", "test.js", datestring)
logInfo("test", "out " + isostring)
testtime.postUpdate(isostring)

results

2021-03-01 22:28:41.945 [INFO ] [.eclipse.smarthome.model.script.test] - in 2018-01-01 12:14:00
2021-03-01 22:28:42.063 [INFO ] [.eclipse.smarthome.model.script.test] - out 2018-01-01T12:14:00Z

2021-03-01 22:28:42.068 [vent.ItemStateChangedEvent] - testtime changed from NULL to 2018-01-01T12:14:00.000+0000

Does your whole MQTT payload consist of “2018-01-01 12:14:00” ?

Thank You for your detailed description! It helped me a lot and I am sure, that this will be a good fundament for further transformations.

Your solution with a further item calculated by a rule works fine! I will implement it now in three independent systems.

But I am not willing to close this topic until anyone have an smart idea, how to do the transformation right when receiving the mqtt-message:
as I mentioned before, the mqtt-binding seems to support a transformationPattern for incoming messages. This is the way I am looking for. Without any further rule, without any further item (which will be initialised with NULL…).

Just to say it once again: Thank You very much for your solution!!

regarding

Yes, this is exactly the payload. There is no timezone delivered. And this is ok for me, since the delivery-system (FHEM) runs on the same CPU with same ntp-access…
On the other hand: the timezone is also not neccessary by the mqtt-binding for receiving datetimes.

SO: does anybody know, how to read the mqtt-payload “2018-01-01 12:14:00” as a datetime?
How to transform this string to “2018-01-01T12:14:00” so that the mqtt-binding will excapt this as datetime?
Is it possible to integrate the upper mentioned .js-file from rossko57 and combine it with transformationPattern?

Yes.
Be aware that when you edit Things you may need to restart the binding package to pick up new settings.

This looks essentially correct to me.

1 Like

Thank You very much! This is exactly the solution I was looking for! It works fine!
Your detailed description also helped me with another transformation-topic. So: all upper mentioned solutions/ workarounds helps a lot to learn how to transform values practically.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.