forecast and weather GUI

Post your working scripts, libraries and tools for AHK v1.1 and older
TheNaviator
Posts: 5
Joined: 18 Mar 2024, 13:07

forecast and weather GUI

18 Mar 2024, 14:21

had this code and wanted to share it, really had fun making it.

Code: Select all

FormatTime, Month, , M
todaystr  = %A_DDD% %A_DD%/%Month%:
tomorrowstr := TomorrowName() . " " . TomorrowDate() . ":"
Data := UpdateWeather()
tempnow := Data.current.temperature_2m Chr(0x00B0)
feelslike := Data.current.apparent_temperature Chr(0x00B0)
today_max := Chr(0x1F4C8) Data.daily.temperature_2m_max[1]
today_min := Chr(0x1F4C9) Data.daily.temperature_2m_min[1]
tomorrow_max := Chr(0x1F4C8) Data.daily.temperature_2m_max[2]
tomorrow_min := Chr(0x1F4C9) Data.daily.temperature_2m_min[2]
currentWeather := GetWeatherDescription(Data.current.weather_code)
nowimage := GetWeatherImage(Data.current.weather_code)
todaydesc := GetWeatherDescription(Data.Daily.weather_code[1])
todayimage := GetWeatherImage(Data.Daily.weather_code[1])
tomorrowdesc := GetWeatherDescription(Data.Daily.weather_code[2])
tomorrowimage := GetWeatherImage(Data.Daily.weather_code[2])
; daylightduration := "about " . Round(Data.Daily.daylight_duration[1] / 3600) . " hours of daylight tomorrow"
sunset := SubStr(Data.Daily.sunset[1], 12)
; cloudage := Data.current.cloud_cover . "%"
rainchancetoday := Data.Daily.precipitation_probability_max[1] . "%"
rainchancetomorrow := Data.Daily.precipitation_probability_max[2] . "%"
maxcolor := CheckColor(Data.daily.temperature_2m_max[1])
mincolor := CheckColor(Data.daily.temperature_2m_min[1])
tomorrowmaxcolor := CheckColor(Data.daily.temperature_2m_max[2])
tomorrowmincolor := CheckColor(Data.daily.temperature_2m_min[2])
nowcolor := CheckColor(Data.current.temperature_2m)
feelslikecolor := CheckColor(Data.current.apparent_temperature)
; If (Data.daily.temperature_2m_max[1] > 20)
;    maxcolor = yellow
; Else
;    maxcolor := "white" ;CheckColor(Data.daily.temperature_2m_max[1])
; If (Data.daily.temperature_2m_min[1] > 20)
;    mincolor = yellow
; Else
;    mincolor := "white" ;CheckColor(Data.daily.temperature_2m_max[1])
; If (Data.daily.temperature_2m_min[2] > 20)
;    tomorrowmincolor = yellow
; Else
;    tomorrowmincolor := "white" ;CheckColor(Data.daily.temperature_2m_max[1])
; If (Data.daily.temperature_2m_max[2] > 20)
;    tomorrowmaxcolor = yellow
; Else
;    tomorrowmaxcolor := "white"
weatherbackground := GetBackground(Data.current.weather_code)
Gui, TextGUI:Margin, 0, 0
Gui, TextGUI:+Border +Owner -Caption -MaximizeBox +AlwaysOnTop +Hwndtexthwnd +LastFound +ToolWindow +E0x20
Gui, TextGUI:Color, 131314
Gui, TextGUI:Font, S30 Q4, Leelawadee UI Semilight
Gui, TextGUI:Add, Text, vtempnow_id cYellow x0 y-10, %tempnow%
; Gui, TextGUI:Add, Picture, x0 y0 h50 w50 HWNDpic, D:\Autohotkey Scripts\weather icons\sunset.png
Gui, TextGUI:Font, S20 Q4, Leelawadee UI Semilight
Gui, TextGUI:Add, Text, vfeelslike_id cWhite x0 y35, Feels like %feelslike%
Gui, TextGUI:Font, S17 Q4, Leelawadee UI Semilight
Gui, TextGUI:Add, Text, vcurrentweather_id cWhite x150 y0, %currentWeather%
; Gui, TextGUI:Font, S16 Q4, Segoe UI
Gui, TextGUI:Add, Picture, x100 y90 h40 w40 HWNDtodayimage_id, %todayimage%
Gui, TextGUI:Add, Picture, x100 y145 h40 w40 HWNDtomorrowimage_id, %tomorrowimage%
Gui, TextGUI:Add, Picture, x100 y0 h40 w40 HWNDnowimage_id, %nowimage%
; Gui, TextGUI:Add, Picture, x0 y45 h25 w45, cloudy.png
; Gui, TextGUI:Add, Text, cWhite x55 y45 vcloudage_id, %cloudage%
; Gui, TextGUI:Add, Text, cWhite x0 y0 vcurrentWeather_id, %currentWeather%
;Gui, TextGUI:Add, Picture, x0 y0 h40 w40 HWNDpic, D:\Autohotkey Scripts\weather icons\sunset.png CONITINUE - ADD IMAGES
Gui, TextGUI:Add, Picture, x300 y40 h50 w50 HWNDpic, D:\Autohotkey Scripts\weather icons\sunset.png
Gui, TextGUI:Add, Text, cWhite x355 y45 vsunset_id, %sunset%
Gui, TextGUI:Add, Text, cWhite x0 y90 vtodaystr, %todaystr%
Gui, TextGUI:Add, Picture, x140 y90 h40 w35, rain chance.png
Gui, TextGUI:Add, Text, cWhite x180 y95 vrainchancetoday_id,%rainchancetoday%
Gui, TextGUI:Add, Text, c%maxcolor% x233 y90 vtoday_max_id, %today_max%
Gui, TextGUI:Add, Text, c%mincolor% x310 y90 vtoday_min_id, %today_min%
Gui, TextGUI:Add, Text, cWhite x0 y145 vtomorrow, %tomorrowstr%
Gui, TextGUI:Add, Picture, x140 y140 h40 w35, rain chance.png
Gui, TextGUI:Add, Text, cWhite x180 y145 vrainchancetomorrow_id, %rainchancetomorrow%
Gui, TextGUI:Add, Text, c%tomorrowmaxcolor% x233 y145 vtomorrow_max_id, %tomorrow_max%
Gui, TextGUI:Add, Text, c%tomorrowmincolor% x310 y145 vtomorrow_min_id, %tomorrow_min%
Gui, TextGUI:Show, x2540 y100 w430 h180 NA
WinSet, Transparent, 200
SetTimer, UpdateWidget, 600000
; SetTimer, UpdateWidget, 2000
Return


UpdateWidget:
prev_nowimage := GetWeatherImage(Data.current.weather_code)
prev_todayimage := GetWeatherImage(Data.Daily.weather_code[1])
prev_tomorrowimage := GetWeatherImage(Data.Daily.weather_code[2])
FileRead, yuh, D:\Autohotkey Scripts\Weather logs\Weather Monday 18.03.2024.json
Data := JSON.Load(yuh)
tempnow := Data.current.temperature_2m Chr(0x00B0)
feelslike := Data.current.apparent_temperature Chr(0x00B0)
today_max := Chr(0x1F4C8) Data.daily.temperature_2m_max[1]
today_min := Chr(0x1F4C9) Data.daily.temperature_2m_min[1]
tomorrow_max := Chr(0x1F4C8) Data.daily.temperature_2m_max[2]
tomorrow_min := Chr(0x1F4C9) Data.daily.temperature_2m_min[2]
currentWeather := GetWeatherDescription(Data.current.weather_code)
nowimage := GetWeatherImage(Data.current.weather_code)
todaydesc := GetWeatherDescription(Data.Daily.weather_code[1])
todayimage := GetWeatherImage(Data.Daily.weather_code[1])
tomorrowdesc := GetWeatherDescription(Data.Daily.weather_code[2])
tomorrowimage := GetWeatherImage(Data.Daily.weather_code[2])
; daylightduration := "about " . Round(Data.Daily.daylight_duration[1] / 3600) . " hours of daylight tomorrow"
sunset := SubStr(Data.Daily.sunset[1], 12)
; cloudage := Data.current.cloud_cover . "%"
rainchancetoday := Data.Daily.precipitation_probability_max[1] . "%"
rainchancetomorrow := Data.Daily.precipitation_probability_max[2] . "%"
maxcolor := CheckColor(Data.daily.temperature_2m_max[1])
mincolor := CheckColor(Data.daily.temperature_2m_min[1])
tomorrowmaxcolor := CheckColor(Data.daily.temperature_2m_max[2])
tomorrowmincolor := CheckColor(Data.daily.temperature_2m_min[2])
nowcolor := CheckColor(Data.current.temperature_2m)
feelslikecolor := CheckColor(Data.current.apparent_temperature)
if (prev_nowimage != nowimage) {
   GuiControl, , %nowimage_id%, %nowimage%
}
if (prev_todayimage != todayimage) {
   GuiControl, , %todayimage_id%, %todayimage%
}
if (prev_tomorrowimage != tomorrowimage) {
   GuiControl, , %tomorrowimage_id%, %tomorrowimage%
}
GuiControl, TextGui:, tempnow_id, %tempnow%
GuiControl, TextGui:, feelslike_id, Feels like %feelslike%
GuiControl, TextGui:, currentWeather_id, %currentWeather%
GuiControl, TextGui:, sunset_id, %sunset%
GuiControl, TextGui:, rainchancetoday_id, %rainchancetoday%
GuiControl, TextGui:, cloudage_id, %cloudage%
GuiControl, TextGui:, today_max_id, %today_max%
GuiControl, TextGui:+c%maxcolor%, today_max_id
GuiControl, TextGui:, today_min_id, %today_min%
GuiControl, TextGui:+c%mincolor%, today_min_id
GuiControl, TextGui:, rainchancetomorrow, %rainchancetomorrow%
GuiControl, TextGui:, tomorrow_max_id, %tomorrow_max%
GuiControl, TextGui:+c%tomorrowmaxcolor%, tomorrow_max_id
GuiControl, TextGui:, tomorrow_min_id, %tomorrow_min%
GuiControl, TextGui:+c%tomorrowmincolor%, tomorrow_min_id
Return
the functions i used :

Code: Select all

;Returns an object with the details of the weather
UpdateWeather()
{
   link := "https://api.open-meteo.com/v1/forecast?&current=temperature_2m,apparent_temperature,weather_code,cloud_cover&daily=weather_code,temperature_2m_max,temperature_2m_min,sunset,daylight_duration,precipitation_probability_max&timezone=auto&forecast_days=3&forecast_hours=6" ;"https://wttr.in/petah+tiqwa?format=%t"
   FormatTime, Time,, dddd dd.MM.yyyy
   UrlDownloadToFile, %link%, D:\Autohotkey Scripts\Weather logs\Weather %Time%.json
   FileRead, yuh, D:\Autohotkey Scripts\Weather logs\Weather %Time%.json
   yuh := JSON.Load(yuh)
   Return yuh
}

; Returns the color based on the temperature
CheckColor(temp) {
   if (temp >= 10 and temp < 15) {
       return "White"
   } else if (temp >= 15 and temp < 25) {
       return "ffff14" ; bright yellow
   } else if (temp >= 25 and temp < 30) {
       return "d98a12"
   } else if (temp >= 30 and temp < 50) {
       return "Red"
   } else {
       return "White"
   }
}

;Gets tomorrow's date
TomorrowDate(GetYear := False) {
	today = %a_now%
	today += +1, days
	If (GetYear)
		FormatTime, today, %today%, dd/M/yy
	Else
		FormatTime, today, %today%, dd/M
	return today
}

;Gets tomorrow's name based on today
TomorrowName()
{
   Switch A_DDD
   {
      case "Sun":
         Return "Mon"
      case "Mon":
         Return "Tue"
      case "Tue":
         Return "Wed"
      case "Wed":
         Return "Thu"
      case "Thu":
         Return "Fri"
      case "Fri":
         Return "Sat"
      case "Sat":
         Return "Sun"
   }
}

;Returns the rain chance in words
RainChance(rainchance) {
   if (rainchance >= 0 and rainchance < 10) {
       return "no chance"
   } else if (rainchance >= 10 and rainchance < 30) {
       return "low chance"
   } else if (rainchance >= 30 and rainchance < 50) {
       return "some chance"
   } else if (rainchance >= 50 and rainchance < 70) {
       return "moderate chance"
   } else if (rainchance >= 70 and rainchance < 90) {
       return "high chance"
   } else if (rainchance >= 90 and rainchance <= 100) {
       return "very high chance"
   } else {
       return "Invalid chance"
   }
}

;Translates the cloud coverage % to a description
CloudCoverage(percentage) {
   if (percentage >= 0 and percentage <= 15) {
       return "Clear"
   } else if (percentage > 15 and percentage <= 30) {
       return "Partly Cloudy"
   } else if (percentage > 30 and percentage <= 70) {
       return "Cloudy"
   } else if (percentage > 70 and percentage <= 100) {
       return "Overcast"
   } else {
       return "Invalid percentage"
   }
}

;Returns a corresponding image based on the weather code
GetWeatherImage(code) {
   switch (code) {
       case 0:
		 Image = clear.png
       case 1, 2, 3:
		 Image = partly cloudy.png
       case 45, 48:
		 Image = fog.png
       case 51, 53, 55:
		 Image = rain.png
       case 56, 57:
		 Image = rain.png
       case 61, 63, 65:
		 Image = heavy rain.png
       case 66, 67:
		 Image = thunderstorm.png
       case 71, 73, 75:
		 Image = thunderstorm.png
       case 77:
		 Image = thunderstorm.png
       case 80, 81, 82:
		 Image = heavy rain.png
       case 85, 86:
		 Image = thunderstorm.png
       case 95:
		 Image = heavy rain.png
       case 96, 99:
		 Image = thunderstorm.png
       default:
           return "Unknown code"
   }
	Return Image
}

;Returns a description of the weather by its weather code (WMC)
GetWeatherDescription(code) {
   switch (code) {
       case 0:
           return  "Clear skies"
       case 1, 2, 3:
           return "Mainly clear, partly cloudy"
       case 45, 48:
           return "Foggy"
       case 51, 53, 55:
           return "Drizzle: Light intensity"
       case 56, 57:
           return "Freezing Drizzle: Light intensity"
       case 61, 63, 65:
           return "Rainy: Moderate intensity"
       case 66, 67:
           return "Freezing Rain: Heavy intensity"
       case 71, 73, 75:
           return "Snow fall: Slight intensity"
       case 77:
           return "Snow grains"
       case 80, 81, 82:
           return "Rain showers: Violent"
       case 85, 86:
           return "Snow showers slight and heavy"
       case 95:
           return "Slight thunderstorm"
       case 96, 99:
           return "Thunderstorm with slight and heavy hail"
       default:
           return "Unknown code"
   }
}
i think its pretty useful, i like having the forecast at a glance.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: forecast and weather GUI

18 Mar 2024, 14:49

Hi @TheNaviator,

I see that this is your first post here, so let me start with...Welcome Aboard!

I was looking forward to testing this, but when I tried to run it, the GetBackground function is missing. Regards, Joe
gregster
Posts: 9035
Joined: 30 Sep 2013, 06:48

Re: forecast and weather GUI

18 Mar 2024, 14:58

Yes, welcome to the AHK forums!
Thank you for the contribution - but some feedback might be appropriate.

I didn't try to run it yet, but it should be added that the png weather images are also not included, and that a JSON library will be needed (it's a good custom to at least link to all needed libraries or functions).

Edit:
I would also advise against writing to a directory like the hardcoded one in this line:

Code: Select all

   UrlDownloadToFile, %link%, D:\Autohotkey Scripts\Weather logs\Weather %Time%.json
There could be problems with writing permissions (if D:\ exists at all) - and the user might not expect it. A (relative) subdirectory of the folder the script is in should be a better choice for a default. Btw, does the weather only get downloaded once at script start?

Edit2:
There is also this hardcoded in the update subroutine...

Code: Select all

FileRead, yuh, D:\Autohotkey Scripts\Weather logs\Weather Monday 18.03.2024.json
Does that subroutine - which gets called every 600 seconds afaics - actually update smth?

Perhaps you could have another look at the script and test it from the viewpoint of an interested forum user.
As long as you are new user with limited rights, we could update the original post for you, if you decide to update your code and/or to add some remarks.
TheNaviator
Posts: 5
Joined: 18 Mar 2024, 13:07

Re: forecast and weather GUI

18 Mar 2024, 21:57

hey, sorry i didn't clean it up much other than the link which had the coordinates. yes you're right, the 2nd hardcoded FileRead was meant for debugging and i forgot to remove it and switch it to the UpdateWeather function (it's in my first post here, it returns an object with all the weather info). actually something about the GetBackground function - it's from an older version of the script. today i had the energy to revise it and give it a more sleek look. i'd upload an image of how it looks for me currently and how it looked before if i figured out how to send files/images. so to clear things up - 1. you don't need that line in the code with getbackground func, you might as well delete it. and 2. it was for debugging and i forgot to remove it. yeah it's supposed to update the weather every 10 minutes to show current temp, forecast for today and tomorrow with weather descriptions basically not very complicated
gregster
Posts: 9035
Joined: 30 Sep 2013, 06:48

Re: forecast and weather GUI

18 Mar 2024, 22:11

TheNaviator wrote:
18 Mar 2024, 21:57
i'd upload an image of how it looks for me currently and how it looked before if i figured out how to send files/images.
You should be able to drag-and-drop png or jpg files onto the full editor's edit field (at least a limited number per post). You can click 'Preview' to check.

Generally, you can also scroll down in the 'Full Editor' view and click on the 'Attachments' tab (then 'Add files'). There you can also upload pictures and certain other file types like .ahk and .zip. Use 'Place inline' to place images at any place in the post - otherwise all pictures will be shown at the end of the post. The following picture was placed inline:

attachments.png
attachments.png (51.57 KiB) Viewed 269 times

If you have a revised script, you could post it below and we could replace the original code.
TheNaviator
Posts: 5
Joined: 18 Mar 2024, 13:07

Re: forecast and weather GUI

18 Mar 2024, 22:22

hey im just about to go to sleep ill upload the files and the not-revised version, i think someone might say the not-revised version is nicer and less minimalist but im not a UI designer :) ill upload the images too. what i was talking about in the first post was the updated version and i just kept the not updated version
gregster
Posts: 9035
Joined: 30 Sep 2013, 06:48

Re: forecast and weather GUI

18 Mar 2024, 22:30

Alright, no worries. For a larger number of (small) pictures, uploading a zip-file might be useful.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: forecast and weather GUI

18 Mar 2024, 22:42

TheNaviator wrote:you don't need that line in the code with getbackground func, you might as well delete it.
Yeah, especially since the weatherbackground var is not used anywhere in the script. :)

The big problem is that the UrlDownloadToFile does not work. Using your link var unchanged, I ran this:

Code: Select all

downloadtest:=A_Temp . "\WeatherTest.txt"
FileDelete,%downloadtest%
link := "https://api.open-meteo.com/v1/forecast?&current=temperature_2m,apparent_temperature,weather_code,cloud_cover&daily=weather_code,temperature_2m_max,temperature_2m_min,sunset,daylight_duration,precipitation_probability_max&timezone=auto&forecast_days=3&forecast_hours=6" ;"https://wttr.in/petah+tiqwa?format=%t"
UrlDownloadToFile,%link%,%downloadtest%
Run,%downloadtest%
The resulting file is empty...zero bytes. Regards, Joe
TheNaviator
Posts: 5
Joined: 18 Mar 2024, 13:07

Re: forecast and weather GUI

19 Mar 2024, 02:40

yeah the file is broken, i very loosely removed my coordinates but you need some type of coordinates, this needs some modifications to add your own.
https://api.open-meteo.com/v1/forecast? ... st_hours=6
replace the zeroes with your coordinates
TheNaviator
Posts: 5
Joined: 18 Mar 2024, 13:07

Re: forecast and weather GUI

19 Mar 2024, 06:54

attached: a zip containing the fixed script i used (updates the info every 10 minutes although it does have comments i left out) and the images i used. some configuration needed.
User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: forecast and weather GUI

19 Mar 2024, 07:53

@TheNaviator - There is no file attached to your post.
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: forecast and weather GUI

19 Mar 2024, 21:25

TheNaviator wrote:replace the zeroes with your coordinates
OK, the link works with proper values for latitude and longitude.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 162 guests