Keeping track on when an Item was last updated (OH3)

Hi all.
I have just upgraded from OH2 to OH3 and I have a rule that’s not working any more. I know I’ve read somewhere that much date stuff has changed, just don’t know really how to solve it.

First in my rules file I have this:

import java.text.SimpleDateFormat
val SimpleDateFormat sdf = new SimpleDateFormat("HH:mm")
var String update_time

and then in a rule which is executed when a particular Item changes state I do

update_time = sdf.format(now.millis)

…and then I use this string for some notification stuff. I simply need a string containing the time when the Item was last changed. Any way to do that in OH3? Maybe even an easier way than this?

For the record, the error I get in my log now is:

'millis' is not a member of 'java.time.ZonedDateTime'

I do not use it but the time provider library changed in OH3.

to respond to your post title: why don’t you use persistence and <item>.lastUpdate

That’s a good question… Thing is, I don’t really want the last update, but the one before that.

What I do is the following:

  1. Whenever the value of the Item is changed, I save the current timestamp in a variable (as shown in my op)
  2. Use the expire binding (or rather now the built in expire functionality in OH3) to set the Item to NULL if it hasn’t been updated for 15 minutes.
  3. When the Item changes to NULL, the rule sends me a message which includes the time saved in step 1 above so I know for how long it has been dead.

So in this scenario I don’t really want the last update (which would be the change to NULL) but the update before that.

If you use previousState(true), you get an HistoricItem with state and timestamp properties that represent the Item when it had a state different than the current state. I’m not sure what is returned when the current state is NULL, but this may suit your purposes.

If not, take a look here…

1 Like

Assuming these updates are caused by updates to Items linked to a Channel, you can create a DateTime Item linked to the same Chanel with one of the time Profiles. This will update the Item with the timestamp of the update. Then user there expire functionality to expire the item if there hasn’t been any update for too long. UNDEF is probably a better choice than NULL.

1 Like

Why is that?

1 Like

NULL is intended to mean the Item has never been initialized. UNDEF is intended to mean “I don’t /can’t know what state the Item is in.” For example, when the MQTT binding loses connection to the broker, it will set all the items linked to Things under that broker thing to UNDEF.

UNDEF better fits your use case.

2 Likes

Thanks for the explanation! I come from a world where a variable either has a value or it’s NULL, all this extra stuff makes it confusing for me :stuck_out_tongue:

In OH 1, NULL was actually called UNINITIALIZED. I was not happy with the change. Note that NULL is not null. NULL is an item state. null is what you are used to it meaning in programming.

2 Likes

I installed a DateTime Timestamp item to a „binary“ channel (contact); which works fine. The next DateTime Timestamp item to an „analog“ channel (temperature) failed saying there is time conversion problem

2021-01-04 11:00:46.708 [WARN ] [e.internal.SseItemStatesEventBuilder] - Exception while formatting value '2021-01-04T10:49:18.095052+0100' of item LS01BK_Temp_Timestamp with format '%.1f %unit%': Conversion = 'u'

You probably need to file an issue.

I tried to repeat the fault, but now everything worked well. I don’t know, what went wrong on the first trials.

@Mclupo You don’t get this exception if you add state description metadata for your item and enter e.g. %1$tF %1$tT in the pattern box.

I think you ran into this bug

I have persistence enabled - could I also use this on the new main UI on cards?
e.g. footer: =items.PoolTemperature_PoolTemperature.lastUpdate

Please try yourself before asking.

I actually did - and it doesn’t work - but is it supposed to work? I saw that another answer that suggests creating separate items for last update but that seems a bit redundant if these values can be accessed through lastUpdate & persistence.

Not from the UI

1 Like