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

9 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

Working out like a charm!..Thank you so much!


2 Likes

Works here also. Many thanks!!!

2 Likes

Nothing came out for me hehe I did the tutorial to the letter

Hi @everardo17,

Would it be possible you do the following to test?

  1. open terminal
  2. cd to folder with the script
  3. run the script from prompt with the for example the simple command behind it?
    for example: ./weather.sh simple

if this generates output in the correct form then it is something in your settings in the Tint2 panel. If not, I’m hoping it is throwing a understandable error on screen.

Hi @everardo17,

Another question: did you make the script executable?

1 Like

yes but i couldn’t get it to work

Okay, what happens when you start the script from the terminal with, for example, the simple statement behind it?


I don’t know what else is next I run the script and nothing happens

PATH is wrong.
~ means home directory
What you wrote in panel config dialog makes no sense.
Use either:

~

or

/home/everardo17/

Some links that may be a help to learn basics.

Hi @everardo17 ,

Your path to the script looks incorrect to me. The ~/ already points to your home folder. I think the path command should look like this: ~/scripts/wheather.sh simple
I could be wrong of course

Also make sure the name of the script is correct but you know best what you called it :slight_smile:

I just tried again the complete steps on my newly installed system and it works correctly.

The temperature comes out but a simple nut, nothing more

I already corrected it /home/everardo17/wheather.sh simple but it shows temperature and a gear or nut but your script didn’t work I’ll stick with this hehehehehe

@everardo17 We have progress :slight_smile:

Did you remove the city example (Groningen) and added your own city (or blank for no city and autodetect)?

Yes, that’s very informative and good!

The path is different as I was checking out , for @bootz 's script in my case the path is : /home/mabox/.config/tint2/scripts/weather.sh so it works out perfectly correct /(Spanish) la ruta del script de @bootz weather.sh es diferente a la de tu sistema, en mi caso este script esta en mi sistema mabox en esta ruta: /home/mabox/.config/tint2/scripts/weather.sh y desde allí opera eficientemente dentro del panel tint2…
I will leave a pic of my script just you cjeck this out / Te dejare una imagen de mi script para que lo revises. Benjamin.

1 Like