I made a simple script to display air pollution data at my status bar, and here’s a quick tutorial to add it to your Polybar.
If you don’t know what Polybar is, please check their website. In short, it’s a hackable status bar, and it looks pretty nice combined with I3 for example, or other WM’s that don’t come with their status bar. Wanna see more? Head to unixporn subreddit.
Requirements:
- Polybar
- Node.js 8+
Note that some paths can be different. I’m using Arch Linux and my Polybar config is at ~/.config/polybar
.
1. Download the Node.js script
The code is available on Gist.
The script is making 3 requests. The first one is to get your city name and coordinates based on your IP address, and 2 requests for the air quality data - one is querying by the city name, and the other by coordinates. I noticed that the API sometimes responds with the wrong location, and that’s why the script is making 2 requests.
You can use cURL to download it:1
curl https://gist.githubusercontent.com/bojan88/21e54decfd5961cf91d71e54921deee8/raw > ~/.config/polybar/scripts/air
Don’t forget to make the script executable:1
sudo chmod +x ~/.config/polybar/scripts/air
2. Request the API token for Air Quality Index
Go to http://aqicn.org/data-platform/token/ and submit the form. Your token will be sent to your email address in a few seconds.
3. Add Polybar module to your configuration file
Edit ~/.config/polybar/config
file and add a new module like this:1
2
3
4[module/air-quality]
type = custom/script
exec = AQI_TOKEN=[your API token from 2. step] ~/.config/polybar/scripts/air
interval = 600
The interval is in seconds, so the data is refreshed every 5 minutes.
I have the default output set to '䷸{pm25}|{pm10}@{time}'
.
You can change that by adding --output
argument:1
exec = AQI_TOKEN=[your API token from 2. step] ~/.config/polybar/scripts/air --output '䷸{no2}|{pm25}|{pm10}@{time}'
4. Add the module name to your Polybar config
Edit ~/.config/polybar/config
to insert the newly added module to your desired position - modules-left
or modules-right
.
My modules-right
looks like this:1
modules-right = network-menu air-quality volume battery0 battery1 cpu clock poweroff
5. Reload your Polybar
I have a launch.sh
script in my Ploybar folder.
If you have it too, just run it with:1
~/.config/polybar/launch.sh
That’s it. Your air quality data should appear shortly on your bar.
Debugging
There’s a --debug
argument support with more logging.
You can see all the requests the script is making as well as JSON responses.
Run it with the debug argument to see all the possible values for --output
argument.
Available values are aqi
, time
, and every property name of the iaqi
object.
Example:1
AQI_TOKEN=[your API token from 2. step] ~/.config/polybar/scripts/air --debug --output '䷸{no2}|{pm25}|{pm10}@{time}'
Contributing
Please leave a comment if you notice any mistakes, or you find a place for improvement.
Also, I would appreciate if you can think of a better prefix character for it. I’m currently using ䷸ which I don’t really like.