[SOLVED] Add LastUpdated info for items for z-wave sensors

How can I add date time information for each item for z-wave sensor when an update was received from it?
ex: - I want to know when the battery level was last time reported by sensor and received in OH2.
- I want to know when alarm was reported by sensor and received in OH2

See: https://www.openhab.org/docs/configuration/persistence.html#persistence-extensions-in-scripts-and-rules

thanks @vzorglub

Do you know how this info could be added on Paper UI, Habmin ?

You can’t as far as I know,
You will have to create items and rules in text files

Example for displayable timestamps, with OH 2.3 onwards

Items

Group gStampMe   // to indicate which Items to timestamp
Group gStamps      // holder for the actual timestamp Items
Contact someSensor "my pir" <icon> (gStampMe)   // put items in that group
DateTime someSensor_stamp  "Last updated [%1$td/%1$tm %1$tR]"  (gStamps)  // same name with _stamp added
Contact otherSensor "that pir" <icon> (gStampMe)
DateTime otherSensor_stamp  "Last updated [%1$td/%1$tm %1$tR]"  (gStamps)

Rule to do the timestamping

rule "Detector Timestamping"
when
    Member of gStampMe received update OPEN  // I ignore the PIR timeout event
then
    val stampName = triggeringItem.name + "_stamp"   // deduce the stamp name
    val myStamp = gStamps.members.filter[i | i.name == stampName].head // get relevant stamp Item
    if (myStamp !== null) {
        myStamp.postUpdate(new DateTimeType()) // defaults to now
    } // else no stamp Item to update, report an error if you wish
end

No need for group filtering to get an item…

I use another approach… I’m retrieving the “Last Wakeup Time” attribute from REST API.
This way I can check when the device communicated with the controller (and that means, of course, when the device reported the battery).
Here is an example from a WALLC-S battery switch - node 29:

DateTime Corridor2_Light_LastUpdate "Corridor2 Light [%1$td.%1$tm.%1$tY %1$tH:%1$tM:%1$tS]" <calendar> (Battery_Switch_LastUpdate) {http="<[http://127.0.0.1:8080/rest/things/zwave%3Adevice%3A1628a3e05cc%3Anode29:600000:JS(lastwakeup2datetime.js)]"}

and the corresponding lastwakeup2datetime.js transformation:

(function(json){
try{
	var otdata = JSON.parse(json);
	var tzoffset = new Date().getTimezoneOffset() * 60000;
    var millis =  new Date(otdata.properties.zwave_lastwakeup).getTime();
    return new Date((millis) - tzoffset).toISOString().slice(0,-1);
} catch(e) {
    return false;
}
})(input)

1 Like

@rossko57, I tried to do as you described and I can see in log the new stamp item updated, but it is not clear how can attach the new item to the thing to be displayed on Paper UI or other?

You can add a manually created item to the paperUI. The paperUI is only for management.
You will need to create a sitemap or use HABpanel