Easy way to convert mqtt payload into OH4 DateTime

Using 4.1.1 release.

I receive a timestamp via MQTT, which throws an WARN:

2024-01-23 17:15:07.094 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command '2024-01-23 14:36:29+00:00' from channel 'mqtt:topic:synology:VW:lastUpdate' not supported by type 'DateTimeValue': 2024-01-23 14:36:29+00:00 is not in a valid format.

Is there a short Value Transformation to convert the time in a OH4-understable format?

You have four options over all.

  1. Change what ever is publishing this message to use ISO8601 formatted strings
  2. Create a transformation that converts the above to an ISO8601 formatted string
  3. Create a transformation that converts the above to the format used by Java ZonedDateTime which is somewhat compatible with ISO8601 in some circumstances but differs slightly in how it handles the timezone.

Implementing 2 or 3 is going to differ based on what scripting langue you want to use to do the transformation.

I JS Scripting I would expect it to look something like:

(function(input) {
  const formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd kk:mm:ssZ');
  return formatter.parse(input).toString();
})(input)

You might run into trouble with locale though. Including all possible locales adds to much to the size of the distro so you might need to install the locale on your own. See [WIP] Time: Add access to JS-Joda Locales by florian-h05 · Pull Request #94 · openhab/openhab-js · GitHub for some preliminary instructions which I’m trying to get added to the main docs, once I have time to test it out and verify some things.

1 Like