Design Pattern: Time of Last Update

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
24 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 !

This was very helpful, thank you!

1 Like

Profile feature only on OH 2.5? Why Doc didn’t mention this? I mean 2.5 haven’t release yet, put this info inside doc without mention really make people confuse.

I have tried timestamp-update without success, google around and finally found here.

This is a great feature, I use it to keep track of unstable or battery power devices. And I alert myself on them if there is no update for a day or so.

I mean 2.5 haven’t release yet

Latest release is 2.5.0.M6 Milestone Build. On the top of Doc in right corner, is selector with Doc versions 2.2, 2.3, 2.4, Latest - means 2.5. So Doc are right :slight_smile:

2 Likes

Thx, I really miss that “latest” selection, thanks

… yes it’s there. But a search will not find this (yet?), see:

The new profiles are explained in .../configuration/items but the search will just point to Developing a Profile. Maybe that changes after 2.5 release :wink:

It depends where are You searching, I prefer google :wink:

Really Cool thing! Thanks a lot!

Is it work on 2.5M5.

Trying to use it in this way


Contact   DoorEntrance 
    "Door Entrance Contact [%s]" 
    (gDoorsSensors)
    {mqtt="<[mosquitto:zigbee2mqtt/0x00158d00044b228e:state:JS(getZigbeeContact.js)]"} 

DateTime  DoorEntranceLastChange 
    "Door Entrance Last Change [%1$td.%1$tm.%1$tY %1$tH:%1$tM:%1$tS]" 
    (gDoorsLastChange) 
    {mqtt="<[mosquitto:zigbee2mqtt/0x00158d00044b228e:state:JS(getZigbeeContact.js)]" [profile="timestamp-update"]}

But it do not updates item(((

This is error i get

2019-12-05 18:58:25.600 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'DoorEntranceLastChange'

Profiles work with channels. You have no channel. Profiles do not work with 1.x binding version.