Unable to get DateTime to work

I have the following item defined:
DateTime DebugTest “Last Heartbeat [%1$tH:%1$tM]” { mqtt="<[mosquitto:debug/test:state:default]" }

And in my sitemap I have:
Default item=DebugTest

But no timestamp is shown. Only ‘Last Heartbeat -:-’. And yes, I have published something in that debug/test mqtt queue

I would try

Text item=DebugTest

but that is just a wild guess :sunglasses:

Is your mqtt broker able to send a DateTime item?
Probably not.
Better use a String Item to receive the data from your mqtt broker and then write a rule which logs the time when you received this data.

Ah. For some reason I thought that it’s going to give me the timestamp when the message was received.

Is it possible to make a rule that would notice if the time between two heartbeats has been too long?

I particularly recommend the Expire Binding version near the bottom of the first post.

Also, I think you could receive an MQTT message into a DateTime Item as long as the value of the message is epoc (it must be a number, not a String representation of the number) or in ISO 8601 format.

Thanks, I will take a look at the thread you mentioned.

I don’t have a timestamp in my MQTT message. It’s just a generic message that let’s you know that the sensor is still alive, the actual content of the message is irrelevant (it’s a running number I believe), so I just wanted to get the timestamp when the message was received. And if I don’t get a heartbeat by some predetermined time, I would like to fire off an alarm letting me know that the sensor is offline (a poor man’s battery monitor, if you will).

I was kinda hoping I could do something like this:

DateTime DebugTest "Last Heartbeat [%1$tH:%1$tM]" <clock> { mqtt="<[mosquitto:debug/test:state:EXEC(/bin/date +%s)]" }

To get the timestamp of the mqtt event.

Okay, I basically got it working the way I wanted for now. Using the following rule:

import org.openhab.model.script.actions.Timer

var Timer timer

rule "Sensor Heartbeat"
when
  Item Node102_Heartbeat changed
then
  logInfo("heartbeat", "Heartbeat from node: " + Node102_Heartbeat)

  if (timer != null) {
    timer.cancel
    timer = null
  }

  timer = createTimer(now.plusSeconds(5)) [|
    logWarn("heartbeat", "No Heartbeat from node: " + Node102_Heartbeat")
  ]
end

Now I just need to modify my items so that they belong to the same group and this rule monitors the whole group. I suppose it’s possible to access the specific item in that group? Or is it not possible to make a rule that would react to a change to any item within a group?

Actually now that I started reading that thread about the generic is alive pattern, that might be the way to go. I’ll see what I come up with after reading it through

That might work and it is an approach I never considered. I’ve never used the Exec transform before.

What didn’t work for you? Have you installed the exec transform? It might require the exec1 binding to work. Check the logs for errors.

I think most of us create a rule to update a DateTime when the associated Item gets an update but I’m intrigued by the idea of doing the same in a transform. Of course for it to work date needs to output in the correct format that can be parsed by the DateTimeItem.

I don’t know the nature of your messages but you might want to use received update as the trigger so the rule runs even when an mqtt message comes in that doesn’t result in a state change.

See:

Not quite. You can trigger a rule based on updates to a group but the rule will trigger multiple times per Item update. Or you can trigger a rule with each item listed as a separate trigger and use the trick in the working with groups link above to identify which Item triggered the rule.

I’m not quite sure. It seems that if I use anything other than ‘default’ as the transform for MQTT items, they never get executed. There was no log entry in event log when I published something in the topic if I used some transformations. It only seems to work when using default.

But it looks like the rule based approach might work better anyway as I need to react to the heartbeat as well, so might as well do it in the rules.

It seems that I have an awful lot of reading to do. I will get right to it. I’ll post here when (if?) I get this thing working.

Big thanks to Rich for all your help!

One thing to be aware of is depending on which package you choose at the start you may need to install the transforms like any other add-on. I use transforms with mqtt routinely so know they do work.