[SOLVED] iCloud time between timestamp Lastupdate and current time

Hi there,

I managed to use the iCloud Binding. But when we go to bed and turn our phones off, the location stays the same and my presence detection thinks, we are still there. I would like to change that.
My idea is to run a rule every 10 minutes that checks whether the timestamp Lastupdate changed within the last 20 minutes. If not, the Presence detection will be switched to OFF. Sadly, I couldnt manage to get the time comparison to work.
Does anyone has an idea?

This is my first draw:

rule "Handy aus"
when 
    Time cron "0 0/10 * 1/1 * ? *"
then
    if(*Current_Time* - iPhone_Location_Tobias_LastUpdate < 20) {
        iPhone_Tobias_Home.postUpdate(OFF)
    }
end

See https://www.openhab.org/docs/configuration/rules-dsl.html#datetime-item for how to convert and compare date times.

Well… You are, aren’t you?
You need to do a house mode kind of trick. UP, IN BED, SICK, PARTY…
As I see it, you presence is working. You are in the house.

Yes its true that I am at home, but I would like my presence detection to think that I would be away while I am sleeping. This way the alarm system is working when I am asleep. And sometimes my wife forgets to turn her phone on when shes going and then the presence detection thinks, that she is still at home.

I managed to produce this code now, but for some reasons the system doesnt like the “-” in my rule. Any ideas?

rule "Handy aus"
when 
    Time cron "0 0/5 * 1/1 * ? *"
then
    val jodaTimeTobias = new DateTime(iPhone_Location_Tobias_LastUpdate.state.toString)
    val jodaTimeTheres = new DateTime(iPhone_Location_Theres_LastUpdate.state.toString)    
    val jodaCurrentTime = new DateTime(now())
    val DiffTimeTheres = jodaCurrentTime - jodaTimeTheres
    val DiffTimeTobias = jodaCurrentTime - jodaTimeTobias
    if(DiffTimeTobias < 900000) {
        if(iPhone_Tobias_Home.state == ON) { iPhone_Tobias_Home.postUpdate(OFF) }
    }
    if(DiffTimeTheres < 900000) {
        if(iPhone_Theres_Home.state == ON) {iPhone_Theres_Home.postUpdate(OFF) }
    }
end

I managed to get it tow work now :slight_smile:

Here is the code

rule "Handy aus"
when 
    Time cron "0 0/5 * 1/1 * ? *"
then
    val Number epochTimeTobias = (iPhone_Location_Tobias_LastUpdate.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
    val Number epochTimeTheres = (iPhone_Location_Theres_LastUpdate.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
    val Number epochCurrentTime = now.millis
    val Number DiffTimeTheres = (epochCurrentTime - epochTimeTheres)/60000
    val Number DiffTimeTobias = (epochCurrentTime - epochTimeTobias)/60000
    
    if(DiffTimeTobias > 15) {
        if(iPhone_Tobias_Home.state == ON) { iPhone_Tobias_Home.postUpdate(OFF) }
    }
    if(DiffTimeTheres > 15) {
        if(iPhone_Theres_Home.state == ON) {iPhone_Theres_Home.postUpdate(OFF) }
    }
end