# Program smart alarms

Good night people! I am doing the automation of my chicken farm with node red .. I am reading temperature inside the farm .. In the first week, the temperature has to be between 35 and 30 degrees, in the second, from 30 to 25 degrees and so on until the seventh week .. How do I create alarms to warn if the temperature changes according to the weeks? Without changing the setpoint every time ... Thanks if someone helps me!

Each set point is fixed with each week accordingly. So the program is a cycle which resets after every 7 weeks. You can use node red function node and these date formats.

``````var d = new Date();
var t = d.getTime();
var year = d.getFullYear();
var month = d.getMonth()+1;
if(month.toString().length == 1) {
month = '0'+month;
}
var day = d.getDate();
var hour  = d.getHours();
msg.date = t;
``````

Hope it may help a bit.

I understood .. I just can't see to apply to my problem, how can I make the set points according to the temperature?

Perhaps this can give you some ideas.

You will need to keep track/have a schedule of the week number
Once you have that, in a function node you could use something like:

``````i = msg.payload.temp // input temperature
w = msg.payload.week // input week number

t = {}
t.week1 = [30,35] // low temp,high temp
t.week2 = [25,30]
t.week3 = [28,32]
...

low = t['week'+w][0]
high = t['week'+w][1]

if(i >= low && i <= high){
msg.payload = "OK - temperature:" + i
}
else{
msg.payload = "ALARM - outside temperature range:" + i
}

return msg
``````

where you define the temperatures for each week and inject the temperature and the weeknumber.

`{payload:{week:1,temp:28}}`

Example flow:

``````[{"id":"d44d5f3c.78179","type":"function","z":"6028390e.f2d3b","name":"","func":"i = msg.payload.temp // input temperature\nw = msg.payload.week // input week number\n\nt = {}\nt.week1 = [30,35] // low temp,high temp\nt.week2 = [25,30]\nt.week3 = [28,32]\n\nlow = t['week'+w][0] // get array from object\nhigh = t['week'+w][1]\n\nif(i >=low && i<=high){\n  msg.payload = \"OK - temperature:\" + i\n}\nelse{\n  msg.payload = \"ALARM - outside temperature range:\" + i  \n}\n\nreturn msg","outputs":1,"noerr":0,"x":375,"y":408,"wires":[["f4ac1536.8ca69"]],"l":false},{"id":"da8a387b.17c8b8","type":"inject","z":"6028390e.f2d3b","name":"","topic":"","payload":"{\"week\":1,\"temp\":35}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":148,"y":288,"wires":[["d44d5f3c.78179"]]},{"id":"f4ac1536.8ca69","type":"debug","z":"6028390e.f2d3b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":447,"y":408,"wires":[],"l":false},{"id":"4ce450d3.1af99","type":"inject","z":"6028390e.f2d3b","name":"","topic":"","payload":"{\"week\":1,\"temp\":28}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":148,"y":336,"wires":[["d44d5f3c.78179"]]},{"id":"2e4ba70b.a34a08","type":"inject","z":"6028390e.f2d3b","name":"","topic":"","payload":"{\"week\":2,\"temp\":24}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":148,"y":432,"wires":[["d44d5f3c.78179"]]},{"id":"ad9b020a.ff2e08","type":"inject","z":"6028390e.f2d3b","name":"","topic":"","payload":"{\"week\":2,\"temp\":29}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":148,"y":480,"wires":[["d44d5f3c.78179"]]}]
``````

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