Subscribe to a temperature threshold?

How can I Subscribe to a temperature value? I understand the canned capability's like switch but not a moving variable.

void initialize() {
subscribe(temperatureMeasurement, "If temperature is > 80", tempHandler)
}

void tempHandler(evt) {
Do stuff
}

You can't... tempHandler (in your example) does the compare.

Recognize that the device itself has no clue about your desire to test the value.. it will send it anyway and the hub will receive it and turn it into an Event. So you're not saving anything with your concept.

Yeah, maybe I shouldn't have asked it like that. More like this. I can't get the tempHandler to fire.

void initialize() {
subscribe(temperatureMeasurement, "temperature", tempHandler)
}

void tempHandler(evt) {
if (temperature > 80) {Do stuff ..........}
}

Are you calling initialize() anywhere? That's not a standard app method (common as it may be) and will only be called if you call it from somewhere. Put a log.debug or something in there to see for sure. Even better, use that to print the value of temperatureMeasurement to make sure that's actually set like you think it is, too.

2 Likes

I like @bertabcd1234 answer.. it's quite common to have an initialize method, used something along these lines:

def updated() {
	unschedule()
	unsubscribe()
	initialize()
	if (debugOutput) runIn(1800,logsOff)
	...
}
1 Like

I have it just like that. The subscribes are in my initialize function. log.debug shows its running through initialize loop just fine at app start and hitting done. I think the issue is in:
subscribe(temperatureMeasurement, "temperature", tempHandler)

specifically the "temperature" part. Whats supposed to makes it go to the handler since its not a State, just a value change?

Devices can have multiple capabilities and the 2nd parameter of the Subscribe method is the capability attribute within the device.

subscribe(temperatureMeasurement, "temperatureMeasurementtemperature", tempHandler)

This should be the attribute (event name), where "temperature" is correct. For the OP, maybe working this down to a minimal example that demonstrates the problem will help. Perhaps someone can find it for you after doing that; or often, people will find it themselves when they take other complications out of the picture. :slight_smile:

1 Like

I did that and figure out my issue.. Thanks.

1 Like