Show date/time of last sensor update

(Matthias Zirngibl) #1


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?

Convert Timestamp
(SiHui) #2

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


(Matthias Zirngibl) #3

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)

Please test the new Expire Binding
(John Cocula) #4

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() )

(Rich Koshak) #5

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.



(Matthias Zirngibl) #6

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

(John Cocula) #7

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

(Sebastian) #8

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.

(John Cocula) #9

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!

(Sebastian) #10

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.

How to post the event time for each sensor in a multi-sensor event
(Buzink) #11

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.

(hildner) #12

@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.

(Kai Kreuzer) #13

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).

(Bill Whyte) #15

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?

(Daniel) #16

I think same problem described here:

(Nicholas Waterton) #17

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.