Parse timestamp with new MQTT binding

Hi,

I have an alarm system that sends MQTT message that looks like this:

SPC/G_SPC_ZONE_STATUS_13 {“update_time”:1545127209239,“status”:“ok”}
SPC/G_SPC_ZONE_INPUT_8 {“update_time”:1545127209238,“status”:“closed”}
SPC/G_SPC_ZONE_STATUS_8 {“update_time”:1545127209239,“status”:“ok”}

I can parse the status part with the new MQTT binding as

  Type string : status "Status"
    [ stateTopic="SPC/G_SPC_ZONE_STATUS_1", transformationPattern="JSONPATH:$.status" ]

The update_time value is a Unix timestamp that can be converted (somehow) to DateTime used in the binding. But how can I do this?

I want to have something that looks like this, or at least gives the requested result:

  Type datetime : timestamp "Timestamp"
    [ stateTopic="SPC/G_SPC_ZONE_STATUS_1", transformationPattern="ToDateTime(JSONPATH:$.update_time" )]

You either need chained transformations (not generally available) or use the JS transformation. You would write a small JS script that applies json path and then convert the value to an ISO date/time string.

1 Like

Thanks,

I solved it like this:

things:

Thing topic spc {
  Channels:
  Type datetime : statusUpdate "Status update"
    [ stateTopic="SPC/G_SPC_ZONE_STATUS_1", transformationPattern="JS:spc_timestamp.js" ]

And the transform spc_timestamp.js:

(function(i){
  var parsed = JSON.parse(i);
  var ts = new Date(parsed.update_time);
  return (ts.toISOString());
})(input)