Design Pattern: Time of Last Update

Tags: #<Tag:0x00007f746154df08>

Please see Design Pattern: What is a Design Pattern and How Do I Use Them for a description of what Design Patterns are and how to use them.

Problem Statement

Sometimes you want to know the latest time when an item has been updated. For example, when was the last time the front door was opened, or when was the last temperature reading from your fridge. It’s very useful for battery powered devices to check that they still are alive.

Concept

By using the new profile functionality for items (in OH 2.5) it’s very easy! An item (switch, sensor, whatever) gets a corresponding DateTime item that stores the last update. You can choose between the last update or the last change, depending on your needs.

Example

String Pumprum_BatteryLevel
  "Pumprum [%s]"
  <battery> (gBattery)
  {channel="mqtt:topic:mybroker:pumprum:battery"}
DateTime Pumprum_BatteryLevel_LastUpdate
  "Pumprum [%1$tF %1$tR]"
  <battery> (gTimestamps)
  {channel="mqtt:topic:mybroker:pumprum:battery" [profile="timestamp-update"]}

The DateTime item uses the same channel as the original item, but with the addition of the [profile… ] part. The profile can be either timestamp-update or timestamp-change.

The format of the DateTime object is by default the same as the original item (Number, String etc,.) but you would normally want the time and/or date, hence the output format of [%1$tF %1$tR].

Profiles can be used for a lot of other stuff, full documentation is here:

There are of course other ways that accomplish the same thing. The advantage of this method is that no rules are needed and it’s a really easy configuration.

Related Design Patterns

Design Pattern How It’s Used
Design Patterns: Generic Is Alive This is another way of getting the “keepalive” functionality. It also works for OH 1.x
9 Likes

Can the same goal be achieved using the PaperUI?

Yes you can, but isn’t the idea of openHAB to automate things?

I think you misunderstood my question. The OP shows items defined in an item file. Can the same goal be achieved when creating things and items using the PaperUI?

Have you tried? Which part are you having difficulty with?

I had to test this, and yes - it is possible to do this with paper UI!
Go the thing and press the blue + sign next to Linked Items. Then you select the profile Timestamp on change or Timestamp on update and continue to create the item in the standard way.

2 Likes

I haven’t tried it, no. I’m running OH2.4 and don’t have a 2,5 system to test with, hence the question.

Fantastic! Thanks very much for testing this. This looks like a great bit of new functionality.

Currently, for items that I wish to record the last update time I create a DateTime item with the same name as the original item but with “_LastUpdate” appended to the name. I add the original item to the gLastUpdate group and I have a rule that’s triggered by an update to “Member of gLastUpdate” that then updates the DateTime item.

The method you described above seems far easier to maintain. Thanks again! :slight_smile:

1 Like

This only works if a channel is linked to an Item? Can I use it with a ‘proxy’ item which is updated by a rule and doesn’t have any channels associated with it?

Nope. Profiles are a function of channels. postUpdate adds one line to a rule.

1 Like

Many Thanks @martin1 !
I’m using rules currently, like @higgers … and this new item sounds wonderful !