This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weather change alert with Node-red
#5
Since I've found some time here is a node-red flow in which mqtt data gets logged to a file. Besides that the file can be analysed by the getData node. There are three variables that can be set by you.

min_recording_time = 30 means: You need at least 30 min of pressure data

max_recording_time  = 180 is the maximum amount of minutes that are analysed

trend = 60 prognoses for the upcoming 60 minutes.

If you provide valid data the node returns: the difference in pressure (oldest data point vs last logged data). the linear regression (intercept, slope, etc), the difference in calculated pressure (oldest data point vs current data both calculated by the linear regression) and a trend

My code is quick and dirty!!! You can ask me (specific) questions about it.
Code:
[{"id":"a4c1f393.f0b03","type":"file in","z":"b1242926.a388f","name":"test","filename":"c:\\test.csv","format":"utf8","x":243,"y":89,"wires":[["49b3fd20.c5009c"]]},{"id":"aaefae93.fa165","type":"inject","z":"b1242926.a388f","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"x":110,"y":90,"wires":[["a4c1f393.f0b03"]]},{"id":"cdc04bd8.f5b898","type":"debug","z":"b1242926.a388f","name":"","active":true,"console":"false","complete":"true","x":611,"y":88,"wires":[]},{"id":"49b3fd20.c5009c","type":"function","z":"b1242926.a388f","name":"getData","func":"// in minutes\nvar min_recording_time  = 30;\nvar max_recording_time  = 180;\nvar trend               = 60;\n//--------------------\n\nvar pressure_dif, time_dif;\nvar timestamp = Date.now();\nvar pressure = [];\nvar minutes = [];\n\nvar data = msg.payload.split(\"\\n\");\ndata = data.filter(function(e){return e});\ndata.reverse();\n\ndelete msg.payload;\ndelete msg.topic;\n\n\nfor(var i=0;i<data.length;i++){\n    var arr = data[i].split(\";\");\n    arr[0] = Number(arr[0]);\n    arr[2] = parseFloat(arr[2]);\n    var x = timestamp - arr[0];\n    if(x > (max_recording_time*60000)){\n        break;\n    }\n    pressure.push(arr[2]);\n    minutes.push(arr[0]);\n    minutes[i]=(minutes[i]-timestamp)/60000;\n}\nif((minutes.slice(-1)[0]*-1) < min_recording_time || minutes.slice(-1)[0] === undefined){\n    msg.error = \"Error: No valid data available! Please wait for \"+ (min_recording_time + minutes.slice(-1)[0]).toFixed()+\" min.\";\n    return msg;\n}\nvar x = minutes;\nvar y = pressure;\nvar lr = {};\nvar n = y.length;\nvar sum_x = 0;\nvar sum_y = 0;\nvar sum_xy = 0;\nvar sum_xx = 0;\nvar sum_yy = 0;\nfor (var i = 0; i < y.length; i++) {\n\tsum_x += x[i];\n\tsum_y += y[i];\n\tsum_xy += (x[i]*y[i]);\n\tsum_xx += (x[i]*x[i]);\n\tsum_yy += (y[i]*y[i]);\n} \nlr['slope'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x);\nlr['intercept'] = (sum_y - lr.slope * sum_x)/n;\nlr['r2'] = Math.pow((n*sum_xy - sum_x*sum_y)/Math.sqrt((n*sum_xx-sum_x*sum_x)*(n*sum_yy-sum_y*sum_y)),2);\nlr['diff'] = lr.intercept - ((lr.slope*minutes.slice(-1)[0])+lr.intercept);\nlr['diff_complete'] = lr['diff'].toFixed(2) + \" hPa / \"+(minutes.slice(-1)[0]*-1).toFixed()+\" min\";\nlr['trend'] = (((lr.slope*trend)+lr.intercept)-lr.intercept).toFixed(2);\npressure_dif = ((pressure[0]*100)-(pressure.slice(-1)[0]*100))/100;\ntime_dif = (minutes[0]-minutes.slice(-1)[0]);\nmsg.pressure_dif = pressure_dif;\nmsg.time_dif = time_dif.toFixed();\nmsg.lr = lr;\n\nreturn msg;","outputs":1,"noerr":0,"x":417,"y":89,"wires":[["cdc04bd8.f5b898"]]},{"id":"325bdac0.8422ce","type":"mqtt in","z":"b1242926.a388f","name":"","topic":"/#","qos":"2","broker":"ac0d0706.7df12","x":109,"y":204,"wires":[["f7808585.15c148"]]},{"id":"39fd5d25.3a174a","type":"file","z":"b1242926.a388f","name":"","filename":"c:\\test.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","x":495,"y":204,"wires":[]},{"id":"f7808585.15c148","type":"function","z":"b1242926.a388f","name":"MQTTtoFile","func":"if(msg.topic == \"/d1mini/bme280/Pressure\"){\n    msg.payload = Date.now()+\";\"+msg.topic+\";\"+msg.payload;\n    return msg;\n}","outputs":1,"noerr":0,"x":294,"y":203,"wires":[["39fd5d25.3a174a"]]},{"id":"ac0d0706.7df12","type":"mqtt-broker","z":"","broker":"192.168.178.60","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]
Reply


Messages In This Thread
RE: Weather change alert with Node-red - by shark24 - 2017-06-13, 11:52 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)