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:
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
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
thanks ukmoose.
Have you an example to set a context value?
I thought the js was the same of C++.
thanks Fabio
Follow the link from the top of the page to the documentation there’s a whole section that covers using the function node
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