Easier way for timestamps possible?

Hey guys,

I have some rules in which the or at least one of the factors to run the rule is if something happend within a specific timeframe. Therefor I create a timestamp in one rule and compare this timestamp in the main rule with the current time.

I feel that this is a lot of code for a quite small task. So I would like to show you how I have done this and maybe you have a easier approach for this.

Also I noticed that it sometimes stop working. When I go to visual studio code and mouse hover the timestamp items it takes quite a long time till the infomation gets displayed at the mouse. But after I’ve done that the rule itself works again. Maybe someone knows were this may come from.

Perhaps some info to understand my example rule. I would like to check if in the last x minutes the humidity was very high in the bathroom and the rule should only run if it’s the case. As I said it’s already working but I think it’s very complicated.

So this is the rule to create the timestamp.

rule "Zeitstempel Bad Hohe Luftfeuchtigkeit"
    Item OG_Bad_Luftfeuchtigkeit changed
    if ((OG_Bad_Luftfeuchtigkeit.state) > (80)){
    Zeitstempel_Bad_Feuchtigkeit_Hoch.postUpdate( new DateTimeType() )

And this is the main rule which checks the timestamp

rule "Badfenester Lüftungsautomatik und Benachrichtigungstimer"
	Item OG_Bad_Fenster changed to OPEN
	if (((Zeitstempel_Bad_Feuchtigkeit_Hoch.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli + 1200000) >= 
		((date_time_today.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli))
		sendCommand(OG_Bad_Rolladen, UP)
		createTimer(now.plusMinutes(10)) [|
			if (OG_Bad_Fenster.state == OPEN){
    		val telegramAction = getActions("telegram","telegram:telegramBot:Telegram_Bot")
			telegramAction.sendTelegram(1424274242L, "Das Fenster im Bad ist noch offen!")
			Echo_Kueche_TTS.sendCommand('Das Fenster im Bad ist noch offen!')	

Instead of using timestamps in any way, you could persist the humidity Item. Then in your rule, use a persistence method like
humidityItem.maximumSince(some time)
for this.

1 Like

Would this also work with switches?
Because this is only one example. I have other scenarios were for example it’s relevant if in the passed 30 minutes a spezific switch was on or if he is on since x

Work it out, see if it can do what you have in mind. Most persistence services store switches as 0/1, so maximumSince(time x) should tell you if it’s been on since x.

Definitely pursue the persistence approach described by rossko57. That’s most likely going to be the best path over all.

In addition, for Switches, one might be able able to eliminate the rule that keeps the timestamp using the timestamp Profile, assuming you want a timestamp every time the Item changes.

But I’m on a little bit of a crusade over “bad ways” to use DateTime Items and ZonedDateTime because it seems so many people go out of their way to make it way more complicated than it needs to be.

First review DateTime Conversion (openHAB 3.x). There are about 20 examples showing various ways to compare and manipulate date times.

As a rule of thumb, if you are using epoch you are probably not doing it in the easiest way.

To see if a saved timestamp is 20 minutes ago or more:

if((MyDateTime.state as DateTimeType).zonedDateTime.isBefore(now.minusMinutes(20)) {

Not only does that involve way less typing but it also is immediately apparent what it does.

How would I have to adapt this very efficient expression to also work in a oh-list-card as a condition for selecting the icon color?

@rlkoshak @rossko57
thanks a lot
will try around with it

Does a list card even have an icon?

See Building Pages - Components & Widgets | openHAB.

I was not precise enough. I mean a oh-label-item within a oh-list-card, like this

component: oh-list-card
config: {}
    - component: oh-label-item
        item: InnogyBridgeStatusText
        title: Innogy Bridge
        icon: "f7: house"
        iconColor: =(items. InnogyBridgeStatusText.state==='ONLINE')?'white':'red'

The answer is still the same, all the expressions that are possible in the UI are documented at the link I provided. It’s really off topic for this thread as everything else discussed here is for Rules, not the UI.