This page looks best with JavaScript enabled

Weather Widget - Root scripte

 ·  ☕ 7 min read  ·  🤖 SWU
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
#
# temp-curve.sh
# Bash sript by Wüsti (p) 2023-12/2024-01
# This file creates a data file by evaluating the OpenWeather JSON file
# and creates a temparatue curve of the predictions with gnuplot
# like the one the openwethermap widges has.
#
# I use bc to compare temperatur data because the shell commands calculate with
# integers and can't operate with real numbers.
# bc prompts the errorcode, 0 true, 1 false; so if you need that you test
# against that prompt when working non interactive with bc.
# I have a ready made gnuplot ini-file, it has all the commands to create the
# $TEMPCURVE_PNG.
#

BIN_DIR="/root/bin/WeatherWidget"

. $BIN_DIR/WeatherWidget.ini

change () {
                local VAR=${PREDICTION_TEMP_NIGHT[$j]}
                PREDICTION_TEMP_NIGHT[$j]=${PREDICTION_TEMP_DAY[$j]}
                PREDICTION_TEMP_DAY[$j]=$VAR
}

declare -a PREDICTION_TEMP_DAY
declare -a PREDICTION_TEMP_NIGHT

for i in {0..7} # Alle Vorhersagedaten in ein Array einlesen
do
        PREDICTION_TEMP_DAY[$i]=`jq -r .daily[$i].temp.day     $J_SOURCE`
        PREDICTION_TEMP_NIGHT[$i]=`jq -r .daily[$i].temp.night $J_SOURCE`

done

# Hier sorgen wir dafür, dass Gnuplot richtig rum zeichnet
for i in 0 2 4 6 # Jedes Gerade gegen das Folgende testen
do
        j=$((i+1))
        # wenn day jetzt > night jetzt und day morgen > nacht morgen change
        if [[ $( echo "${PREDICTION_TEMP_DAY[$i]} > ${PREDICTION_TEMP_NIGHT[$i]}" | bc ) -ne 0 ]] && [[ $( echo "${PREDICTION_TEMP_DAY[$j]} > ${PREDICTION_TEMP_NIGHT[$j]}" | bc ) -ne 0 ]]
        then
                change
        fi
        # wenn day jetzt < night jetzt und day morgen < nacht morgen change
        if [[ $( echo "${PREDICTION_TEMP_DAY[$i]} < ${PREDICTION_TEMP_NIGHT[$i]}" | bc ) -ne 0 ]] && [[ $( echo "${PREDICTION_TEMP_DAY[$j]} < ${PREDICTION_TEMP_NIGHT[$j]}" | bc ) -ne 0 ]]
        then
                change
        fi
done

rm $D_FILE >/dev/null 2>&1
rm $I_SOURCE_FILE >/dev/null 2>&1

# Datendatei für Gnuplot erzeugen
for i in {0..6} # Alle Daten minus 1, da wir mit $i+1 arbeiten, s .u.
do
        j=$((i+1))
        echo "$i ${PREDICTION_TEMP_DAY[$i]}" >>   $D_FILE
        echo "$i ${PREDICTION_TEMP_NIGHT[$i]}" >> $D_FILE
        echo "$j ${PREDICTION_TEMP_DAY[$j]}" >>   $D_FILE
        echo "$j ${PREDICTION_TEMP_NIGHT[$j]}" >> $D_FILE
        echo "$i ${PREDICTION_TEMP_DAY[$i]}" >>   $D_FILE
        echo "" >>                                $D_FILE
done

# Grafik erzeugen
gnuplot $RC_FILE
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/sh
#

BIN_DIR="/root/bin/WeatherWidget"

. $BIN_DIR/WeatherWidget.ini

# If we get new data, rebuild HTML file
wget "http://api.openweathermap.org/data/2.5/onecall?lat=$MY_LAT&lon=$MY_LON&exclude=minutely,hourly,alerts&units=$MY_UNITS&lang=$MY_LANG&appid=$MY_API_KEY" -O $J_SOURCE
$BIN_DIR/weather-content.sh
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/sh
#

BIN_DIR="/root/bin/WeatherWidget"

. $BIN_DIR/WeatherWidget.ini

# If we get new data, rebuild HTML file
wget "http://api.openweathermap.org/data/2.5/onecall?lat=$MY_LAT&lon=$MY_LON&exc
$BIN_DIR/weather-content.sh
root@uranus:~# cat bin/WeatherWidget/weather-content.sh
#!/bin/bash
#
# weather-content.sh
# Bash Weather-Widget by Sascha Wüstemann (p) 2023-12
# This file evaluates the OpenWeather JSON file and puts the
# data into varibles for current and the predictions
# into an array - then it creates the Weather Widget html file
#

BIN_DIR="/root/bin/WeatherWidget"

. $BIN_DIR/WeatherWidget.ini

if [[ "METRIC" == $MY_UNITS ]]
then
        MY_UNITS="°C"
else
        MY_UNITS="°F"
fi

CURRENT_DAY_TIME=`jq -r .current.dt $J_SOURCE`
TODAY=`date --date="@$CURRENT_DAY_TIME" +%A`
CURRENT_DESCRIPTION=`jq -r .current.weather[].description $J_SOURCE`
CURRENT_TEMP=`jq -r .current.temp $J_SOURCE`
CURRENT_WIND=`jq -r .current.wind_speed $J_SOURCE`
CURRENT_DEG=`jq -r .current.wind_deg $J_SOURCE`
CURRENT_WEATHER_ICON=`jq -r .current.weather[].icon $J_SOURCE`
CURRENT_ICON_FILE="$CURRENT_WEATHER_ICON.png"

degree_me () {
        if [ $T -ge 0 ] && [ $T -le 10 ]
        then
                DEG="Nord"
        elif [ $T -ge 10 ] && [ $T -le 30 ]
        then
                DEG="Nord Nordost"
        elif [ $T -ge 30 ] && [ $T -le 50 ]
        then
                DEG="Nordost"
        elif [ $T -ge 50 ] && [ $T -le 70 ]
        then
                DEG="Ost Nordost"
        elif [ $T -ge 70 ] && [ $T -le 100 ]
        then
                DEG="Ost"
        elif [ $T -ge 100 ] && [ $T -le 120 ]
        then
                DEG="Ost Südost"
        elif [ $T -ge 120 ] && [ $T -le 140 ]
        then
                DEG="Südost"
        elif [ $T -ge 140 ] && [ $T -le 160 ]
        then
                DEG="Süd Südost"
        elif [ $T -ge 160 ] && [ $T -le 190 ]
        then
                DEG="Süd"
        elif [ $T -ge 190 ] && [ $T -le 210 ]
        then
                DEG="Süd Südwest"
        elif [ $T -ge 210 ] && [ $T -le 230 ]
        then
                DEG="Südwest"
        elif [ $T -ge 230 ] && [ $T -le 250 ]
        then
                DEG="West Südwest"
        elif [ $T -ge 250 ] && [ $T -le 280 ]
        then
                DEG="West"
        elif [ $T -ge 280 ] && [ $T -le 300 ]
        then
                DEG="West Nordwest"
        elif [ $T -ge  300 ] && [ $T -le 330 ]
        then
                DEG="Nordwest"
        elif [ $T -ge 330 ] && [ $T -le 340 ]
        then
                DEG="Nord Nordwest"
        elif [ "$T" -ge 340 ] && [ "$T" -le 360 ]
        then
                DEG="Nord"
        else
                DEG="undefiniert $T"
        fi
}

if [ ! -f "$I_SOURCE/$CURRENT_ICON_FILE" ]
then
        wget "https://openweathermap.org/img/wn/$CURRENT_WEATHER_ICON@2x.png" -O "$I_SOURCE/$CURRENT_ICON_FILE" -o /dev/null
        cp "$I_SOURCE/$CURRENT_ICON_FILE" /home/sascha/.rawdog/IMG/
fi

T="$CURRENT_DEG"
degree_me

declare -a PREDICTION_DAY_TIME
declare -a PREDICTION_WEATHER_DESCRIPTION
declare -a PREDICTION_TEMP_MAX
declare -a PREDICTION_TEMP_MIN
declare -a PREDICTION_TEMP_NIGHT
declare -a PREDICTION_WIND_SPEED
declare -a PREDICTION_WIND_GUST
declare -a PREDICTION_WIND_DEG
declare -a PREDICTION_WIND_WAY
declare -a PREDICTION_WEATHER_ICON
declare -a PREDICTION_ICON_FILE
declare -a HTML
declare -a THEN

for i in {0..7}
do
        PREDICTION_DAY_TIME[$i]=`jq -r .daily[$i].dt $J_SOURCE`
        THEN[$i]=`date --date="@${PREDICTION_DAY_TIME[$i]}" +%a`
        PREDICTION_WEATHER_DESCRIPTION[$i]=`jq -r .daily[$i].weather[].description $J_SOURCE`
        PREDICTION_TEMP_MAX[$i]=`jq -r .daily[$i].temp.max $J_SOURCE`
        PREDICTION_TEMP_MIN[$i]=`jq -r .daily[$i].temp.min $J_SOURCE`
        PREDICTION_TEMP_NIGHT[$i]=`jq -r .daily[$i].temp.night $J_SOURCE`
        PREDICTION_WIND_SPEED[$i]=`jq -r .daily[$i].wind_speed $J_SOURCE`
        PREDICTION_WIND_GUST[$i]=`jq -r .daily[$i].wind_gust $J_SOURCE`
        PREDICTION_WIND_DEG[$i]=`jq -r .daily[$i].wind_deg $J_SOURCE`
        PREDICTION_WEATHER_ICON[$i]=`jq -r .daily[$i].weather[].icon $J_SOURCE`
        PREDICTION_ICON_FILE[$i]="${PREDICTION_WEATHER_ICON[$i]}.png"
        if [ ! -f "$I_SOURCE/${PREDICTION_ICON_FILE[$i]}" ]
        then
                wget "https://openweathermap.org/img/wn/${PREDICTION_WEATHER_ICON[$i]}@2x.png" -O "$I_SOURCE/${PREDICTION_ICON_FILE[$i]}"
                cp "$I_SOURCE/${PREDICTION_ICON_FILE[$i]}" /home/sascha/.rawdog/IMG/
        fi
        T="${PREDICTION_WIND_DEG[$i]}"
        degree_me
        PREDICTION_WIND_WAY[$i]=$DEG

        HTML1[$i]="<div class=\"w3-third w3-small w3-round-large w3-center w3-green\" style=\"width: 10%\">"
        HTML0[$i]=" <p>${THEN[$i]}</p>"
        HTML2[$i]=" <p><img src=\"IMG/${PREDICTION_ICON_FILE[$i]}\" style=\"width:75%\"</img>"
        HTML3[$i]=" <p>${PREDICTION_WEATHER_DESCRIPTION[$i]}</br>"
        HTML4[$i]=" ${PREDICTION_TEMP_MIN[$i]} / ${PREDICTION_TEMP_MAX[$i]} °C</br>"
        HTML5[$i]=" ${PREDICTION_TEMP_NIGHT[$i]} $MY_UNITS</br>"
        HTML6[$i]=" ${PREDICTION_WIND_SPEED[$i]} / ${PREDICTION_WIND_GUST[$i]} km/h</br>"
        HTML7[$i]=" aus ${PREDICTION_WIND_WAY[$i]}</p>"
        HTML8[$i]="</div>"

done

HTML1[0]="<div class=\"w3-third w3-small w3-center w3-light-green w3-round-large\" style=\"width: 10%\">"
HTMLOUT=""

for i in {0..7}
do

        HTMLOUT="$HTMLOUT${HTML1[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML0[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML2[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML3[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML4[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML5[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML6[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML7[$i]}
        "
        HTMLOUT="$HTMLOUT${HTML8[$i]}
        "

done

NOW=`date +"%F %T"`
MESSAGE="<p>$MY_MESSAGE</br>$NOW</p>"

# if html file is build, also rebuild curve
$BIN_DIR/temp-curve.sh

cat << EOF > $WWW_DIR/$WWW_FILE
<!DOCTYPE html>
<html lang="$HTML_LANG">
 <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>$HTML_TITLE</title>
   <link rel="stylesheet" href="w3.css">
   <!-- Origin: https://www.w3schools.com/w3css/4/w3.css -->
 </head>
 <body>
  <div class="w3-card-4 w3-text-white w3-round-large w3-teal" style="width: 100%" >
   <div class="w3-row  w3-margin w3-teal w3-round-large">
    <div class="w3-third w3-center w3-lime w3-round-large" style="width: 20%"  >
     <h3>Wetter Wolfenbüttel</br>
         $TODAY</h3>
        <p><img src="IMG/$CURRENT_ICON_FILE"></img></p>
        <p>$CURRENT_DESCRIPTION</br>
        $CURRENT_TEMP $MY_UNITS</br>
        $CURRENT_WIND $MY_WIND_DIR $DEG</br>
        $MESSAGE</p>
    </div>
        $HTMLOUT
        <img src="IMG/$TEMPCURVE_PNG" style="margin-top: 20px; margin-left: 65px; width: 69%;"></img>
   </div>
  </div>
 </body>
</html>
EOF
1
2
3
4
5
6
7
#!/bin/sh

BIN_DIR="/root/bin/WeatherWidget"

. $BIN_DIR/WeatherWidget.ini

wget "https://www.dwd.de/DWD/wetter/aktuell/deutschland/bilder/wx_brd_akt.jpg" -O $I_SOURCE/wx_brd_akt.jpg
1
2
3
4
5
6
7
8
9
#!/bin/sh

BIN_DIR="/root/bin/WeatherWidget"

. $BIN_DIR/WeatherWidget.ini


wget "https://www.dwd.de/DWD/wetter/sat/satwetter/njob_satrad.png" -O $I_SOURCE/njob_satrad.png
wget "https://www.dwd.de/DWD/wetter/radar/rad_nib_akt.jpg" -O $I_SOURCE/rad_nib_akt.jpg
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# One variable data file sourced by WeatherWidget scripts
#
# Openweather.org one-call-3 api variables used by weather-api.sh
# Place your one-call api data here:
MY_API_KEY="your Openweathermap Key"
MY_LANG="your 2 capital letter country code, e.g. DE"
MY_UNITS="your temperature units e.g. METRIC"
MY_LON="XX.XX" # your longitude
MY_LAT="XX.XXXX" # your latitude

# Section to adjust directories and filenames if nescessary
MY_WIND_DIR="km/h aus"
TEMPCURVE_PNG="curve.png"
WWW_DIR="/var/www/html"
WWW_FILE="weather-content.html"
I_SOURCE="$WWW_DIR/IMG"
J_SOURCE="$WWW_DIR/DATA/wolfenbuettel.json"
RC_FILE="$WWW_DIR/DATA/gnuplot.ini"
D_FILE="$WWW_DIR/DATA/curve.data"
MY_MESSAGE="Letzte Aktualisierung:"
HTML_LANG="de"
HTML_TITLE="WETTER WOLFENBÜTTEL"

# do not change
I_SOURCE_FILE="$I_SOURCE/$TEMPCURVE_PNG"

wüsti
WRITTEN BY
SWU
human