Six Colors
Six Colors

by Jason Snell & Dan Moren

Childhood Cancer Awareness Month

This month, join us and Relay FM in supporting St. Jude.

By Jason Snell

How bad is the air out there?

LaMetric Time displaying AQI
A hazardous air quality warning in my living room.

Right now, several parts of the state of California are on fire. This isn’t the usual early-fall fire season where dry offshore winds fan the flames—that’s yet to come, oh boy! This is a weirder set of fires started by lightning from a rare set of thunderstorms that passed through a few weeks ago.

In any event, when there are fires burning within a few hundred miles of where you live, the air quality can be really bad and highly variable. Just yesterday, we awoke to clean air. Then the wind shifted and the Air Quality Index (AQI) went rapidly from green to red. It was hazy and ugly outside, and smelled like a barbecue. A couple of hours later, the air was back in healthy territory and I went for a run.

These days, it’s important for me to be able to get an instant read on air quality. And air quality is extremely local; one day last week, our air quality was terrible and a few miles up the freeway it was perfectly good. A couple of days later, the situation was completely reversed.

Recently I discovered PurpleAir, a large network of air-quality monitors, and began using their map to get a better sense of what my local air quality was. (I also ordered a PurpleAir sensor of my own, because there’s nothing more hyperlocal than a sensor in your backyard. They’re backordered. Go figure.)

BitBar displaying AQI
AQI in my menu bar.

Inspired by my adventures with BitBar, I decided I would try to use the PurpleAir web API to create an air-quality notification I could see in my menu bar. I started with my old standby AppleScript, since it’s the scripting language so worn with years of use that it fits me like a glove.

My first challenge was realizing that PurpleAir stations don’t report the AQI number itself. It’s a number that’s calculated from a sensor’s particle count. I found a message board thread that helpfully included a method to translate that data into an AQI number, which is what I wanted to display. The Air Quality Index comes with a range of conditions with colors and names, which I wanted to build into my script, too. (If you want to check out the AppleScript script, it’s here. I eventually adapted it into a PHP script with the help of Dan Moren, and that script is here. You’ll need to find the ID of a PurpleAir station near you and plug it in to the top of the script.)

Bringing it to the living room

It all worked fine, and I ended up with a menu bar item that displays the current AQI number and associated color and category. But then I started to consider other uses for this data. We’ve got a LaMetric Time in my living room, which displays the current time and temperature (straight from my weather station, of course). The LaMetric Time is a fascinating and pricey gadget that I bought because we had become reliant on a Slim Devices Squeezebox music player as a display of the current time and temperature, and as that platform died I was desperate to find a small, bright display with big numbers to use as a replacement. The LaMetric Time fit the bill.

The LaMetric Time can do all sorts of things that I don’t use it for. I just want it to be a display. So I “built” a very simple “app” for mine that does one thing: displays the contents of a page being served by my local webserver. That page contains some simple JSON that the LaMetric Time knows how to interpret and display.

For a few years now, that file has been a simple PHP script that displays the current time and a temperature variable supplied by my weather station. But I had a thought: the LaMetric Time can also show a color icon on the left side of its display. Could I alter my PHP script to check the PurpleAir sensor nearest my house and display a warning icon if the air quality was poor?

The answer was yes. Rather than hit the PurpleAir network every 30 seconds when the LaMetric Time queried the web server, I built a separate script that queries PurpleAir every ten minutes and writes the current air-quality status to a file. Then I modified the PHP file feeding the LaMetric Time to read the status and, if the air quality was poor, insert an air-quality status icon.

There were some bumps along the way, including my discovery that an icon, the time, and the temperature didn’t all fit on the LaMetric Time’s tiny display. I ended up writing a bunch of if-then statements to reduce what is displayed on the LaMetric Time when an icon is present. But in the end, it all worked. If the outside air quality is bad, you can now tell with a glance at the LaMetric Time in our living room.

Bringing it to the iPad

iPad widgets displaying AQI
Different locations, as seen on my iPad’s home screen via Scriptable in iPadOS 14.

So, job done! But I had built up so much momentum with this project that I began to wonder if there were other places I could deploy it. I recalled seeing that there’s a new version of the Scriptable app in beta testing for iOS 14 that allows you to write your own widgets in JavaScript. I’m really not knowledgeable about JavaScript, so this seemed like a tough hill to climb, until I discovered that Matt Silverlock had already been building one.

I discovered that Matt’s script didn’t convert the sensor numbers to a “true” AQI number, so I pointed him at that same forum thread and then began adapting his script based on what I’d already learn. I spent the evening realizing how little I know about JavaScript, but by late last night I had managed to get a widget onto my iPad’s home screen that was more or less what I had envisioned. (Here’s the my version of the code.)

As I wrote last week, I love how BitBar lets me put little bits of information in my Mac’s menu bar. It feels like Scriptable will do the same for my iPad and iPhone, since I’ll be able to create custom widgets that are fed by whatever data sources I’m interested in. That’s pretty great.

The reason I went down this path—horrible fires and an increased sense of isolation because the air quality prevents us from going outside—is very bad. But at least, thanks to some clever software, I was able to help myself and my family a little and exert a bit more control over my life.

If you appreciate articles like this one, support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories, and a special community.


Search Six Colors