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

**URL:** https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416
**Category:** General
**Created:** [19 December 2021 01:41 UTC](https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416 "2021-12-19T01:41:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cloudgenki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cloudgenki/32/52840_2.png) [@cloudgenki](https://discourse.nodered.org/u/cloudgenki)
#### Post date: [19 December 2021 01:41 UTC](https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416/1 "2021-12-19T01:41:53Z")

</div>

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!!

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [19 December 2021 01:49 UTC](https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416/2 "2021-12-19T01:49:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cloudgenki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cloudgenki/32/52840_2.png) [@cloudgenki](https://discourse.nodered.org/u/cloudgenki)
#### Post date: [19 December 2021 01:50 UTC](https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416/3 "2021-12-19T01:50:39Z")

</div>

Hi Jbudd,

Thanks, I will try that now.  
🙂

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [19 December 2021 02:34 UTC](https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416/4 "2021-12-19T02:34:13Z")

</div>

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."

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [19 December 2021 22:35 UTC](https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416/5 "2021-12-19T22:35:02Z")

</div>

> [@cloudgenki](#):
>
> if (msg.payload.Temperature \<= 32) {  
> msg.topic = "Alert: Aspen temp below zero !!"  
> return msg;  
> }  
> return null;

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:

```auto
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;
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [17 February 2022 22:35 UTC](https://discourse.nodered.org/t/function-to-report-temp-below-zero-but-suppress-until-temp-rises-above-zero/55416/6 "2022-02-17T22:35:19Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
