Hi there, I have an app that schedules the same method with different params in the data map. Is there any way to unschedule a specific instance of that scheduled method? From the documentation, it doesn't look like unschedule accepts anything but a method name.
Correct. From my experience it looks for scheduled jobs linked to the app by name and removes them. You might consider using a state variable with your parameters and just update it accordingly that the schedule job will leverage. I do that with the GCal Search app I maintain for the community.
Thank you! That was my backup plan but I was hoping to avoid yet another state to track.
On a related question: Do you know if schedule() with the option [overwrite: true] will overwrite anything with the same method name or does it respect the differences in parameters passed?
Yes, I am using this in many of my drivers to throttle some very chatty Tuya devices by sending an 'delayed' event later :
Example
def sendBatteryPercentageEvent(batteryPercent, isDigital=false) {
if ((batteryPercent as int) == 255) {
logWarn "ignoring battery report raw=${batteryPercent}"
return
}
def map = [:]
map.name = 'battery'
map.timeStamp = now()
map.value = batteryPercent < 0 ? 0 : batteryPercent > 100 ? 100 : (batteryPercent as int)
map.unit = '%'
map.type = isDigital ? 'digital' : 'physical'
map.descriptionText = "${map.name} is ${map.value} ${map.unit}"
map.isStateChange = true
//
def latestBatteryEvent = device.latestState('battery', skipCache=true)
def latestBatteryEventTime = latestBatteryEvent != null ? latestBatteryEvent.getDate().getTime() : now()
//log.debug "battery latest state timeStamp is ${latestBatteryTime} now is ${now()}"
def timeDiff = ((now() - latestBatteryEventTime) / 1000) as int
if (settings?.batteryDelay == null || (settings?.batteryDelay as int) == 0 || timeDiff > (settings?.batteryDelay as int)) {
// send it now!
sendDelayedBatteryPercentageEvent(map)
}
else {
def delayedTime = (settings?.batteryDelay as int) - timeDiff
map.delayed = delayedTime
map.descriptionText += " [delayed ${map.delayed} seconds]"
logDebug "this battery event (${map.value}%) will be delayed ${delayedTime} seconds"
runIn(delayedTime, 'sendDelayedBatteryEvent', [overwrite: true, data: map])
}
}
private void sendDelayedBatteryPercentageEvent(Map map) {
logInfo "${map.descriptionText}"
//map.each {log.trace "$it"}
sendEvent(map)
}
Sorry to be pendantic but do you mean
- "Yes, it will overwrite anything with the same method name"
- "Yes, it will respect the differences in parameters passed"
I think the answer is (1), but I have not tested scheduling multiple tasks using one and the same method, but different parameters.
I either use [overwrite: true] for re-scheduling the execution of one and the same function (different parameters every time) , or [overwrite: false] for scheduling multiple executions of one and the same function at different time.
Overwrites any scheduled job with same method name regardless of parameters passed. Again from my experience it looks at any scheduled job linked to app with that method name and replaces it. Without it duplicates are allowed.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.