Time (sunset and sunrise) calculations

enter image description here You could use bigtimer node to play with time, or you can use JS time methods and open source API to calculate your own based on coordinates!

Complete instructions

Features:

  • tell you if it's night or day
  • tell you the sunrise and sunset time
  • tell you the day length
  • provide you with astronomical, nautical, civil dusk and dawn times
  • use these values to create timers
  • all that good stuff in your local time!

Settings

Configure Set Coords Subflow with your Lat and Long and the time variables will be available as globals.

More about me:

If you want to get the latest updates to this project you can follow me via your preferred social media:

And if you feeling like buying me a coffee or supporting me in a more continuous way:

I hope you have enjoyed the project!

[{"id":"3c1a9880.5362b8","type":"subflow","name":"Set Coords","info":"","category":"","in":[{"x":540,"y":100,"wires":[{"id":"e4984393.2e0f"}]}],"out":[{"x":840,"y":100,"wires":[{"id":"e4984393.2e0f","port":0}]}],"env":[],"color":"#DDAA99"},{"id":"e4984393.2e0f","type":"function","z":"3c1a9880.5362b8","name":"set coords","func":"var lat = env.get(\"Latitude\"); \nvar long = env.get(\"Longitude\");\n\n\nflow.set(\"$parent.Latitude\", lat);    \nflow.set(\"$parent.Longitude\", long);\n\n\nmsg.payload = \"Your postition has been saved to: Latitude: \" + lat + \", Longitude: \" + long;\n\nreturn msg;\n\n\n","outputs":1,"noerr":0,"x":670,"y":100,"wires":[[]]},{"id":"b4f27448.856258","type":"tab","label":"Time (sunset and sunrise) calculations","disabled":false,"info":"![enter image description here](https://notenoughtech.com/wp-content/uploads/2019/02/YouTube_NEW1-2.jpg)\r\nYou could use bigtimer node to play with time, or you can use JS time methods and open source API to calculate your own based on coordinates! \r\n\r\n  [Complete instructions](https://notenoughtech.com/home-automation/nodered-home-automation/nodered-sun-and-time/)\r\n\r\n\r\n**Features**:\r\n-   tell you if it's **night** or **day**\r\n-   tell you the **sunrise** and **sunset time**\r\n-   tell you the **day length**\r\n-   provide you with **astronomical, nautical, civil dusk and dawn times**\r\n-   use these values to create timers\r\n-   **all that good stuff in your local time!**\r\n\r\n## Settings\r\nConfigure **Set Coords** Subflow with your Lat and Long and the time variables will be available as globals.\r\n\r\n## More about me:\r\n\r\nIf you want to get the latest updates to this project you can follow me via your preferred social media:\r\n\r\n-   [Facebook](https://www.facebook.com/NotEnoughTECH/)\r\n-   [Twitter](https://twitter.com/NotEnoughTECH)\r\n-   [Instagram](https://www.instagram.com/notenoughtech/)\r\n-   [YouTube](https://www.youtube.com/user/Polepositionpage)\r\n\r\nAnd if you feeling like buying me a coffee or supporting me in a more continuous way:\r\n\r\n-   [Paypal](https://www.paypal.me/notenoughtech)\r\n-   [Patreon](https://www.patreon.com/NotEnoughTECH)\r\n\r\nI hope you have enjoyed the project!"},{"id":"47d5e6ec.2151b8","type":"comment","z":"b4f27448.856258","name":"Get Sun Info (the easy way)","info":"","x":220,"y":120,"wires":[]},{"id":"68046019.da2bd","type":"function","z":"b4f27448.856258","name":"Is it night time yet? ","func":"var time = new Date();\nvar timeNow = time.getTime();\n\nvar sunsetToday  = global.get('Time_sunset');\nvar sunriseToday = global.get('Time_sunrise');\n\n\nif(timeNow < sunsetToday && timeNow > sunriseToday){\n    msg.payload = \"it's daytime\"\n    var x = \"day\";\n    global.set(\"TimeOfDay\", x)\n    \n}\n\nif(timeNow > sunriseToday && timeNow < sunsetToday){\n    msg.payload = \"it's night\";\n    x = \"night\";\n    global.set(\"TimeOfDay\", x)\n    \n}\n\nif(timeNow > sunsetToday){\n    msg.payload = \"it's day\"\n    x = \"day\";\n    global.set(\"TimeOfDay\", x)\n}\nif(timeNow < sunriseToday){\n    msg.payload = \"it's night\"\n    x = \"night\";\n    global.set(\"TimeOfDay\", x)\n}\n\n\nmsg.times = {\n    \"sunset\": sunsetToday,\n    \"sunrise\": sunriseToday,\n    \"time\": timeNow\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":395,"y":335,"wires":[["f273179e.3b8c88"]]},{"id":"556196f6.0aae48","type":"inject","z":"b4f27448.856258","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":335,"wires":[["68046019.da2bd"]]},{"id":"f273179e.3b8c88","type":"debug","z":"b4f27448.856258","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":580,"y":335,"wires":[]},{"id":"76b7ffa3.d3a2f","type":"function","z":"b4f27448.856258","name":"Save Recalculated times (the easy way)","func":"let date = new Date()\ntimeZoneOffset = date.getTimezoneOffset() *60;\n\nfunction recalculateTime(x){\n    var z = new Date(x).valueOf();\n    var adjustedTime = (z/1000 + (-1* timeZoneOffset))*1000;\n    return new Date(adjustedTime);\n}\n\nfunction secondsToHms(d) {\n    d = Number(d);\n\n    var h = Math.floor(d / 3600);\n    var m = Math.floor(d % 3600 / 60);\n\n    return ('0' + h).slice(-2) + \"h \" + ('0' + m).slice(-2)+\"min\";\n}\n\nfunction stringtodate(timenow){\n    let date1 = Date.parse(timenow);\n    return date1;\n    \n}\n\n\n\n//sunrise\nvar sunrise = recalculateTime(msg.payload.results.sunrise);\nglobal.set('Time_sunrise', stringtodate(sunrise));\n\n\n//sunset\nvar sunset = recalculateTime(msg.payload.results.sunset);\nglobal.set('Time_sunset', stringtodate(sunset));\n\n//solar_noon\nvar solar_noon = recalculateTime(msg.payload.results.solar_noon);\nglobal.set('Time_solar_noon', solar_noon);\n\n//day_length\nvar day_length =  secondsToHms(msg.payload.results.day_length);\nglobal.set('Time_day_length', day_length);\n\n//civil_twilight_begin\nvar civil_twilight_begin = recalculateTime(msg.payload.results.civil_twilight_begin);\nglobal.set('Time_civil_twilight_begin', civil_twilight_begin);\n\n//civil_twilight_end\nvar civil_twilight_end = recalculateTime(msg.payload.results.civil_twilight_end);\nglobal.set('Time_civil_twilight_end', civil_twilight_end);\n\n//nautical_twilight_begin\nvar nautical_twilight_begin = recalculateTime(msg.payload.results.nautical_twilight_begin);\nglobal.set('Time_nautical_twilight_begin', nautical_twilight_begin);\n\n//nautical_twilight_end\nvar nautical_twilight_end = recalculateTime(msg.payload.results.nautical_twilight_end);\nglobal.set('Time_nautical_twilight_end', nautical_twilight_end);\n\n//astronomical_twilight_begin\nvar astronomical_twilight_begin = recalculateTime(msg.payload.results.astronomical_twilight_begin);\nglobal.set('Time_astronomical_twilight_begin', astronomical_twilight_begin);\n\n//astronomical_twilight_end\nvar astronomical_twilight_end = recalculateTime(msg.payload.results.astronomical_twilight_end);\nglobal.set('Time_astronomical_twilight_end', astronomical_twilight_end);\n\n\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":860,"y":200,"wires":[["a792452e.0b27c8"]]},{"id":"587817ba.2103d8","type":"inject","z":"b4f27448.856258","name":"Get Sun info","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":200,"y":210,"wires":[["d9e937ee.73a7c8"]]},{"id":"d9e937ee.73a7c8","type":"function","z":"b4f27448.856258","name":"Get Sunset info","func":"var latitude = flow.get('Latitude');\nvar longitude = flow.get('Longitude');\n\n\nmsg.url = \"https://api.sunrise-sunset.org/json?&formatted=0&lat=\"+ latitude +\"&lng=\"+ longitude;\n\nreturn msg;\n\n//https://sunrise-sunset.org/api\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":375,"y":210,"wires":[["2b681956.c78bd6"]]},{"id":"2b681956.c78bd6","type":"http request","z":"b4f27448.856258","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"","tls":"","persist":false,"proxy":"","authType":"","x":545,"y":210,"wires":[["76b7ffa3.d3a2f"]]},{"id":"1a2319d2.2a7926","type":"comment","z":"b4f27448.856258","name":"Is it night time yet","info":"","x":205,"y":290,"wires":[]},{"id":"a792452e.0b27c8","type":"debug","z":"b4f27448.856258","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1220,"y":210,"wires":[]},{"id":"ce90dae6.7520f8","type":"subflow:3c1a9880.5362b8","z":"b4f27448.856258","name":"","env":[{"name":"Latitude","value":"54.5232866","type":"num"},{"name":"Longitude","value":"-1.3063204","type":"num"}],"x":360,"y":160,"wires":[["e6cea506.9a7e68"]],"icon":"node-red/cog.svg"},{"id":"ab1eeb84.8d6308","type":"inject","z":"b4f27448.856258","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":210,"y":160,"wires":[["ce90dae6.7520f8"]]},{"id":"e6cea506.9a7e68","type":"debug","z":"b4f27448.856258","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":160,"wires":[]}]

Flow Info

Created 4 years, 4 months ago
Updated 4 years ago
Rating: not yet rated

Actions

Rate:

Node Types

Core
  • comment (x2)
  • debug (x3)
  • function (x4)
  • http request (x1)
  • inject (x3)
Other
  • subflow (x1)
  • subflow:3c1a9880.5362b8 (x1)
  • tab (x1)

Tags

  • time
  • sunset
  • sunrise
  • timezones
  • coordinates
  • nautical
  • twilight
  • astronomical
  • civil
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option