Created
October 10, 2024 14:04
-
-
Save kawsark/663124c5f089f085d7e0c209a0bf93d3 to your computer and use it in GitHub Desktop.
Run command based on National Weather Service API from weather.gov
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The command to run. Use operation=$1 to pass it in as a parameter. | |
OPERATION='kasa --type plug --alias "basement-fan" on' | |
# Threshold for maximum humidity | |
th=65 | |
# Threshold for minimum temperature | |
tt=60 | |
# Get your Hourly forecast URL from weather.gov: | |
# 1. get your lat/long from google maps: right click on the map where you want the coordinates | |
# 2. Paste it on to the points URL: https://api.weather.gov/points/<latitude>,<longitude> | |
# e.g.: curl https://api.weather.gov/points/43,-78.78 | |
# 3. Get your hourly forecast URL and it set it below: | |
HOURLY_FORECAST_URL=https://api.weather.gov/gridpoints/BUF/38,53/forecast/hourly | |
# Write the response to the tmp directory | |
filename="/tmp/nws-$(date +%N).json" | |
curl -s $HOURLY_FORECAST_URL > $filename | |
# Read the humidity value | |
rh=$(cat $filename | jq -r '.properties.periods[0].relativeHumidity.value') | |
# Read the temperature value | |
temp=$(cat $filename | jq -r '.properties.periods[0].temperature') | |
echo "INFO: $(date) - Relative humidity: ${rh}, Temp: ${temp}, Operation: ${OPERATION}" | |
# Delete the file | |
rm $filename | |
# run the command if the temperature is higher than 60 and humidity is lower than 65 | |
if [[ $temp -gt "60" ]] && [[ $rh -lt "65" ]]; then | |
echo "INFO: Running command" | |
eval ${OPERATION} | |
else | |
echo "INFO: Skipping command" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment