Presence detection with iPhone and Owntracks

Hi, hoping someone can help me as have spent the day going mad with this. I’m trying to do presence detection of my iPhone using a combination of the network binding and mqtt with user-defined Regions in Owntracks (i’m using OpenHAB 2, so unable to use mqttitude) I have these items declared:

Group StuMobiles
Switch Presence
Switch Stu_MainPhone (StuMobiles) {channel="network:device:192_168_0_8:online"}

String StuRegionAction {mqtt="<[mosquitto:owntracks/stu/iPhone6/event:state:JSONPATH($.event)]"}

Now when I check my event.log file, i get things like:

2016-04-09 19:55:22.905 [GroupItemStateChangedEvent] - StuMobiles changed from OFF to ON through Stu_MainPhone
2016-04-09 19:55:22.906 [ItemStateChangedEvent     ] - Stu_MainPhone changed from OFF to ON
2016-04-09 19:53:53.315 [ItemStateChangedEvent     ] - StuRegionAction changed from leave to enter

…which is all good.

Unfortunately it’s well-known fact that the iPhone flaps around a bit on WiFi when it sleeps and checks in again, so all I want to do is say that when StuMobiles is ON, and StuRegionAction is enter, then set Presence to ON. And when StuMobiles is OFF, and StuRegionAction is leave, then set Presence to OFF. I’ve written this rules file, but it disregards the StuRegionAction setting, despite the conditions in the When, so Presence just switches on and off with StuMobiles. Where am I going wrong please?

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule "Stu is home"

when
        Item StuMobiles changed from OFF to ON and
        Item StuRegionAction changed from leave to enter
then
        logInfo("PresenceCheck", "Stu is home")
        sendCommand(Presence, ON)
        sendMail("xxxxx@gmail.com","Stuart is home","Stuart's iPhone has arrived home")
end


rule "Stu has left home"

when
        Item StuMobiles changed from ON to OFF and
        Item StuRegionAction changed from enter to leave
then
        logInfo("PresenceCheck", "Stu has left home")
        sendCommand(Presence, OFF)
        sendMail("xxx@gmail.com","Stuart has left home","Stuart's iPhone has left home")
end

I also tried these “when” conditions, but they didn’t do anything at all to Presence

Item StuMobiles is ON and
Item StuRegionAction is enter

and

Item StuMobiles is OFF and
Item StuRegionAction is leave

I use JSR223 rules instead of Xtext so I very well may not know what I’m talking about here. However, my understanding is that only disjunctive (“or”) trigger conditions are allowed (vs. “and”) [[source]] (https://github.com/eclipse/smarthome/blob/master/bundles/model/org.eclipse.smarthome.model.rule/src/org/eclipse/smarthome/model/rule/Rules.xtext). However, I’m surprised the rule would be parsed and work at all if that’s true. Logically speaking, “and” doesn’t make sense for events (vs. states) because the events don’t happen simultaneously.

You could try using an “or” for the “when” conditions and then checking the “and” of the states with an expression in the “then” part of the rule.

Great idea, that makes total sense. However my rule-writing skills appear to be still not up to scratch. Below is what I have now

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule "Stu is home"

when
        Item StuMobiles changed from OFF to ON or
        Item StuRegionAction changed from leave to enter
then
        if(StuMobiles.state == ON && StuRegionAction == enter) {
        logInfo("PresenceCheck", "Stu is home")
        sendCommand(Presence, ON)
        }
        else {
        }
end


rule "Stu has left home"

when
        Item StuMobiles changed from ON to OFF or
        Item StuRegionAction changed from enter to leave
then
        if(StuMobiles.state == OFF && StuRegionAction == leave) {
        logInfo("PresenceCheck", "Stu has left home")
        sendCommand(Presence, OFF)
        }
        else {
        }
end

I think the issue is what I need to query on the StuRegionAction item, which as per my first message is declared as a string… the error in the OpenHAB debug log is:

Error during the execution of rule 'Stu is home': The name 'enter' cannot be resolved to an item or type.

What’s the right type here? I’ve tried .state and .name as a suffix in the if statement, but got the same error. I’ve searched the Xtend documentation and can’t even find anything about .state (which seems to be very common in rule file examples on this site), let alone finding any info about what I should do for text. Are there any resources that everyone would recommend I read? I’m a techy-minded individual, I love learning about this sort of thing and happy to put the hours in, I’m just lacking knowledge…

Just guessing here since I don’t use Xtext, but I’d try…

StuRegionAction.state == "leave"

with quotes around String-typed value (same for “enter”).

Success!! Works perfectly now thank you :grinning: