Rule should run differently if something happened in the past

Hey guys,

I wrote some rules yesterday which are running perfectly fine. But I need something to not run the rule in case something happend in the passed. I give you an example.

I created a rule which automtically turns on the light in the kitchen when our stairway light was turned on in the morning on a weekday and if its still dark outside. The code is attached.

Idea of the rule is of course to have the house “smart” because it knows if someone comes down the stairway on the weekday in the morning the person will go in the kitchen for breakfast and after that leave the house for work. There is know one at home over the day on a weekday so this makes sense for us.

But problem is that when we go in the hall to take on our shoes the stairway light goes on because the shoes are directly near to the stairway. I already tried to change the settings of the stairway light but didnt helped.

So my idea is to add something to the rule like “if the kitchen ligt was turned of in the last 10 minutes” dont run the rule.

Thats the rule:

rule "Automatisches Küchenlicht Wochentags"
when
	Item EG_Flur_Licht_Treppenlicht changed to ON
then
	if (Daemmerung.state <= 250 && Tagesart.state == "Wochentag" && now.getHourOfDay >= 5 && now.getHourOfDay <= 8){
	sendCommand (EG_Kueche_Licht, ON)
    sendCommand (EG_Kueche_ArbeitsLicht, ON)
    sendCommand (EG_Esszimmer_Dose_Sideboard, ON)
    }
end

Of course I know that I have to add this as a part of the “if” and to add is as “not”.
So described as text I have to add - && “the kitchen light was not turned of in the last 10 minutes”.
How to add it as a “not” is clear to me. But I have no idea how to check the status in a specific timeslot.

Any idea?

The easiest way would be (from my p.o.v.) to make the information available whether the kitchen light was turned off within the last 10 minutes. And then query that information in your rule. Look into timers for things like that.

This is an excellent use case for the timestamp-change profile. See the Profiles sections in the item docs:

Short-short version: If you have an item that uses the timestamp-change profile for the kitchen light channel then you will easily be able to test on/off state of the light (regular item) and the last time it was changed (timestamp item).

1 Like

Hey guys I worked on this a little more and came closer to my target.
Im able to compare the current time with the timestamp of an item now. This works fine.
But I also want to check if the timestamp is older thant 10 minutes and only if its the case the rule should run.
I will add my rule below. My idea is to use the timestamp and add 10 minutes to it and than compare if the timestamp is bigger as the current time. If thats the case it means that the timestamp is not older than 10 minutes. But I don’t know how to add 10 minutes to my timestamp correctly.

Timestamp and Date Items

DateTime date_time_today "Heute Uhrzeit [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"   <calendar>  { channel="ntp:ntp:local:dateTime" }
DateTime        Zeitstempel_Soeren_Home "Timestamp Sören [%1$td.%1$tm.%1$tY %1$tH:%1$tM]"

Rule

rule "Testerei"
when
    Item Test_Schalter  changed to ON
then
    if (((Zeitstempel_Soeren_Home.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli) > ((date_time_today.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)){
    Echo_Buero_TTS.sendCommand('Wenn du das hörst hat es funktioniert')
    }
end

Rule that files the timestamp

rule "Sören Zuhause"
when
    Item HandySoeren  changed to OPEN
then
    Zeitstempel_Soeren_Home.postUpdate( new DateTimeType() )
end

So any idea how to add 10 minutes to the timestamp in the first rule?

As you already converted your times to millis, you just need to add/sustract the 10 minutes as a number in milliseconds.

Oh boy…

Shame on my head… that I didn’t think of it myself. Have the calculated with minutes the whole time and not thought about that I have converted to milliseconds.

Sorry and thank you very much

No worries. And you are welcome