For an example of alerting when the travel time gets too long or a different route from usual is see below. I configured the rule template to only run every minute between 07:00 and 07:20.
items.School_CheckTime.postUpdate(time.toZDT());
var usualDistance = Quantity("17.6 mi");
var distanceBuffer = Quantity("0.5 mi");
var usualTime = Quantity("19.1 min");
var timeBuffer = Quantity("5 min");
var notificationId = "schoolDriveTime";
console.debug('Received travel info from Waze');
var arrivalTime = time.toZDT(items.School_TravelTime).toLocalTime();
function getTime(inst) {
let hour = inst.hour();
let meridian = "AM"
if(hour == 12) meridian = 'PM';
if(hour > 12) {
hour -= 12;
meridian = 'PM'
}
return hour + ":" + String(inst.minute()).padStart(2, '0') + " " + meridian;
}
// If route is more than distanceBuffer from the usual route
var differentRoute = items.School_TravelDistance.quantityState.subtract(usualDistance).greaterThan(distanceBuffer);
// If the travel time is more than timeBuffer from the usual route
var longerTime = items.School_TravelTime.quantityState.subtract(usualTime).greaterThan(timeBuffer);
var msg = "As of " + getTime(time.toZDT().toLocalTime());
if(differentRoute) msg += " a different route is suggested by Waze"
else msg += " the usual route is still the best"
msg += " to school and travel duration is "
+ items.School_TravelTime.quantityState.toUnit("min").float.toPrecision(3)
+ " minutes for an arrival time of " + getTime(arrivalTime);
console.info(msg);
items.School_TravelMessage.postUpdate(msg);
if(differentRoute || longerTime) {
actions.notificationBuilder(msg).addUserId('rlkoshak@gmail.com')
.withTitle('Time to leave!')
.withIcon('f7:car-fill')
.withOnClickAction('app:android=com.waze') // untested
.withReferenceId(notificationId)
.send();
cache.private.get(notificationId)?.cancel();
cache.private.put(notificationId, actions.ScriptExecution.createTimer(ruleID, time.toZDT('PT10M'), () => {
actions.notificationBuilder('cancel notification').withReferenceId(notificationId).hide().send();
cache.private.remove(notificationId);
}));
actions.Voice.say(msg, null, "sink:id"); // all speakers group
}
else {
cache.private.get(notificationId)?.cancel();
actions.notificationBuilder('cancel notification').withReferenceId(notificationId).hide().send();
cache.private.remove(notificationId);
}
The School_TravelMessage Item is a String Item I use with Embedded Waze Live Traffic Map Widget as the footer so I can see the travel time and the current traffic conditions in MainUI in the same widget. I use =items.Shcool_TravelMessage.state
for the footer property of the widget.