Set last tripped/alarm date, or last action date to an item - best practice?

What happens if it takes longer than a second to get into the rule? What happens if the door.state is not (or no longer) 1? I’d put a logInfo before the group search and another in a else clause to see if either is happening.

I’ve changed that value to something longer and also put a loginfo at the start of the rule with no checks required to execute the line. When it breaks everything breaks - no rules run. I’m just in the middle of a full OH2 reinstall to see if that makes a difference…

After a complete OH2 reinstall and Snapshot #723.

I have yet to have RULES break but for some reason i can’t get the rule to work correctly with “changedSince(now.minusSeconds(5))”. I changed the value to 5 seconds for a larger gap incase that was the problem. I’ve tried all numbers including the default of 1 and same results.

Items:

Group gDoorSensors1
Switch 		vGarageDoor	 				"Garage Door [MAP(doorsensor.map):%s]"           <door>           (gDoorSensors1, gRemindDoorSensors)    	{ channel="zwave:device:159a54632e2:node15:sensor_binary"
Switch 		vHouseDoor					"House Door [MAP(doorsensor.map):%s]" 				<frontdoor>			(gDoorSensors1, gRemindDoorSensors)    	{ channel="zwave:device:159a54632e2:node13:sensor_binary" }

Rule:

rule "A Door's State Changed. Log Timestamp"
when
        Item vGarageDoor changed
then
{
	logInfo("RULE.DOORSENSOR", "### A Door's State Changed. Log Timestamp ###") 
	gDoorSensors1.members.filter(s|s.changedSince(now.minusSeconds(5))).forEach[ door |
                logInfo("RULE.DOORSENSOR", "[INFO] Door state changed: {}", door.name)
        ]
    ]
}
end

Openhab Log for ON/OFF Trigger:

2017-01-16 10:29:55.674 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - ### A Door's State Changed. Log Timestamp ###
2017-01-16 10:29:55.685 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - ### A Door's State Changed. Log Timestamp ###

If i change the rule so it’s !s.changedSince(now.minusSeconds(5)) instead of s.changedSince(now.minusSeconds(5)) Like this:

rule "A Door's State Changed. Log Timestamp"
when
        Item vGarageDoor changed
then
{
	logInfo("RULE.DOORSENSOR", "### A Door's State Changed. Log Timestamp ###") 
	gDoorSensors1.members.filter(s|!s.changedSince(now.minusSeconds(5))).forEach[ door |
                logInfo("RULE.DOORSENSOR", "[INFO] Door state changed: {}", door.name)
        ]
    ]
}
end

I get the following results now:

OPEN:

2017-01-16 10:37:00.284 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - ### A Door's State Changed. Log Timestamp ###
2017-01-16 10:37:00.290 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - [INFO] Door state changed: vHouseDoor
2017-01-16 10:37:00.294 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - [INFO] Door state changed: vGarageDoor

CLOSED:

2017-01-16 10:37:37.619 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - ### A Door's State Changed. Log Timestamp ###
2017-01-16 10:37:37.624 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - [INFO] Door state changed: vHouseDoor
2017-01-16 10:37:37.627 [INFO ] [arthome.model.script.RULE.DOORSENSOR] - [INFO] Door state changed: vGarageDoor

Am i missing something?

What persistence are you using? Are these Items being persisted?

Try a Thread::sleep(100) to give persistence a chance to catch up. I’ve found that often the rule gets triggered before persistence has finished saving the update.

I use this rule, see my postings in this thread.

Thread::sleep(300) // give persistence time to catch up
val haveHistory = gFensterkontakte.members.filter[c|c.lastUpdate != null]
logInfo("Test", "There are " + haveHistory.size + " contacts that have a last update")

val mostRecent = haveHistory.sortBy[lastUpdate].last
logInfo("Test", "Most recent contact is " + mostRecent.name + " and its state is " + mostRecent.state)
if(mostRecent == null) logError("Test", "Failed to find most recent")
else if(mostRecent.state == OPEN){
    val dt = gKontakteZeit.members.filter[dt|dt.name == mostRecent.name+"_last_opened").head
    if(dt == null) logError("Test", "Failed to find " + mostRecent.name+"_last_opened")
    else dt.postUpdate(new DateTimeType)
}
else {
    logInfo("Test", mostRecent.name + " is not OPEN")
}

Which items do i have to persist for this rule working? And which persistence do i have to use? Do i have to use rrd4j or is it working with mapDB too? I know that mapDB is not working with OH2. But i only want to know, if i need to persist every change for a longer time or if it´s enough to store the latest value?

I have to groups used inside my rule: “gFensterkontakte” and “gKontakteZeit”

I will try that tonight. I’m running mapdb for restore & mysql for historical and persisting all values:

mapdb.persist

Strategies {
    default = everyUpdate
}

Items {
    // persist all items on every change and restore them from the db at startup
    * : strategy = everyChange, restoreOnStartup
}

mysql.persist

Strategies {
        default = everyUpdate
}

Items {
        // persist all items on every change and restore them from the db at startup
        * : strategy = everyChange
}

I persist the items in a rrd4j databese and this rrd4j files for window contacts and lights get always corruptet.

I have to delete them manually and then it´s working for some time.

Strategy is everyChange, everyMinute, restoreOnStartup

Is rrd4j the wrong persistence for this case?

All other rrd4j files are working without problems. Only the rrd4j files generated from this items of the rule are corrupt.

I persist the items with MapDB also, this persistence is working. Even if i remove all lights*.rrd-files, and restart openhab, i get the last time of opening the window last time or power on the lights.

There is no “wrong” persistence engine. RRd4j has its advantages and disadvantages. But if all you want is the lastUpdate time and you don’t need to check on historic data then MapDB is probably a better choice. If you want accurate data for a long time, MySQL, InfluxDB, et al would be better choices.

For this case the only interesting thing is the last update of the item.

So i will switch to MapDB - i´m using it already for the last state after reboot.

My main persistence is rrd4j, so if i remove the items from this rule from rrd4j and want to use the already running MapDB persistence, i have to add mapdb in the rule behind the command item.lastupdate?

Yes. item.lastUpdate("mapdb") or you can change your default persistence to be mapdb. Personally I always specify which persistence I want to grab things from.

I changed my rule to this:

rule "Licht State Changed"
when
        Item Licht received update // NOTE: the rule will trigger multiple times per event
then
        Thread::sleep(500) // give persistence time to catch up
        val haveHistory = Licht.members.filter[l|l.lastUpdate("mapdb") != null]
        val mostRecent = haveHistory.sortBy[lastUpdate].last
        if(mostRecent == null) logError("Test Licht", "Failed to find most recent")
        else if(mostRecent.state == ON){
                val dt = gLichtZeit.members.filter[dt|dt.name == mostRecent.name+"_last_switched_on").head
                if(dt == null) logError("Test Licht", "Failed to find " + mostRecent.name+"_last_switched_on")
                else dt.postUpdate(new DateTimeType)
        }
end

I only added mapdb to this line:

val haveHistory = Licht.members.filter[l|l.lastUpdate("mapdb") != null]

But the rule isn´t working anymore. I get no updates on my items anymore.

Can someone tell me, whre i have to add (“mapdb”) in the rule above to use mapdb instead of rrd4j?

I have tested some more places, where i added mapdb, but i always got errors… I think the one position where i added mapdb in the rule above was not enough.

Ate you sure MapDB is working? I’d you change it to rrd4j in the call to last update does it work?

Yes, MapDB is working.

Since i installed MapDB, after an Openhab restart all my items are filled with the last value. Before all values changed to — or undefined and i had to wait, until the next update from the devices starts.

If i remove (“MapDB”) and add the items back to the rrd4j persistence, it is working again.

If i remove the Items from rrd4j persistence, the old value from MapDB stays forever and a trigger doesn´t change the value.

Maybe i have to add mabdb to the other val also (val mostrecent)? or is the line i wrote it in the only one and it is enough for the rule to work?

Could it be, that the rule is not working the first minutes of starting openhab?

When restarting openhab, i get many many errors from this rule, but after some minutes no more erors in the log and it seems that the rule is working now with mapdb…

Can i make a delay in the rule, so that the rule will be first triggered after a minute or so?

Indeed, the second call to lastUpdate needs to have “mapdb” as well (haveHistory.sortBy[lastUpdate("mapdb")].last)

Thanks, now no more errors on startup of openhab.

sorry for bumping to this old question.

what is the differenz between:

if(door.state == OPEN)
{
gDoorSensorsTime?.members.filter(dt|dt.name == door.name+"_Last_Opened").head.postUpdate(new DateTimeType)
}

if(door.state == OPEN)
{
gDoorSensorsTime.members.filter(dt|dt.name == door.name+"_Last_Opened").head.postUpdate(new DateTimeType)
}

for what is the “?” used to?

solved

Maybe someone can have a look in this thread?

The rule isn´t working anymore, since upgrade to OH2 or 2.1 snapshaot, can´t say the right date.

With OH1.9 it was working like a charm.