Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Weather widget for waybar
"custom/weather": {
"format": "{}",
"tooltip": true,
"interval": 3600,
"exec": "waybar-wttr.py",
"return-type": "json"
},
#!/usr/bin/env python
import json
import requests
from datetime import datetime
WEATHER_CODES = {
'113': '☀️',
'116': '⛅️',
'119': '☁️',
'122': '☁️',
'143': '🌫',
'176': '🌦',
'179': '🌧',
'182': '🌧',
'185': '🌧',
'200': '⛈',
'227': '🌨',
'230': '❄️',
'248': '🌫',
'260': '🌫',
'263': '🌦',
'266': '🌦',
'281': '🌧',
'284': '🌧',
'293': '🌦',
'296': '🌦',
'299': '🌧',
'302': '🌧',
'305': '🌧',
'308': '🌧',
'311': '🌧',
'314': '🌧',
'317': '🌧',
'320': '🌨',
'323': '🌨',
'326': '🌨',
'329': '❄️',
'332': '❄️',
'335': '❄️',
'338': '❄️',
'350': '🌧',
'353': '🌦',
'356': '🌧',
'359': '🌧',
'362': '🌧',
'365': '🌧',
'368': '🌨',
'371': '❄️',
'374': '🌧',
'377': '🌧',
'386': '⛈',
'389': '🌩',
'392': '⛈',
'395': '❄️'
}
data = {}
weather = requests.get("https://wttr.in/?format=j1").json()
def format_time(time):
return time.replace("00", "").zfill(2)
def format_temp(temp):
return (hour['FeelsLikeC']+"°").ljust(3)
def format_chances(hour):
chances = {
"chanceoffog": "Fog",
"chanceoffrost": "Frost",
"chanceofovercast": "Overcast",
"chanceofrain": "Rain",
"chanceofsnow": "Snow",
"chanceofsunshine": "Sunshine",
"chanceofthunder": "Thunder",
"chanceofwindy": "Wind"
}
conditions = []
for event in chances.keys():
if int(hour[event]) > 0:
conditions.append(chances[event]+" "+hour[event]+"%")
return ", ".join(conditions)
data['text'] = WEATHER_CODES[weather['current_condition'][0]['weatherCode']] + \
" "+weather['current_condition'][0]['FeelsLikeC']+"°"
data['tooltip'] = f"<b>{weather['current_condition'][0]['weatherDesc'][0]['value']} {weather['current_condition'][0]['temp_C']}°</b>\n"
data['tooltip'] += f"Feels like: {weather['current_condition'][0]['FeelsLikeC']}°\n"
data['tooltip'] += f"Wind: {weather['current_condition'][0]['windspeedKmph']}Km/h\n"
data['tooltip'] += f"Humidity: {weather['current_condition'][0]['humidity']}%\n"
for i, day in enumerate(weather['weather']):
data['tooltip'] += f"\n<b>"
if i == 0:
data['tooltip'] += "Today, "
if i == 1:
data['tooltip'] += "Tomorrow, "
data['tooltip'] += f"{day['date']}</b>\n"
data['tooltip'] += f"⬆️ {day['maxtempC']}° ⬇️ {day['mintempC']}° "
data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n"
for hour in day['hourly']:
if i == 0:
if int(format_time(hour['time'])) < datetime.now().hour-2:
continue
data['tooltip'] += f"{format_time(hour['time'])} {WEATHER_CODES[hour['weatherCode']]} {format_temp(hour['FeelsLikeC'])} {hour['weatherDesc'][0]['value']}, {format_chances(hour)}\n"
print(json.dumps(data))
@bjesus
Copy link
Author

bjesus commented Sep 13, 2020

screenshot_2020-09-13_21-45-04_877480664

@crstian19
Copy link

crstian19 commented Jan 3, 2021

Thank you dude! Love you <3

@etrigan63
Copy link

etrigan63 commented Apr 28, 2021

What font do I use for this? I am missing the sunrise/sunset icons.

@tmpm697
Copy link

tmpm697 commented Jul 22, 2021

@bjesus : this script causes font adjustment to my current font, is there how to fix it? if it just dues to font and have must to use noto-fonts-emoji, that's sad.

@bjesus
Copy link
Author

bjesus commented Jul 22, 2021

My script doesn't change anything about your fonts. Installing and uninstalling Noto Emoji (or any other emoji font) is outside the scope of this script and you should follow a method that's relevant to your operating system.

@tmpm697
Copy link

tmpm697 commented Jul 22, 2021

yeah, I found that weather icons cause the problems. And fixed that on my pc.

@Zeioth
Copy link

Zeioth commented Sep 16, 2021

Weird, I can run the command manually, but not from waybar.

Here is my config file.
https://pastebin.com/TMRE7ZSM

FIXED: I had to put waybar-wttr.py in ~/.local/bin

@ankkax
Copy link

ankkax commented Nov 15, 2021

How can I make this widget show current temp instead of feelslike temp? Sorry for my lack of understanding python

@bjesus
Copy link
Author

bjesus commented Nov 20, 2021

@ankkax change FeelsLikeC on like 92 to temp_C

@ankkax
Copy link

ankkax commented Nov 21, 2021

@ankkax change FeelsLikeC on like 92 to temp_C

Thank you.

@Haikuno
Copy link

Haikuno commented Jan 15, 2022

For some reason, the widget is only working whenever I run waybar manually, it doesn't seem to work on startup.
Any idea why? I've tried copying the .py file to both /usr/bin/ and ~/.local/bin/ and it still doesn't work. Sorry if I am missing something obvious
Thanks for the widget :)

EDIT: Solved! Turns out waybar was loading faster than my Wi-Fi could get connection, so the widget would fail fetching the data.
I wrote a little shell script to wait for internet access before loading waybar, I'm not sure if there's a better solution but I'm sticking with it for now.

@Haikuno
Copy link

Haikuno commented Jan 16, 2022

Only problem I'm having right now is that https://wttr.in/?format=j1 is giving a different result than https://wttr.in/ for current condition, so the current temperature is outdated. For example, right now it's 26C but the widget reports 43C, which would have been accurate a couple of hours ago I believe.

@bjesus
Copy link
Author

bjesus commented Jan 16, 2022

That's weird - I'm seeing exactly the same temperature in both :-/ not sure how to help but maybe it's worth checking at https://github.com/chubin/wttr.in ?

@PavlosMelissinos
Copy link

PavlosMelissinos commented Mar 13, 2022

Hey, thanks for the script!

For anyone interested, I played a bit with it and here's the result.

I've made a couple of changes:

  • I've added localization support. If you want text to appear in a different language all you have to do is
  • Weather events appear in descending order of probability, e.g. Fog 32%, Overcast 82%, Rain 24% now appears as Overcast 82%, Fog 32%, Rain 24%

@mortenivar
Copy link

mortenivar commented Jun 29, 2022

Thanks for the widget, @bjesus! Could you modify it to also show the wind direction with a little arrow in the waybar?

@bjesus
Copy link
Author

bjesus commented Aug 6, 2022

Hey @mortenivar, you should be able to use winddir16Point and replace it with a unicode character like 🡰 🡲 🡱 🡳 🡴 🡵 🡶 🡷.

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