Forecast.io rain prediction
Periodically calls the forecast.io api with a lat / long location and publishes the predicted number of minutes until the next rainfall to an mqtt topic.
By default the flow will publish a value -1 if no rain is predicted in the next 45 minutes.
The flow also includes a node to generate RGB values to send to ambient light.
A forecast.io api-key is required within the HTTP request node can can be obtained by registering with the forecast.io website: https://developer.forecast.io/
1000 calls can be made to the forecast.io api a day free of charge.
For further information see:
[{"id":"ba386057.845d3","type":"mqtt-broker","broker":"localhost","port":"1883"},{"id":"e11c896.4489af8","type":"inject","name":"Location","topic":"Home","payload":"51.0246,-1.3906","repeat":"180","crontab":"","once":true,"x":117,"y":129,"z":"9e6e960.b4b3868","wires":[["b5925784.0cee7"]]},{"id":"b5925784.0cee7","type":"httpget","name":"get forecast.io","baseurl":"https://api.forecast.io/forecast/<api-key-goes-here>/","append":"?units=uk","x":278.9999237060547,"y":130.0000820159912,"z":"9e6e960.b4b3868","wires":[["8e8b62ba.26af5","20de4ee4.d7dec2"]]},{"id":"8e8b62ba.26af5","type":"function","name":"make Weather object","func":"//parse forecast.io message\n\nvar weather = JSON.parse(msg.payload); \n\nvar data = weather.minutely.data;\nvar timeToRain = -1;\n\nvar i=1;\nfor (var i=0 ; i< data.length;i++) {\n\tif (data[i].precipProbability > 0.25) { weather.nextRain = parseInt(data[i].time); break; }\n}\n\n//reduce size of object\ndelete weather.minutely;\ndelete weather.hourly;\ndelete weather.daily;\ndelete weather.flags;\n\nif (weather.nextRain != null) {\n\tvar t =( weather.nextRain - parseInt(weather.currently.time) );\n\nif (t<0){t=0}\nelse{t = t/60;}\ntimeToRain = t;\n}\nvar msg2 = { payload:timeToRain};\nreturn msg2;","outputs":"1","x":493.99993896484375,"y":187,"z":"9e6e960.b4b3868","wires":[["a1dcd736.a98778","d205f172.861468"]]},{"id":"d205f172.861468","type":"debug","name":"","complete":true,"x":753.8333129882812,"y":121.5,"z":"9e6e960.b4b3868","wires":[]},{"id":"a1dcd736.a98778","type":"mqtt out","name":"Minutes to Rain","topic":"weather/forecast/minsToRain","broker":"ba386057.845d3","x":742,"y":182,"z":"9e6e960.b4b3868","wires":[]},{"id":"3ff7f65f.e9f652","type":"mqtt in","name":"Minutes To Rain","topic":"weather/forecast/minsToRain","broker":"ba386057.845d3","x":129,"y":286,"z":"9e6e960.b4b3868","wires":[["512f2f52.a0bc2"]]},{"id":"512f2f52.a0bc2","type":"function","name":"Is weather unsafe?","func":"// console.log(msg.topic, msg.payload, msg.qos, msg.retain);\n// context = {};\nvar unsafe=0;\nvar minsToRain = parseInt(msg.payload);\nif ((minsToRain >=0) && (minsToRain < 46) )\n{\n unsafe=1;\n}\nmsg.payload=unsafe;\nreturn msg;","outputs":"1","x":376,"y":286,"z":"9e6e960.b4b3868","wires":[["1c87d0b7.166f57"]]},{"id":"1c87d0b7.166f57","type":"mqtt out","name":"Weather unsafe","topic":"weather/unsafe","broker":"ba386057.845d3","x":634,"y":287,"z":"9e6e960.b4b3868","wires":[]},{"id":"6f68971b.7ee3","type":"function","name":"Rain Warning Colour","func":"// The received message is stored in 'msg'\n// It will have at least a 'payload' property:\n// console.log(msg.payload);\n// The 'context' object is available to store state\n// between invocations of the function\n// context = {};\n\n//if weather status switched off end flow now\nif(context.global.weatherStatus == \"0\")\n{\n return null;\n}\nelse\n{\n\tvar red =0;\n\tvar blue =0;\n\tvar green =0;\n\t\n\tvar minsToRain = msg.payload;\n\t\n\tif (minsToRain == -1 || minsToRain > 45)\n\t{\n\t\tgreen=255;\n\t}\n\telse if (minsToRain == 0)\n\t{\n\t red =255;\n\t}\n\telse\n\t{\n\t\t\tblue = (255 - (255 - (minsToRain*5)));\n\t red = (255 - (minsToRain*5));\n\t green =0;\n\t}\n\t\n\tmsg.payload = red + \",\" + green + \",\" + blue;\n}\nreturn msg;","outputs":1,"x":408.25,"y":441.75,"z":"9e6e960.b4b3868","wires":[["1283c550.3936cb","58302292.89a534"]]},{"id":"1283c550.3936cb","type":"debug","name":"rgb out","active":true,"complete":"false","x":688.25,"y":489.75,"z":"9e6e960.b4b3868","wires":[]},{"id":"1f2cc787.f7de18","type":"mqtt in","name":"Minutes To Rain","topic":"weather/forecast/minsToRain","broker":"ba386057.845d3","x":132.25,"y":443.75,"z":"9e6e960.b4b3868","wires":[["6f68971b.7ee3"]]},{"id":"58302292.89a534","type":"mqtt out","name":"Ambient Light Lounge 1","topic":"home/ambient/lounge/1","broker":"ba386057.845d3","x":741,"y":399,"z":"9e6e960.b4b3868","wires":[]},{"id":"20de4ee4.d7dec2","type":"debug","name":"","active":true,"complete":"false","x":464,"y":126,"z":"9e6e960.b4b3868","wires":[]},{"id":"76227f9c.3c1bc","type":"comment","name":"Rain Forecaster","info":"","x":107,"y":82,"z":"9e6e960.b4b3868","wires":[]}]