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 d885f36da7969b6bddacbef3f541a2dc5eff1cb3

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
32 return 0
33end
34
35# ensure cache_dir exists
36mkdir -p $cache_dir
37
38# use cached data if less than $cache_max_age old
39set -l current_time (date +%s)
40if test -f $cache_file
41 set -l file_age (math $current_time - (stat -c %Z $cache_file))
42 if test $file_age -lt $cache_max_age
43 set -l wxdata (cat $cache_file | string split ',')
44 print_weather $wxdata; and return 0
45 end
46end
47
48# fetch fresh data
49set -l wxjson (curl --max-time 2 -sL --compressed \
50 "https://swd.weatherflow.com/swd/rest/better_forecast?station_id=$station&token=$token&units_temp=f")
51
52if test $status -eq 0; and echo $wxjson | jq empty >/dev/null 2>&1
53 set -l wxdata (echo $wxjson | jq -r '[
54 (.current_conditions.conditions | ascii_downcase),
55 (.current_conditions.air_temperature | round),
56 (.forecast.daily[0].air_temp_low | round),
57 (.forecast.daily[0].air_temp_high | round)
58 ] | @csv' | tr -d '"' | string split ',')
59 if test (count $wxdata -eq 4)
60 string join ',' $wxdata >$cache_file
61 print_weather $wxdata; and return 0
62 end
63end
64