Creating a simple wind forecast bot in Mastodon
Tags: bash, mastodon, weather, scripting
Published: 2023-10-06 12:28:03

---
title: Creating a simple wind forecast bot in Mastodon
published: true
description: Using simple bash scripting and open-meteo so to publish wind forecast in Mastodon.
tags: bash, mastodon, weather, scripting
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njcycpif9zkerjcwds8f.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-10-06 12:04 +0000
---

I'm keeping a manual log (manual like in pencil and paper) of weather forecast in the mountain I like the most (Sierra Nevada). At some point I got interested in building scripts to help me out and I started sharing that information in Mastodon [using a bot](https://mastodon.green/@SierraNevada).

![illustration of Mastodon mascot walking on Sierra Nevada](https://files.mastodon.green/accounts/avatars/109/104/321/132/142/990/original/f504c7af8b558074.png)

First of all, a note on Mastodon bots. They are easy to set up! specially using `toot` command. I created an account, added the specification that says it's a bot, configured the authorization in `toot` and got it running. Once I got there I created a simple weather parser so to be able to toot automatically the weather forecast in Mastodon.

Then I started to learn about wind and I got interested in getting wind details easily so to note them down. I asked around my community and I got an answer: [open-meteo](https://open-meteo.com).

While it has it's limitations it worked perfectly for my purpose, a script to help me gather wind details through the day.
![gif of a puppy being hit by the wind](https://media.giphy.com/media/U4hWYCZo9QzWU/giphy-downsized-large.gif)

First of all I used the interactive API to get the info that I wanted (Sierra Nevada, one day, wind details) into a CSV:

```
curl "https://api.open-meteo.com/v1/forecast?latitude=37.095&longitude=-3.3969&hourly=temperature_2m,windspeed_180m&forecast_days=1&format=csv" > mywindtoday.csv
```

Then I counted the total lines of the file:

```
lines=$(wc -l mywindtoday.csv | cut -d ' ' -f1|tr '\n' ' '
```

Also I created a variable for the half of the values line, discarding the first description lines (which are 5):

```
mdline=$((($lines/2)+5))
```
Between the noon (first line, 00:00) and the half of the file is morning, so I created a variable for morning:

```
mnline=$(( ($mdline/2)+5 ))
```
 
And between the half and the end it's evening, so I created another variable for that:

```
eveline=$(( (($lines-$mdline)/2) + $mdline ))
```

The first noon values That I want are hour and wind, so I parse the CSV looking for those values in the key daytimes I estimated before:

```
midnight=$(csvcut -c 1,3 mywindtoday.csv | sed '5!d')
midnightdate=${midnight%,*}
midnighthour=${midnightdate#*T}
midnightwind=${midnight#*,}

morning=$(csvcut -c 1,3 mywindtoday.csv | head -n $mnline | tail -1)
morningdate=${morning%,*}
morninghour=${morningdate#*T}
morningwind=${morning#*,}

afternoon=$(csvcut -c 1,3 mywindtoday.csv | head -n $mdline | tail -1)
afternoondate=${afternoon%,*}
afternoonhour=${afternoondate#*T}
afternoonwind=${afternoon#*,}

evening=$(csvcut -c 1,3 mywindtoday.csv | head -n $eveline | tail -1)
evedate=${evening%,*}
evehour=${evedate#*T}
evewind=${evening#*,}

night=$(csvcut -c 1,3 mywindtoday.csv | head -n $lines | tail -1)
ngtdate=${night%,*}
ngthour=${ngtdate#*T}
```

Now all I had to do was save it all into a text document in a readable format:

```
touch mywindtoot

echo "Detalles del viento en Sierra Nevada" >> mywindtoot
echo "A las $midnighthour de hoy,  $midnightwind k/h" >> mywindtoot
echo "A las $morninghour de hoy,  $morningwind k/h" >> mywindtoot
echo "A las $afternoonhour de hoy,  $afternoonwind k/h">> mywindtoot
echo "A las $evehour de hoy, $evewind k/h">> mywindtoot
echo "A las $ngthour de hoy, $ngtwind k/h">> mywindtoot
```

And, if I wanted to toot it:

```
toot post "$(cat mywindtoot)"
```

Or if I just wanted to read it in the terminal, I just `cat` it. 

```
Detalles del viento en Sierra Nevada
A las 00:00 de hoy,  15.8 k/h
A las 09:00 de hoy,  8.0 k/h
A las 14:00 de hoy,  19.5 k/h
A las 18:00 de hoy, 2.2 k/h
A las 23:00 de hoy, 14.1 k/h

```

Finally a little clean up:

```
rm mywindtoot
rm mywindtoday
```

And that's it! [My first test using it in Mastodon](https://mastodon.green/@SierraNevada/111187890324269047) worked just fine. You can check the whole code [here](https://git.sr.ht/~alienagain/SierraNevada/tree/master/item/src/wind.sh). Why don't you try to build something similar for your city? Maybe share with your neighborhood.