Location between iPhone and home with rule engine and script - What script type?

  • Platform information:
    • Hardware: rPi 4
    • OS: rPi
    • openHAB version: OH3

I would like to change a state of an item based on the location of the user (iPhone) from home.
I setup the rule first in the GUI rule: when item location change
then I try to create a script from one of the users here in the community. But it doesn’t work and I am not sure which of the 3 script type that should be (python I guess not) but javascript or rule?

THE SCRIPT
rule “iPhone Home”
when
Item iPhone12Pro_Location changed
then
val PointType home_location = new PointType(new DecimalType(xx.948837), new DecimalType(xx.959258))
val PointType phone_location = iPhone12Pro_Location.state as PointType
val int distance = phone_location.distanceFrom(home_location).intValue()

// home radius geofence to determine home/away (in meters)
if ( distance < 500)
{
logInfo(logName, “I am at home.”)
}
else
{
logInfo(logName, “I am away.”)
}
end

THE ERROR

  1. The method or field rule is undefined; line 1, column 0, length 4
  2. The method or field when is undefined; line 2, column 19, length 4
  3. The method or field changed is undefined; line 3, column 62, length 7
  4. The method or field then is undefined; line 4, column 70, length 4
  5. The method or field logName is undefined; line 13, column 472, length 7
  6. The method or field logName is undefined; line 17, column 519, length 7
  7. The method or field end is undefined; line 19, column 544, length 3
  8. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 1, column 5, length 13
  9. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 3, column 28, length 4
  10. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 3, column 33, length 28

When you create the rule just put this in for the DSL script - The rule bits at the stop should be defined within the GUI

Thanks!
I also noticed then that logName was not defined, should be set or manually set as “openhab” or any other logfile name.

You can either define the logName at the start:

var logName = "Iphone_Home"

or call the function with a string:

logInfo("Iphone_Home", "I am at home.")
1 Like