Created
March 31, 2018 21:22
-
-
Save andresbott/46d431ed5e4a977f5a30c5c380f644b5 to your computer and use it in GitHub Desktop.
Node Red Stock market warning, send a email when a stock value goes bellow a configured value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"b459b352.e35c5","type":"inject","z":"fdb65e1e.45f03","name":"","topic":"","payload":"","payloadType":"date","repeat":"7200","crontab":"","once":false,"onceDelay":"","x":175,"y":76,"wires":[["b38a40f7.6890f"]]},{"id":"31944a31.4db1e6","type":"http request","z":"fdb65e1e.45f03","name":"Get current price from iextrading.com","method":"GET","ret":"txt","url":"https://ws-api.iextrading.com/1.0/stock/{{{stock_item}}}/price","tls":"","x":480.18182373046875,"y":118,"wires":[["fc60b702.6ddaf8"]]},{"id":"b296ce5e.4edd8","type":"debug","z":"fdb65e1e.45f03","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":997,"y":78,"wires":[]},{"id":"fc60b702.6ddaf8","type":"function","z":"fdb65e1e.45f03","name":"Compare Stock Value","func":"// This function compares the payload with warn_level\nmsg.warn_level = msg.warn_level || 0.0;\n\nvar mailTopic = \"[WARN] \"+msg.stock_item+\" Stock value\";\nvar mailFrom = \"[email protected]\";\n\nfunction addZero(i) {\n if (i < 10) {\n i = \"0\" + i;\n }\n return i;\n}\n\nfunction timeConverter(UNIX_timestamp){\n var a = new Date(UNIX_timestamp );\n var year = a.getFullYear();\n var month = addZero(a.getMonth());\n var date = addZero(a.getDate());\n var hour = addZero(a.getHours()); \n var min = addZero(a.getMinutes());\n var sec = addZero(a.getSeconds());\n var time = date + '/' + month + '/' + year + ' ' + hour + ':' + min + ':' + sec ;\n return time;\n}\n\n\nif (msg.payload <= msg.warn_level){\n var rstring = \"Warning: The Stock value of: \"+msg.stock_item+\"\\n\"+\n \"is now bellow: \"+msg.warn_level+\"\\n\"+\n \"current value: \"+ msg.payload+\"\\n\"+\n \"Date:\"+timeConverter(msg.check_date);\n return {payload:rstring,topic:mailTopic,from:mailFrom,date:msg.check_date}\n}else{\n return false;\n}\n","outputs":1,"noerr":0,"x":767,"y":117,"wires":[["b296ce5e.4edd8","ecae85f2.e05768"]]},{"id":"b38a40f7.6890f","type":"change","z":"fdb65e1e.45f03","name":"Configure Stock data","rules":[{"t":"set","p":"stock_item","pt":"msg","to":"ADBE","tot":"str"},{"t":"set","p":"warn_level","pt":"msg","to":"210","tot":"str"},{"t":"set","p":"check_date","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":201,"y":120,"wires":[["31944a31.4db1e6"]]},{"id":"ecae85f2.e05768","type":"e-mail","z":"fdb65e1e.45f03","server":"","port":"587","secure":false,"name":"","dname":"Mail ","x":1007,"y":167,"wires":[]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment