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 |