john

john / Fish Fetch Tempest Weather

Last active 5 months ago

Like 0

Hasty fish script to fetch basic weather data (conditions, temperature, low/high temperature forecast) from a Weatherflow Tempest station

Revision 617d00f166536cde488ed281da561208477e44d2

fish_fetch_tempest_weather.fish Raw
1#!/usr/bin/env fish
2# vim: ft=fish
3
4# set vars
5set -l station 000000 # replace with your station ID
6set -l token 00000000-0000-0000-0000-000000000000 # replace with API token
7set -l cache_dir "$XDG_CACHE_HOME/fish"
8set -l cache_file "$cache_dir/wx"
9set -l cache_max_age 3600 # 1 hour in seconds
10
11function print_weather
12 # print weather
13 set -l wxdata $argv
14 if test (count $wxdata) -ne 4
15 return 1
16 end
17 set_color -d white
18 echo -ne "\nwx@home: "
19 set_color green
20 echo -n "$wxdata[1] and $wxdata[2]°f "
21 set_color white
22 echo -n "("
23 set_color blue
24 echo -n "$wxdata[3]°"
25 set_color white
26 echo -n "|"
27 set_color red
28 echo -n "$wxdata[4]°"
29 set_color white
30 echo ")"
31 set_color normal
32end
33
34# ensure cache_dir exists
35mkdir -p $cache_dir
36
37# use cached data if less than $cache_max_age old
38set -l current_time (date +%s)
39if test -f $cache_file
40 set -l file_age (math $current_time - (stat -c %Z $cache_file))
41 if test $file_age -lt $cache_max_age
42 set -l wxdata (cat $cache_file | string split ',')
43 print_weather $wxdata; and exit 0
44 end
45end
46
47# fetch fresh data
48set -l wxjson (curl --max-time 2 -sL --compressed \
49 "https://swd.weatherflow.com/swd/rest/better_forecast?station_id=$station&token=$token&units_temp=f")
50
51if test $status -eq 0; and echo $wxjson | jq empty >/dev/null 2>&1
52 set -l wxdata (echo $wxjson | jq -r '[
53 (.current_conditions.conditions | ascii_downcase),
54 (.current_conditions.air_temperature | round),
55 (.forecast.daily[0].air_temp_low | round),
56 (.forecast.daily[0].air_temp_high | round)
57 ] | @csv' | tr -d '"' | string split ',')
58 if test (count $wxdata -eq 4)
59 string join ',' $wxdata >$cache_file
60 print_weather $wxdata; and exit 0
61 end
62end
63