willasm / obsidian-open-weather

Obsidian plugin for OpenWeather API
MIT License
47 stars 3 forks source link

Support for Daily Forecast #10

Closed gazoir closed 4 months ago

gazoir commented 1 year ago

Hi! I’m trying to create a forecast section in my daily note. Right now it seems like you have amazing support for the current and dynamic weather in a status bar, but I’m looking for a burnout of the forecast for the day, with things like:

I know some daily data is already exposed (eg. Sunset/Sunrise); I wonder if higher level daily info is something you’ve considered roadmapping? Thanks for the plugin it’s very clear and simple to use.

willasm commented 1 year ago

I would love to include more data but unfortunately most of open weathers api's are paid subscriptions. The one I'm using (Current Weather Data) is completely free and allows for up to 60 calls/minute or 1,000,000 calls/month. More than any single user is likely to ever use. They have other api's that allow some free calls but are not nearly as generous in the number of free calls allowed before starting to charge the user. You can have a look at a list all the api's here. Note: their website can be difficult to navigate and find what you are looking for. I obviously want to avoid any api that could possibly incur a cost to the user. Unless something has changed to their api's list since I originally subscribed (that was more than a year ago) I'm not likely to change api's. Note that if I were to switch to another api that would essentially require a complete re-write of the plugin as well and that is something I do not want to do unless the different api would be worth the effort. Anyhow, have a look at that list, there may be something added since I last checked...

ssjy1919 commented 10 months ago

Hi! I saw a comparison table on the website https://openweathermap.org/price, and in the free column, there is an option for "3-hour Forecast 5 days mischief". I'm wondering if it's possible to add it?

willasm commented 10 months ago

I will have a look at it. It has been a while since I have been there and they occasionally make changes to their plans so it might be possible. Give me some time to see if this will be doable or not.

VivianBombycilla commented 7 months ago

It turns out it's not much work to add forecast API data: Where the "current weather" API link is https://api.openweathermap.org/data/2.5/weather, the "forecast" API link is https://api.openweathermap.org/data/2.5/forecast. Here's a link to the documentation.

I managed to add my own forecast API data by adding these lines to main.js (apologies if it's scuffed, I have no essentially no experience):

    let url2 = `https://api.openweathermap.org/data/2.5/forecast?q=${this.location}&lang=${this.language}&appid=${this.key}&units=${this.units}`;
    let req2 = await fetch(url2);
    let json2 = await req2.json();
    let forecastTime1 = json2.list[0].dt_txt
    let forecastTime2 = json2.list[1].dt_txt
    let forecastTime3 = json2.list[2].dt_txt
    let forecastTime4 = json2.list[3].dt_txt
    let forecastTime5 = json2.list[4].dt_txt
    let forecastTemp1 = json2.list[0].main.temp
    let forecastTemp2 = json2.list[1].main.temp
    let forecastTemp3 = json2.list[2].main.temp
    let forecastTemp4 = json2.list[3].main.temp
    let forecastTemp5 = json2.list[4].main.temp
    weatherData = {
      ...
      "forecastTime1": forecastTime1,
      "forecastTime2": forecastTime2,
      "forecastTime3": forecastTime3,
      "forecastTime4": forecastTime4,
      "forecastTime5": forecastTime5,
      "forecastTemp1": forecastTemp1,
      "forecastTemp2": forecastTemp2,
      "forecastTemp3": forecastTemp3,
      "forecastTemp4": forecastTemp4,
      "forecastTemp5": forecastTemp5,
    };
...
    weatherString = weatherString.replace(/%forecastTime1%/gmi, `${weatherData.forecastTime1}`);
    weatherString = weatherString.replace(/%forecastTime2%/gmi, `${weatherData.forecastTime2}`);
    weatherString = weatherString.replace(/%forecastTime3%/gmi, `${weatherData.forecastTime3}`);
    weatherString = weatherString.replace(/%forecastTime4%/gmi, `${weatherData.forecastTime4}`);
    weatherString = weatherString.replace(/%forecastTime5%/gmi, `${weatherData.forecastTime5}`);
    weatherString = weatherString.replace(/%forecastTemp1%/gmi, `${weatherData.forecastTemp1}`);
    weatherString = weatherString.replace(/%forecastTemp2%/gmi, `${weatherData.forecastTemp2}`);
    weatherString = weatherString.replace(/%forecastTemp3%/gmi, `${weatherData.forecastTemp3}`);
    weatherString = weatherString.replace(/%forecastTemp4%/gmi, `${weatherData.forecastTemp4}`);
    weatherString = weatherString.replace(/%forecastTemp5%/gmi, `${weatherData.forecastTemp5}`);
VivianBombycilla commented 7 months ago

A couple more notes about the 5-day forecast:

willasm commented 7 months ago

Sorry I have not gotten back to you on this. I did have a look at this and ran into the same issue regarding the forecast for the current 3 hour period. There is a historic API which would solve this but unfortunately it is a paid API. Then Christmas was coming and I got sidetracked and this ended up on the backburner. I will have another look at this this weekend. I had thought about using a script but it would only run when Obsidian is started leaving the same issue for the current 3 hour period. Thank you for adding this to the code in your main.js (nice job by the way), it looks basically like what I had already attempted and that is where I got stumped with the current forecast issue. Like I said, I will have another look at this over the weekend and see what I can come up with. I may just add it as is and add the current time period forecast limitation to the Readme to make people aware of it.

Thanks for bringing this back to my attention. I had forgotten about this after the holidays and it is a feature I would like to have, William.

willasm commented 7 months ago

Just want to keep you informed about what is happening. I spent the past week fixing a couple of long standing bugs that affected a few users and I added a new setting for using Latitude and Longitude which overrides the location setting. This was needed as a few users were getting the wrong data returned. For example, if 2 cities have the same name the API was returning the first one found. That meant a 50/50 chance of getting the wrong data back and it gets worse if there are more than 2 cities Eg. Springfield. This was something that had to be fixed first as it potentially affected a lot of people.

These fixes are already implemented but I have not created a release yet as I wanted to let you know that any update will overwrite the changes you have currently made to your Main.js file. Could you please make a backup copy and let me know when that is done so I can make a release with these fixes.

Now that these fixes have been implemented your request is now at the top of my list. I want to get this added as I can make use of this as well. I also had a quick look at one of the other free API's that I might like to pull some data from as well. I am looking forward to making this addition and have started imagining how to display this new data in my Daily template.

Please let me know when you have that backed up and I will keep you updated on my progress with this addition, William

willasm commented 7 months ago

I have just uploaded a new release, v1.7.0 which adds a lot of new features. I added the Air Pollution API and the Geocoding API. The Geocoding API was a priority as the request by name feature for the OpenWeather API has been depreciated for quite a while now. They could remove that option at any time and the plugin would stop working when that happens so that had to be fixed first. The Geocoding API retrieves a list of up to 5 cities that match your search query. The response file includes the latitude and longitude for the city which is the new preferred way to make any request from any of their API's. It is actually the only way to get the Air Pollution API response file. I have added a new button in the settings that will do just that, so much easier to enter your city without errors now.

I have started on adding the daily forecast already. It seems straight forward enough but it is a lot of data that is returned. Just thinking about how best to expose the data to the end user. Most likely something like day1.temp, day1.feelslike, day2.temp, day2.feelslike, and so on. If you have any suggestions please let me know.

Thanks, William

willasm commented 7 months ago

Finally making some headway on this. Have run into a few issues but am making progress now. Here is a couple of screenshots...

Example template... Test1

After placeholder replacements... Test2

Did have a memory issue involving all these variable placeholder replacements that had to be solved before I could get anywhere. Each weather variable needs to be defined 40 times to account for each 3 hour time slice. Then each also requires 40 placeholder string definitions with 40 replace text calls just for one piece of weather data. That is very time consuming to enter all of this but now that I have everything working as expected, entering all the rest of the weather data variables should be much quicker. One nice feature is that the future and current weather placeholders can be mixed in the same document without any issues.

One thing to note is the date and time returned by the API is in GMT. That means that the dt_text date and time is only going to be accurate if you live near the prime meridian. For me it was off by 7 hours. I have corrected for this and the date and time strings are now set to local times. Another annoyance is that technically there are only 4 days with complete data for the day with the exception of a 3 hour period when the first of the 40 data objects is just past midnight. When that occurs all 5 days have 8 weather data objects returned (3x8 = 24 hours). For the rest of the day there will be between 1 and 7 weather data objects for the first day (today) and the same for the 5th day combined the two days will have 8. The 4 days in between always have the complete 8. This will be an issue for anyone wanting to see the complete days information. I will have to decide how to handle that.

Anyhow, just wanted to update this thread with the progress. Hope to have at least a partial release soon, so long as all the most important weather data items are added.

Thanks, William.

willasm commented 7 months ago

I will be uploading a new release soon including the 5 day forecast as soon as I update the Readme.md file. I have added all the placeholders (33 x 40 = 1320 total!) plus an additional 3, %next12%, %next24%, and %next48% which returns a formatted list of weather conditions. Here is a couple of screenshots...

Template... 01_template

And after replacement... (Sorry, could not capture to end of 48 hours) 02_replaced

rtm commented 5 months ago

Looking forward to this and thanks for all your work!

gazoir commented 4 months ago

Hey thanks so much for looking into this. I realise it's over a year since I posted this request and I haven't been back since. But I'm still using Obsidian and love this community! If you need someone to test this before you deploy I'd be happy to help.

willasm commented 4 months ago

Thanks for reminding me. I got this done ages ago and just had to update the documentation and thought I had uploaded this already but apparently I never did (getting old I guess). I will update the documentation today and upload the update after. It will get done this weekend for sure.

willasm commented 4 months ago

Just uploaded v1.8.0 which should be available in Obsidian shortly. The 5 day forecast is now available. Please see the ### Forecast Weather Placeholders section in the readme.md file for a full list of the new placeholders.

I am going to close this issue as it has been around for a while. If you have any questions or remarks regarding the new 5 day forecast features, please create a new issue.

Thanks, William.

rtm commented 4 months ago

Thanks, I was looking forward to this!!