Check timespan since last item state update

Hi,
I have a rule triggering our vacuum robot to clean our apartment at certain days (MON, WED, FRI).
This works fine and I do not intend to change it, BUT
I want to check how long nobody has been home and then decide if to run the vacuum or not.
Why ? If we are on vacation, we do not need the vacuum to run.
The idea:

  • check if item gPresence has the state “OFF” (item gPresence is a switch typ): this is easy
  • check how long gPresence has been in “OFF” state: this is where I need help
    ++ My idea is to check (e.g.) if gPresence has been in “OFF” for longer than 3 days. If it has been in “OFF” for that long, it’s considered we are on vacation.
    ++ I’d check when gPresence was last updated and (gPresence.lastUpdate)
  • decide to either continue with the rule or not: this is easy

My persistance is influxdb.

if (gPresence.state == OFF){
   val DateTime lastUpdate == gPresence.lastUpdate
   val days_between = lastUpdate.until(now, ChronoUnit.DAYS)
   if (days_between >= 3){
      return;
   }
   else {
      ... // continue with rule
   }
}
 

Am I on the right path ? Or can my idea be solved easier ?

Thanks

Should be possible to check with gPresence.previousState(true).timestamp.
The true parameter is: the previous state must be different from the current state. So if gPresence.state is OFF, gPresence.previousState(true).timestamp should give you the time (and date) when gPresence changed from ON to OFF.

I just have an away switch and turn that on when I am on holidays etc so the front gate doesn’t open and also when the away is on it sets random lights on and off etc and don’t do things that I need doing when I am at home. (opening curtains etc)

Thanks folks.
The idea manually triggering a specific switch sounds easily doable, but I guess I like automation.

I’ll test the timestamp solution @Udo_Hartmann suggested and will report back.

Thanks.

Yet another option is .cangedSince()

var Urlaub=false
if(gPresence.state==OFF && !gPresence.changedSince(now.minusDays(3)))
    Urlaub=true    
1 Like

that’s also the solution I did. Works like a charm. I use it to allow a state-change only every 30mins.

Sorry for my late reply,
my solution (which works like a charm) is:

var VACATION = false
if (gPresence.state == OFF && !gPresence.changedSince(now.minusDays(3)))
{
	VACATION = true
}
//... continue if VACATION == false