Openweathermap Historical Data

OpenWeatherMap (OWM) has historical data available but there's no option in the current nodes for obtianing it.

I use this node as part of an automated watering system. I can reliably check for future rain and decide on watering but I can't easily find out if it already rained. I have noticed that I'm watering after flash flood warnings.

So rather than adding historical data to NodeRED, I'd rather just call the OWM API and find out how much rain we've had in the last day and then decide if I want to water.

Looks like all it needs is an additional UTC Unix value that falls in the last five days (which I'm sure someone more clever than I am can turn into a relative "days, hours, minutes ago" entry in the node).

fixjunk.

The weather display I found and use doesn't need and specific weather node, just runs off the API data

Just subtract the equivalent time of a day in UNIX time.

JavaScript uses milliseconds, so first convert todays UNIX timestamp to milliseconds then delete the equivalent of 24 hours (in milliseconds that is 86400)

image

[{"id":"34fa6b955b5a850f","type":"function","z":"3df5b2d46b354704","name":"Yesterday","func":"let past = msg.payload - (86400) * 1000 // Convert UNIX to milliseconds and remove one day\nmsg1 = {payload: past};\nlet pastday = new Date(past).toLocaleString(\"en-US\");\npastday = pastday.replace(\",\",\"\");  // OPTIONAL Remove Commas\nmsg2 = {payload: pastday};\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":540,"wires":[["21b7b709900ee1bd"],["72dff8d4f9293e7b"]]},{"id":"33e0242831cabb83","type":"inject","z":"3df5b2d46b354704","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":500,"y":540,"wires":[["34fa6b955b5a850f","1c0b31c42a8e5cc0","beeb57da5e0ce076"]]},{"id":"21b7b709900ee1bd","type":"debug","z":"3df5b2d46b354704","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":830,"y":520,"wires":[]},{"id":"1c0b31c42a8e5cc0","type":"function","z":"3df5b2d46b354704","name":"Today","func":"let past = msg.payload\nmsg1 = {payload: past};\nlet pastday = new Date(past).toLocaleString(\"en-US\");\npastday = pastday.replace(\",\",\"\");  // OPTIONAL Remove Commas\nmsg2 = {payload: pastday};\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":380,"wires":[["4728f80a8e9a1d25"],["428439e2b8e1ea46"]]},{"id":"4728f80a8e9a1d25","type":"debug","z":"3df5b2d46b354704","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":830,"y":360,"wires":[]},{"id":"beeb57da5e0ce076","type":"function","z":"3df5b2d46b354704","name":"Two days ago","func":"let past = msg.payload - (86400*2) * 1000 // Convert UNIX to milliseconds and remove two days\nmsg1 = {payload: past};\nlet pastday = new Date(past).toLocaleString(\"en-US\");\npastday = pastday.replace(\",\",\"\");  // OPTIONAL Remove Commas\nmsg2 = {payload: pastday};\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":700,"wires":[["570e0444de929f21"],["b0c7410ab8cd31cb"]]},{"id":"570e0444de929f21","type":"debug","z":"3df5b2d46b354704","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":830,"y":680,"wires":[]},{"id":"428439e2b8e1ea46","type":"debug","z":"3df5b2d46b354704","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":830,"y":420,"wires":[]},{"id":"72dff8d4f9293e7b","type":"debug","z":"3df5b2d46b354704","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":830,"y":580,"wires":[]},{"id":"b0c7410ab8cd31cb","type":"debug","z":"3df5b2d46b354704","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":830,"y":740,"wires":[]}]

Now feed that number into the API for historical data.

You will have to account for each additional past day and submit the request up to five times for the 5 day history.

https://openweathermap.org/history

thanks @Gunner I will look into it.

I don't need a dashboard, but that's ok.

The node-red-contrib-moment node is very handy for doing offset datetime calculations, too.

So for example, you can ask for a date 5 days ago -- no data arithmetic involved, and no worrying about leap seconds, timezone offsets, and daylight savings adjustments.

@Gunner Between the weather display example and the previous dates functions, I managed to cobble together a very useable subflow that appears very similar to the existing openweathermap nodes. You put in the same basic data (lat, long, api, units, etc.) plus add a "Days ago" selection from 1 to 5.

when it receives a timestamp, it generates a weather report for the date a number of "days ago" and outputs it. I then sum up the rain.1h data and have my previous day rainfall total.

perfection!

2 Likes

@Gunner

I have noticed today that without any changes to my API call, OWM historical data has been reporting rain at certain times when there was no rain...

here's a nearby example:
https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=39&lon=-77&appid=2a9f71f67144ad1076b1db7308123ff6&units=imperial&lang=en&exclude=minutely&dt=1634134779

There was no rain at those coords but OWM shows several entries for rain. It's actually worse at my exact lat/long (but I don't want to share them) and it's negatively impacting my automatic watering system.

Have you had any issues like this? Could it be a local station error? This has been going on for a few days...

I honestly don't track reality with the history on my setup...

Your supplied Lat/Lon just allows the API to find the closest available data source to it, but most of these reports will come from specific locations, like airports, and it is entirely possible to have notable differences in types of weather across a local area, say a city.