Skip to content

Instantly share code, notes, and snippets.

@statico
Last active April 27, 2021 17:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statico/5f1871d05722d3e61a9311131bda93ea to your computer and use it in GitHub Desktop.
Save statico/5f1871d05722d3e61a9311131bda93ea to your computer and use it in GitHub Desktop.
Purple Air Sensor Plugin for BitBar
#!/usr/bin/env bash
#
# <bitbar.title>Purple Air PM2.5 AQI</bitbar.title>
# <bitbar.dependencies>bash,jq,node</bitbar.dependencies>
#
# Derived from https://github.com/matryer/bitbar-plugins/blob/master/Weather/aqi.15m.sh by Chongyu Yuan
COLORS=("#0ed812" "#ffde33" "#ff9933" "#cc0033" "#660099" "#7e0023" "#404040")
PURPLE_AIR_SENSOR_ID=5220
URL="https://www.purpleair.com/map?opt=1/i/mAQI/a10/cC0&select=${PURPLE_AIR_SENSOR_ID}#6.46/38.305/-122.813"
DATA=$(curl -s https://www.purpleair.com/json?show=${PURPLE_AIR_SENSOR_ID} | /usr/local/bin/jq '.results[0].PM2_5Value')
# Conversion code from https://cfpub.epa.gov/airnow/index.cfm?action=airnow.calculator
/Users/ian/.nvm/versions/node/v12.9.1/bin/node >/tmp/aqi <<EOF
function Linear(AQIhigh, AQIlow, Conchigh, Conclow, Concentration) {
var linear
var Conc = parseFloat(Concentration)
var a
a = ((Conc - Conclow) / (Conchigh - Conclow)) * (AQIhigh - AQIlow) + AQIlow
linear = Math.round(a)
return linear
}
function AQIPM25(Concentration) {
var Conc = parseFloat(Concentration)
var c
var AQI
c = Math.floor(10 * Conc) / 10
if (c >= 0 && c < 12.1) {
AQI = Linear(50, 0, 12, 0, c)
} else if (c >= 12.1 && c < 35.5) {
AQI = Linear(100, 51, 35.4, 12.1, c)
} else if (c >= 35.5 && c < 55.5) {
AQI = Linear(150, 101, 55.4, 35.5, c)
} else if (c >= 55.5 && c < 150.5) {
AQI = Linear(200, 151, 150.4, 55.5, c)
} else if (c >= 150.5 && c < 250.5) {
AQI = Linear(300, 201, 250.4, 150.5, c)
} else if (c >= 250.5 && c < 350.5) {
AQI = Linear(400, 301, 350.4, 250.5, c)
} else if (c >= 350.5 && c < 500.5) {
AQI = Linear(500, 401, 500.4, 350.5, c)
} else {
AQI = 'PM25message'
}
return AQI
}
// console.error("PM2_5Value:", $DATA)
console.log(AQIPM25($DATA))
EOF
AQI=$(cat /tmp/aqi)
function colorize {
if [ "$1" = "-" ]; then
echo "${COLORS[6]}"
elif [ "$1" -le 50 ]; then
echo "${COLORS[0]}"
elif [ "$1" -le 100 ]; then
echo "${COLORS[1]}"
elif [ "$1" -le 150 ]; then
echo "${COLORS[2]}"
elif [ "$1" -le 200 ]; then
echo "${COLORS[3]}"
elif [ "$1" -le 300 ]; then
echo "${COLORS[4]}"
else
echo "${COLORS[5]}"
fi
}
COLOR="$(colorize "${AQI}")"
echo "AQI ${AQI} | color=${COLOR}}"
echo "---"
echo "Detail... | href=${URL}"
echo "Refresh... | refresh=true"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment