Skip to content

Instantly share code, notes, and snippets.

@schafeld
Created March 17, 2017 13:46
Show Gist options
  • Save schafeld/66de0aba6187456ee908bde570bc5a9a to your computer and use it in GitHub Desktop.
Save schafeld/66de0aba6187456ee908bde570bc5a9a to your computer and use it in GitHub Desktop.
Node Red scriptlet for requesting weather information from OpenWeatherMap API.
[{"id":"78ee8651.692618","type":"inject","z":"469bc24d.c9b79c","name":"Request Weather Data","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":169.5,"y":94,"wires":[["f0f273bd.ed46e"]]},{"id":"f0f273bd.ed46e","type":"http request","z":"469bc24d.c9b79c","name":"OpenWeather API","method":"GET","ret":"txt","url":"http://api.openweathermap.org/data/2.5/weather?q=Cologne,DE&units=metric&APPID=000_YOUR_API_KEY_GOES_HERE_000","tls":"","x":261.5,"y":213,"wires":[["294c54f6.e5b17c","9ec7b5a5.4504b8"]]},{"id":"294c54f6.e5b17c","type":"function","z":"469bc24d.c9b79c","name":"Output Filter: Temperature in Celsius","func":"// Parse OpenWeatherMap JSON answer\nvar weather=JSON.parse(msg.payload);\n \n// Create string with embedded data\nvar newMsg = { payload: \"Current temperature in Cologne is \"+weather.main.temp + \"° Celcius.\" };\n \n// Return formatted message string\nreturn newMsg;\n","outputs":1,"noerr":0,"x":480.5,"y":297,"wires":[["650f3bae.4ea624"]]},{"id":"650f3bae.4ea624","type":"debug","z":"469bc24d.c9b79c","name":"","active":true,"console":"false","complete":"false","x":678.5,"y":406,"wires":[]},{"id":"e0946cc6.c1a7f","type":"comment","z":"469bc24d.c9b79c","name":"Enter your API key here.","info":"You need to apply for a (free) OpenWeather \nAPI key and enter it here. Visit: \nhttp://openweathermap.org/appid\n","x":282.5,"y":175,"wires":[]},{"id":"9ec7b5a5.4504b8","type":"function","z":"469bc24d.c9b79c","name":"Output Filter: Sunset Time","func":"// Parse OpenWeatherMap JSON answer\nvar weather=JSON.parse(msg.payload);\n\n// Create new JavaScript Date object from timestamp\n// multiply by 1000 so argument is milliseconds, not seconds.\nvar date = new Date(weather.sys.sunset * 1000);\nvar timeZoneOffset = -1 * date.getTimezoneOffset()/60;\nvar hours = date.getHours();\n// add leading 0 to handle single digits\nvar minutes = \"0\" + date.getMinutes();\nvar seconds = \"0\" + date.getSeconds();\n\n// Format time like this: 12:45:23 \n// Use last 2 digits, so leading zero gets cut off for double digit numbers.\nvar formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);\nvar formattedLocalTime = (hours + timeZoneOffset) + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);\n\n// Create string with embedded data\nvar newMsg = { payload: \"Sunset at: \"+ formattedTime + \" (UTC). (Which seems to be \"+ formattedLocalTime +\" local time for you.)\" };\n \n// Return formatted message string\nreturn newMsg;","outputs":1,"noerr":0,"x":443.5,"y":342,"wires":[["650f3bae.4ea624"]]}]
@Shirola
Copy link

Shirola commented Feb 18, 2018

Great post mate.
and this is how to get the icon info, as it took me a while to figure that out:
var newMsg = {payload:weather.weather[0].icon};

@chuckf201
Copy link

Great post. Finally figured out payload read.
Thx

@chuckf201
Copy link

I'm trying to reduce the container so it can sit with other object. When I do the compass becomes distorted.
Reducing the container skews the pointer, 'N' and arrow.
Can you help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment