Function to report temp below zero, but suppress until temp rises above zero

Hi Guys,

I've written a function to email me when a temperature value is below zero.

The problem is that I want to suppress further emails, until the temperature rise above zero.

Can anyone point me in the right direction here?

Here's the function
if (msg.payload.Temperature <= 32) {
msg.topic = "Alert: Aspen temp below zero !!"
return msg;
}
return null;

Thanks!!

You can use a filter node, set to block unless msg.topic changes.
Your function would have to send a message rather than null when temperature > 32.
That would result in an alert when the temperature dips and another when it rises again.
To prevent the rising notification use a switch node after the filter.

Edit - changed "filter" to "function" in line 2

Hi Jbudd,

Thanks, I will try that now.
:slight_smile:

This is such a basic part of system control that you probably should code it up yourself just for the experience. On the other hand, you will learn a lot by looking at the descriptions of contributed nodes that do the job. Try searching the library for "hysteresis."

I don't like to use function nodes at all. But here they may be useful - as you can use a context of the node - means you can store within the function node, if an alert has been already sent. In other words - with a context you can store a state which can be retrieved each time a message is received.

However you must define a clear if - else block. Here I modified the code of your function node to remember if you already have been alerted. Nevertheless you should probably use a hysteresis as recommended by @drmibell. Otherwise you get severeal messages when temperature changes around the threshold.

Here the code for using context within a function node:

var alerted = context.get('alerted') || false;

if (msg.payload.Temperature <= 32) {
msg.topic = "Alert: Aspen temp below zero !!"

context.set('alerted',true);
if (!(alerted)) return msg;
} else
{
context.set('alerted',false);
return null;
}