#!/usr/bin/env bash # # Fetches current weather for Herzogenbuchsee, Switzerland via open-meteo.com. # No API key required. # # WMO weather codes: https://open-meteo.com/en/docs#weathervariables # Coordinates for Herzogenbuchsee, CH LAT="47.1897" LON="7.7058" # NOTE: MeteoSwiss ICON-CH1 model (~1 km resolution) via open-meteo, matching # the MeteoSwiss mobile app data source. API_URL="https://api.open-meteo.com/v1/forecast" MODEL="meteoswiss_icon_ch1" . ~/bin/i3blocks/_utils # Map WMO weather interpretation codes to icons (Nerd Font nf-weather-*) wmo_icon() { local code=$1 case $code in 0) echo "clear" ;; # Clear sky 1) echo "mostly clear" ;; # Mainly clear 2) echo "partly cloudy" ;; # Partly cloudy 3) echo "overcast" ;; # Overcast 45|48) echo "fog" ;; # Fog / depositing rime fog 51|53|55) echo "drizzle" ;; # Drizzle: light, moderate, dense 56|57) echo "freezing drizzle" ;; # Freezing drizzle: light, dense 61|63|65) echo "rain" ;; # Rain: slight, moderate, heavy 66|67) echo "freezing rain" ;; # Freezing rain: light, heavy 71|73|75|77) echo "snow" ;; # Snow: slight, moderate, heavy, snow grains 80|81|82) echo "showers" ;; # Rain showers: slight, moderate, violent 85|86) echo "snow showers" ;; # Snow showers: slight, heavy 95) echo "thunderstorm" ;; # Thunderstorm 96|99) echo "thunderstorm+hail" ;; # Thunderstorm with hail: slight, heavy *) echo "?" ;; esac } weather() { local response response=$(curl -sf "${API_URL}?latitude=${LAT}&longitude=${LON}¤t=temperature_2m,apparent_temperature,weather_code,wind_speed_10m&daily=sunrise,sunset&wind_speed_unit=kmh&timezone=Europe%2FZurich&models=${MODEL}") if [[ $? -ne 0 || -z "$response" ]]; then echo " N/A" exit 0 fi local temp apparent code wind sunrise sunset temp=$(echo "$response" | jq -r '.current.temperature_2m') apparent=$(echo "$response" | jq -r '.current.apparent_temperature') code=$(echo "$response" | jq -r '.current.weather_code') wind=$(echo "$response" | jq -r '.current.wind_speed_10m') sunrise=$(echo "$response" | jq -r '.daily.sunrise[0]') sunset=$(echo "$response" | jq -r '.daily.sunset[0]') # Compare current time against sunrise/sunset (format: YYYY-MM-DDTHH:MM) local now sunrise_epoch sun_label sun_time now=$(date +%s) sunrise_epoch=$(date -d "$sunrise" +%s) if [[ $now -lt $sunrise_epoch ]]; then sun_label="" sun_time=$(date -d "$sunrise" +%H:%M) else sun_label="" sun_time=$(date -d "$sunset" +%H:%M) fi local label label=$(wmo_icon "$code") local bg fg stat if [[ $(echo "$temp < 0" | bc -l) == "1" ]]; then bg=$frost_bg fg=$frost_fg elif [[ $(echo "$temp < 8" | bc -l) == "1" ]]; then bg=$cold_bg fg=$cold_fg elif [[ $(echo "$temp >= 28" | bc -l) == "1" ]]; then bg=$hot_bg fg=$hot_fg elif [[ $(echo "$temp >= 22" | bc -l) == "1" ]]; then bg=$warm_bg fg=$warm_fg fi # NOTE: apparent temperature shown in parentheses as "feels like" stat="$label $(printf '%.0f' "$temp")°C ($(printf '%.0f' "$apparent")°C) $(printf '%.0f' "$wind") km/h $sun_label $sun_time" if [[ -n "$bg" ]]; then echo -e "OUT $stat " else echo -e "OUT $stat " fi } weather