The goal here is to have a rule that says “if iPhone > 75 meters away from Home_Location, send Telegram message”. The rule should not send another message until I come back home, and leave again.
I first created these items, and this rule to define Home_Location (fake location for posting purposes):
Location Home_Location
Number Distance_From_Home
String Away_From_Home_Sent_Notice
rule "System Start"
when
System started
then
var loctmp = new PointType(new DecimalType(21.108810),new DecimalType(-90.337296))
Home_Location.postUpdate(loctmp)
end
Here’s the code for the notification rule:
rule "iPhone Home"
when
Item icloud_device_ff10e1d9_2150ce7_location changed
then
// Calculate distance from home, in meters
var Number distFromHome = Home_Location.distanceFrom(icloud_device_ff10e1d9_2150ce7_location)
Distance_From_Home.postUpdate(distFromHome)
// Sometimes reports 55m from home, even though I've never left
if (distFromHome > 75)
{
// Left building; Have we sent telegram?
if (Away_From_Home_Sent_Notice.state.toString() == "SENT")
{
// Skip sending message
logInfo("leftbuilding", "Already sent message since leaving the building")
return;
}
// Left building; Need to send telegram
val actions = getActions("telegram","telegram:telegramBot:1abcd474")
if (actions !== null)
{
// Store notice has been sent so not to repeat notices
Away_From_Home_Sent_Notice.postUpdate("SENT")
actions.sendTelegram("Nelix has left the building!")
}
else
{
logError("leftbuilding", "Telegram Issue: Unable to get actions")
}
}
else
{
// Arriving home; Still home; Reset sent status
logInfo("leftbuilding", "We have arrived, or are still home")
Away_From_Home_Sent_Notice.postUpdate("NONE")
}
end
Does that look good? I wasn’t sure if String was an OK type to use for the ‘has sent message’ identifier.
Thanks for taking a look!