Show date/time of last sensor update


(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
when
  Item RFX_Wind received update
then
  RFX_Wind_Update.postUpdate( new DateTimeType() )
end

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

Thanks!

Rich


(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.
BR


(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

Hey,
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:

.item:

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

.rule:

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

.sitemap:

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

Hope this helps.
BR


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:
https://community.openhab.org/t/valuecolor-problems-after-update-to-oh2-2/37636


(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
    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", item.name + " DateTimeItem updated at: " + Timestamp )
            }
        else if(item instanceof StringItem) {
            postUpdate(item, Timestamp)
            logInfo("Last Update", item.name + " StringItem updated at: " + Timestamp )
            }
        else
            logWarn("Last Update", item.name + " is not DateTime or String - not updating")
    }
    Timestamp
    ]

You call it in a rule like this:

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

or:

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

or:

rule "Autolock Front Door"
when
    Item virtualfrontDoorDoorContact changed to CLOSED
then
    // 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)

end

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.


(Luciano De Paula) #18

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?


(Jason Wiseman) #19

Thank you for this!

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

Best, Jay


zWave Monitor lastSent & lastReceive Date/Time on Nodes
(Kim Andersen) #20

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:

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

Sitemap

		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

(Rich Koshak) #21

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


(Kim Andersen) #22

I have no idea where to place that part, to be honest.


(Rich Koshak) #23

At the top of your .rules file.


(Kim Andersen) #24

Thx Rich.

Can´t get it to work. Tried with another PIR (Aeotec Multisensor6).

My rule file:

val Functions$Function2<GenericItem, String, String> getTimestamp = [  //function (lambda) to get a timestamp. Returns formatted string and optionally updates an item
    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", item.name + " DateTimeItem updated at: " + Timestamp )
            }
        else if(item instanceof StringItem) {
            postUpdate(item, Timestamp)
            logInfo("Last Update", item.name + " StringItem updated at: " + Timestamp )
            }
        else
            logWarn("Last Update", item.name + " is not DateTime or String - not updating")
    }
    Timestamp
    ]


rule "Multisensor6 PIR Last Updated"
when 
    Item ZWaveNode5ZW100MultiSensor6_MotionAlarm changed from OFF to ON
then
    getTimestamp.apply(ZWaveNode5ZW100MultiSensor6_MotionAlarmUpdated, "%1$ta %1$tR")
end

My item:

String ZWaveNode5ZW100MultiSensor6_MotionAlarmUpdated <clock>

And my sitemap:

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

This is my log:

2018-07-02 22:42:40.676 [vent.ItemStateChangedEvent] - ZWaveNode5ZW100MultiSensor6_MotionAlarm changed from OFF to ON
2018-07-02 22:42:41.412 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Multisensor6 PIR Last Updated': null