Skip to content

Instantly share code, notes, and snippets.

@wilt00
Last active February 5, 2022 02:23
Show Gist options
  • Save wilt00/3d4afdc9f8a3bc25b5eff268f3dc2836 to your computer and use it in GitHub Desktop.
Save wilt00/3d4afdc9f8a3bc25b5eff268f3dc2836 to your computer and use it in GitHub Desktop.
Weatherping

A simple service to check the weather every 15 minutes, and send me a nudge if it's nice out. Uses wttr.in for weather, discord webhooks for notifications, and systemd timers for scheduling.

#!/usr/bin/env bash
shopt -s nullglob
for serviceFile in {*.service,*.timer,*.slice}; do
sudo ln -s "$(pwd)/$serviceFile" "/etc/systemd/system/$serviceFile"
done
sudo systemctl daemon-reload
[Unit]
Description=Ping when the weather's nice
Wants=weatherping.timer
[Service]
ExecStart=/home/<REDACTED>/scripts/weatherping.sh
WorkingDirectory=/home/<REDACTED>/scripts
[Install]
WantedBy=multi-user.target
#!/usr/bin/env bash
# https://github.com/chubin/wttr.in/blob/master/lib/constants.py
NICE_CODES=("113 116 119 122 143 248 260")
NICE_TEMP_F=40
HOOK_URL='https://discord.com/api/webhooks/<REDACTED>'
CODE_DESCRIPTIONS=(
[113]="sunny"
[116]="partly cloudy"
[119]="cloudy"
[122]="very cloudy"
[143]="foggy"
[248]="foggy"
[260]="foggy"
)
# yes there are really three codes for foggy
WEATHER_DATA=$(curl wttr.in/Chicago?format=j1)
FEELS_LIKE=$(echo "$WEATHER_DATA" | jq --raw-output '.current_condition[0].FeelsLikeF')
WEATHER_CODE=$(echo "$WEATHER_DATA" | jq --raw-output '.current_condition[0].weatherCode')
if [[ "${NICE_CODES[*]}" =~ $WEATHER_CODE && (($FEELS_LIKE -ge $NICE_TEMP_F)) ]] ; then
MSG="The weather is $FEELS_LIKE°F and ${CODE_DESCRIPTIONS[$WEATHER_CODE]} - go outside!"
curl -X POST -H "Content-Type: application/json" -d "{\"content\": \"$MSG\"}" $HOOK_URL
echo "Weather ping script ran; temp was $FEELS_LIKE, code was $WEATHER_CODE; ping sent"
else
echo "Weather ping script ran; temp was $FEELS_LIKE, code was $WEATHER_CODE; ping not sent"
fi
[Unit]
Description=Run weatherping every 15 mins
Requires=weatherping.service
[Timer]
Unit=weatherping.service
# [7 AM to 8 PM), every 15 mins
# Time zone included because this is running on a VPS
OnCalendar=*-*-* 07..19:00/15 America/Chicago
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment