# Node red + telegram

**URL:** <https://discourse.nodered.org/t/node-red-telegram/13966>\
**Category:** General\
**Created:** [3 August 2019 14:31 UTC](https://discourse.nodered.org/t/node-red-telegram/13966 "2019-08-03T14:31:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fafidio](https://avatars.discourse-cdn.com/v4/letter/f/839c29/32.png) [@fafidio](https://discourse.nodered.org/u/fafidio)\
**Post date:** [3 August 2019 14:31 UTC](https://discourse.nodered.org/t/node-red-telegram/13966/1 "2019-08-03T14:31:59Z")

</div>

hello, i'm Fabio and would use node red for monitoring more sensors. I 'm not very clever with code so I ask your help.  
I've created a function to send values from sensor to Telegram : but my target is to have the notify only for one time when the sensor reach the limit.  
This is my function:

```auto
if((msg.payload>20) && (var1==1)){
 var1=0;
 var alert = "up level ";
 msg.payload = {chatId :xxxxxxx, type : 'message', content : alert}

}

else
{
var1=1;
msg.payload =0;
}
return msg;

```

But with this function I have a lot of notify, when message is \>20.  
Could someone help me?  
Where is the mistake?  
ThanksFabio

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 August 2019 14:55 UTC](https://discourse.nodered.org/t/node-red-telegram/13966/2 "2019-08-03T14:55:22Z")

</div>

Your function is triggered every time it gets a message.  
So where do you set var1?  
The function has no memory of previous messages unless you use a context value (see docs on functions)  
Try also putting node.warn lines in your code (again see docs) to allow you to see in the debug what is happening within your code

---

<div class="post-metadata">

**Author:** ![fafidio](https://avatars.discourse-cdn.com/v4/letter/f/839c29/32.png) [@fafidio](https://discourse.nodered.org/u/fafidio)\
**Post date:** [3 August 2019 15:02 UTC](https://discourse.nodered.org/t/node-red-telegram/13966/3 "2019-08-03T15:02:37Z")

</div>

thanks ukmoose.  
Have you an example to set a context value?  
I thought the js was the same of C++.  
thanks Fabio

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [3 August 2019 15:32 UTC](https://discourse.nodered.org/t/node-red-telegram/13966/4 "2019-08-03T15:32:05Z")

</div>

Follow the link from the top of the page to the documentation there’s a whole section that covers using the function node

---

<div class="post-metadata">

**Author:** ![fafidio](https://avatars.discourse-cdn.com/v4/letter/f/839c29/32.png) [@fafidio](https://discourse.nodered.org/u/fafidio)\
**Post date:** [3 August 2019 19:11 UTC](https://discourse.nodered.org/t/node-red-telegram/13966/5 "2019-08-03T19:11:46Z")

</div>

```auto
var sogliaMax = 40;
var sogliaMin = 20;

var previousDist = context.previousDist || 
        (sogliaMax - sogliaMin)/2;        
var dist = msg.payload;

context.previousDist = dist;

if (previousDist != dist){
	if ((previousDist < sogliaMax) && (dist >= sogliaMax)){
	var alert = "Riserva Cisterna olio ";
 msg.payload = {chatId :xxxxxxxxxxxxx, type : 'message', content : alert}
		return msg;
	}
	
	else if ((previousDist > sogliaMin) && (dist <= sogliaMin)){
		var alert1 = " Livello Ciasterna olio ok ";
 msg.payload = {chatId :xxxxxxxxxxxxx, type : 'message', content : alert1}
		return msg;
	}
}

```

Solved thanks
