Event Processing Priority

@patrick
Does the team have any plans on customizing the priority in which events are received by subscribers.

If there are multiple apps subscribed to an event and some of them require more intense processing than others, I’m having latency issues with some of the more time sensitive ones.

I have a motion sensor that does multiple things and has different apps subscribed to it to accomplish those things

  1. It’s first priority is to turn on the lights when mode is home as soon as possible when motion is detected. 1st app
  2. At night it should turn on a nightlight in another room ( less priority) 2nd app
  3. In the morning it should activate a good morning routine when motion is detected. 3rd app

Items 2 and 3 require more processing than 1 (and have more logic than only what was giving in the title above) . If only item 1 is subscribed, I get a latency of only .036 seconds which is great.

If all apps are subscribed and items 2 and 3 get the event, it sometimes takes 1.5-2 seconds for item 1 to get the event depending on the order of the subscriptions.

I know the developer of the Otherhub event pusher noticed this as well and uses a runIn function to push priority down so that others can look at the event first but it wasn’t practical for me to modify the code in different places for different apps.

What are your thoughts?

Is it possible to update it to use some type of threading to push out the event to multiple listeners at the same time to reduce this latency or to give certain subscriptions higher priority than others? I’ve posted a simple example below to monitor the event delay for my testing.

definition(
    name: "Custom Lighting",
    namespace: "Custom",
    author: "Me",
    description: "Custom Lighting",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")


preferences {
	section("Title") {
		// TODO: put inputs here
        input "motion", "capability.motionSensor", title: "Motion Sensor", multiple: false
        input "lights", "capability.switch", title: "Lights", multiple: true
        input "restriction", "capability.switch", title: "Restriction", multiple: false
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"

	initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"

	unsubscribe()
	initialize()
}

def initialize() {
    if(motion){
     	subscribe(motion, "motion", "motionHandler")   
    }
}

def motionHandler(evt){
    log.debug "Evt delay ${ (now() - evt.date.time)/1000} seconds"
    log.debug "Motion event: ${evt.value}"
    if(evt.value == "active"){
        log.debug "${restriction.currentValue("switch")}"
        if(restriction?.currentValue("switch") == "off"){
         	return
        }
        else {
        	lights?.each { it.on() }
        }
    }
    else {
        lights?.each { it.off() }   
    }
}
4 Likes