Show date/time of last sensor update


i use a lot of weather sensor with RFXCOM binding. Sometimes reception is bad / battery gone. I like to show the last update date/timestamp. What would be the best way to do so?

You could use persistence and try to change the following sample to your needs.


Thanks. I tried this:

var SimpleDateFormat df = new SimpleDateFormat( "YYYY-MM-dd HH:mm:ss" )
var String Timestamp = df.format( new Date() )
logInfo("RFX_Wind_Update", Timestamp )

Logfile outputs correctly:

[o.model.script.RFX_Wind_Update] - 2015-08-26 21:09:34

but the item state itself isn’t set. Item definition looks like:

String RFX_Wind_Update “Wind letztes Update:” (FF_Office,Klima)

Make a DateTime item:

DateTime RFX_Wind_Update "[%1$tm/%1$td %1$tH:%1$tM]"

Have a rule update when the “paired” item is updated:

import org.openhab.core.library.types.* // not needed in OH2

rule Foo
  Item RFX_Wind received update
  RFX_Wind_Update.postUpdate( new DateTimeType() )

I can’t believe I didn’t think of this. [smacks head]

I was using a Text item and formatting the string itself in my rule. This is so much better.



1 Like

Works perfectly, thank you. Just to understand the system better: Why did the rule with the string not work?

A String works just fine; it’s just too much work! And it’s hard to use for comparison later if you need that.

In my sitemaps I use:

Text item=Fibaro_Motion_1_LastUpdate valuecolor=[>6000="red",>600="orange",<=600="green"]

Which perfectly shows me the status of the devices in color code.

1 Like

How is this item defined in your items file, so we can understand why this works? It would be very nice to have stale updates highlighted by color!

I’m using a DateTime item. OH can do maths on dates. Very nice feature. I think I got this from the sample rules, but not too sure. Heres my full config stack:


DateTime Fibaro_Motion_1_LastUpdate     "Last seen [%1$ta %1$tR]" <clock>


rule "Records when device was last seen"
  Item Fibaro_Motion_1 received update or
  Item Fibaro_Motion_1_Temp received update or
  Item Fibaro_Motion_1_Lux received update
  postUpdate(Fibaro_Motion_1_LastUpdate, new DateTimeType())


Text item=Fibaro_Motion_1_LastUpdate valuecolor=[>6000="red",>600="orange",<=600="green"]

Hope this helps.


I think you could also use something like Switch.lastUpdate(WindowsStart,“rrd4j”) if you have set up persistence correctly. Saves you the trouble of creating a rule.


@Kai Would be nice to have a timestamp (lastUpdated) exposed via REST + org.openhab.core.items.GenericItem interface similar to state. Or put differently: State only makes sense with a timestamp nearby to avoid fishy data.

1 Like

I wouldn’t add it to the GenericItem, see here a previous discussion. But we can think of other ways to make it more easily available to UIs through REST (although we have to be careful not to send too many events as some bindings might create several state updates per second).


Hi All

I have been doing this in openHAB for a while, but recently updated to the latest build and now everything shows as Green? Has something changed that would cause this?

I think same problem described here:

I have the same problem, just waiting for a new stable version which will (hopefully) fix this.

I use this lambda for timestamps:

val Functions$Function2<GenericItem, String, String> getTimestamp = [  //function (lambda) to get a timestamp. Returns formatted string and optionally updates an item
    date_format |
    var date_time_format = date_format
    if(date_format == "" || date_format === null) date_time_format = "%1$ta %1$tT" //default format Day Hour:Minute:Seconds
    var String Timestamp = String::format( date_time_format, new Date() )
    if(item != NULL && item !== null) {
        var t = new DateTimeType()
        if(item instanceof DateTimeItem) {
            postUpdate(item, t)
            logInfo("Last Update", + " DateTimeItem updated at: " + Timestamp )
        else if(item instanceof StringItem) {
            postUpdate(item, Timestamp)
            logInfo("Last Update", + " StringItem updated at: " + Timestamp )
            logWarn("Last Update", + " is not DateTime or String - not updating")

You call it in a rule like this:

rule "HEM Last Updated"
    Item HEM_C1 received update or
    Item HEM_C2 received update
    getTimestamp.apply(HEMLastUpdated, "")


rule "Aeon Labs Multisensor LA Last Updated"
    Item AeonMS61LA changed or
    Item AeonMS62LA changed or
    Item AeonMS63LA changed or
    Item AeonMS64LA changed
    getTimestamp.apply(AeonMS6LastUpdatedLA, "%1$ta %1$tR")


rule "Autolock Front Door"
    Item virtualfrontDoorDoorContact changed to CLOSED
    // Front Door Closed
    if(DoorTimer !== null) DoorTimer.cancel
    DoorTimer = null
    logInfo("FRONT_DOOR", "Front Door CLOSED - Master Sensor: " + FrontDoorSensorSelected.state )
    postUpdate(hallway_HSM200_setcolour, 3)  //RED (closed, unlocked)
    var String Timestamp = getTimestamp.apply(virtualfrontDoorLastUpdate, "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS")
    AutoRelock.apply(" by: Door Close Auto Lock", Timestamp, LockTimers, "FrontDoor", DoorRelockTime)


This way, it doesn’t matter if the item is a DateTime type, or a String, you can optionally specify the format of the string, and you can get the Timestamp value returned as a String to use in notifications, or logs etc.

I have implemented this LastUpdate method but it stopped changing the colors after updating Openhab to 2.2.0-1. The timer is still working. It seems valuecolor can not get the time in seconds from a DateTime item anymore. Any ideas?

Thank you for this!

I was looking for a way to date/time stamp the zWave nodes and this will do it . . .

Best, Jay

1 Like

I´m just trying your examples.

I have these PIR´s all around, and I want to know when they last fired.
I thought I could just


rule "Spisestue PIR Last Updated"
    Item spise_pir changed
    getTimestamp.apply(spise_pirUpdated, "%1$ta %1$tR")


		Text item=spise_pirUpdated label="Sidste åbnet [%1$tH:%1$tM %1$td.%1$tm.%1$tY]"

But it doesn´t really work…
Isn´t it possible to catch a timestamp from an action and create this time to an item, to show it in a sitemap?

This is the error I get:

2018-07-02 21:32:34.592 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Spisestue PIR Last Updated': The name 'getTimestamp' cannot be resolved to an item or type; line 5, column 5, length 12

You are missing the code under “I use this lambda for timestamps:”