Weather info on Tint2

I was looking for a way to display some weather info on the tint panel. This is what it looks like on mine:

image

Below I will share a script and the basic steps I took to make this happen.

First I obtained a FREE openweather API key (Pricing - OpenWeatherMap, click on get API key, create account etc.)

Then I opened Tint2 config
Menu => Mabox Config => Tint2 Panels => Configure (GUI) .tint2rc
Added an Executor in the [Panels items] section. The Executor will receive also a number, one for first, two for second and so on
In the left column the [Executor ] was added, selected that one and changed some of the parameters to something like this:

image
Other settings like font and color is up to you

Important is the path to the weather script (were you saved it!), Interval (free API allows for 60 calls every minute but the script will run about 2 seconds (on my rig that is) so I advise to set it higher than 2). Interval is in seconds. I have set it to run once every two minutes (120 seconds). Deselect Show Icon and Cache Icon and tick the Markup selection box

The weather script:

The script allows for one command line argument, you may choose one of these;
simple => shows temp current weather
future => shows temps current + upcoming weather (+ arrow to indicate if temp goes up,down or stays the same)
full => shows temps on current + upcoming + indication about either “sun down” or " sun up" in xx:xx hours (if your local time is past “sun down” it displays time to sunrise and vice-versa
When the option is invalid, no output will be shown ( :crossed_fingers:)

Script depends on:
OpenWeatherMap API Key (Free), mandatory!
weather-icons (AUR => ttf-weather-icons) but if you want you could use another font and copy/paste the glyph’s into the script!
jq
curl

Inside the scripts also directions are given on what info to add like KEY and City. Some are optional.

Save below script to a suitable place and give it a name. Make sure it’s executable (chmod +x )

#!/bin/sh

#-----------------------------------------------------------------------#
# Forked from original script (unknown creator) for Mabox by BootZ
# added some simple command options to display weather in different ways

# Script depends on:
# OpenWeatherMap API Key (Free, get it here )
# weather-icons (AUR => ttf-weather-icons)
# jq
# curl

# commandline options either one of the following:
# simple       => shows temp current weather
# future       => shows temps on current + predicted weather (3 hours ahead)
#                 (+ arrow to indicate if temp goes up,down or the same)
# full         => shows temps on current + predicted + indication about 
#                 either "sun down" or "sun up" in xx:xx hours


#-----------------------------------------------------------------#
# This routine converts number, given by openweather, into glyph  #
#-----------------------------------------------------------------#
get_icon() {
    
    case $1 in

        01d) icon="";;
        01n) icon="";;
        02d) icon="";;
        02n) icon="";;
        03*) icon="";;
        04*) icon="";;
        09d) icon="";;
        09n) icon="";;
        10d) icon="";;
        10n) icon="";;
        11d) icon="";;
        11n) icon="";;
        13d) icon="";;
        13n) icon="";;
        50d) icon="";;
        50n) icon="";;
        *) icon="";
 
    esac

    echo $icon
}


get_duration() {

    osname=$(uname -s)

    case $osname in
        *BSD) date -r "$1" -u +%H:%M;;
        *) date --date="@$1" -u +%H:%M;;
    esac

}

# KEY:
# Mandatory, without no info can be retrieved! A free key 
# can be obtained at https://openweathermap.org/api
#
# CITY:
# If City is empty the script wil try to retrieve current network 
# location via Mozilla API. CITY can either be a city ID, city name or 
# city name + countrycode
# EXAMPLES:
# ID for city Groningen = 2755251
# City Name = Groningen
# OR City Name,Countrycode = Groningen,NL
#
# UNITS:
# Can be metric, imperial or standard. If left empty standard is default
#
# SYMBOL:
# Choose your own symbol like ° for degrees, maybe ℉ of plain F for
# fahrenheit. Can be empty if desired

KEY="your (free) API key"
CITY="Groningen"
UNITS="metric"
SYMBOL="°"

# We talk towards API openweathermap
API="https://api.openweathermap.org/data/2.5"

if [ -n "$CITY" ]; then
    if [ "$CITY" -eq "$CITY" ] 2>/dev/null; then
        CITY_PARAM="id=$CITY"
    else
        CITY_PARAM="q=$CITY"
    fi

    current=$(curl -sf "$API/weather?appid=$KEY&$CITY_PARAM&units=$UNITS")
    forecast=$(curl -sf "$API/forecast?appid=$KEY&$CITY_PARAM&units=$UNITS&cnt=1")
else
    location=$(curl -sf https://location.services.mozilla.com/v1/geolocate?key=geoclue)

    if [ -n "$location" ]; then
        location_lat="$(echo "$location" | jq '.location.lat')"
        location_lon="$(echo "$location" | jq '.location.lng')"

        current=$(curl -sf "$API/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS")
        forecast=$(curl -sf "$API/forecast?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS&cnt=1")
    fi
fi

# Do we have information lookup completed
if [ -n "$current" ] && [ -n "$forecast" ]; then
    
    current_temp=$(echo "$current" | jq ".main.temp" | cut -d "." -f 1)
    current_icon=$(echo "$current" | jq -r ".weather[0].icon")

    forecast_temp=$(echo "$forecast" | jq ".list[].main.temp" | cut -d "." -f 1)
    forecast_icon=$(echo "$forecast" | jq -r ".list[].weather[0].icon")


    if [ "$current_temp" -gt "$forecast_temp" ]; then
        trend=""
    elif [ "$forecast_temp" -gt "$current_temp" ]; then
        trend=""
    else
        trend=""
    fi

    sun_rise=$(echo "$current" | jq ".sys.sunrise")
    sun_set=$(echo "$current" | jq ".sys.sunset")
    now=$(date +%s)

    if [ "$sun_rise" -gt "$now" ]; then
        daytime=" $(get_duration "$((sun_rise-now))")"
    elif [ "$sun_set" -gt "$now" ]; then
        daytime=" $(get_duration "$((sun_set-now))")"
    else
        daytime=" $(get_duration "$((sun_rise-now))")"
    fi
    
fi

cmdlnopt=$(echo "$1" | tr '[:upper:]' '[:lower:]')
    
if [ "$cmdlnopt" == "simple" ]; then
  echo "$(get_icon "$current_icon") $current_temp$SYMBOL"
elif [ "$cmdlnopt" == "future" ]; then
  echo "$(get_icon "$current_icon") $current_temp$SYMBOL  $trend  $(get_icon "$forecast_icon") $forecast_temp$SYMBOL"
elif [ "$cmdlnopt" == "full" ]; then
  echo "$(get_icon "$current_icon") $current_temp$SYMBOL  $trend  $(get_icon "$forecast_icon") $forecast_temp$SYMBOL  $daytime"
fi

Do with it as you please and share improvements!!

Cheers,

BootZ

6 Likes

I did see an issue on my panel having the wrong glyphs shown. You can easily correct this by setting the font for the executor to Weather-Icons font!

2 Likes