- type: custom-api title: ☀️ Sunrise & Sunset body-type: string cache: 12h options: location: Quito, Ecuador template: | {{ $timeLayout := "3:04 PM" }} {{/* Step 1: geocode the location */}} {{ $loc := replaceAll " " "%20" (.Options.StringOr "location" "") }} {{ $geoUrl := printf "https://geocoding-api.open-meteo.com/v1/search?name=%s&count=1&language=en&format=json" $loc }} {{ $geo := newRequest $geoUrl | getResponse }} {{ $results := $geo.JSON.Array "results" }} {{ if eq (len $results) 0 }}

Location "{{ .Options.StringOr "location" "" }}" not found.

{{ else }} {{ $lat := $geo.JSON.String "results.0.latitude" }} {{ $lon := $geo.JSON.String "results.0.longitude" }} {{/* Step 2: today's sunrise/sunset/daylight from Open-Meteo */}} {{ $fcUrl := printf "https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s&daily=sunrise,sunset,daylight_duration&forecast_days=1&timezone=auto" $lat $lon }} {{ $fc := newRequest $fcUrl | getResponse }} {{ $sunriseRaw := $fc.JSON.String "daily.sunrise.0" }} {{ $sunsetRaw := $fc.JSON.String "daily.sunset.0" }} {{ $daylight := $fc.JSON.Int "daily.daylight_duration.0" }} {{ if or (eq $sunriseRaw "") (eq $sunsetRaw "") }}

No sunrise/sunset today (polar region).

{{ else }} {{ $sunrise := $sunriseRaw | parseTime "2006-01-02T15:04" }} {{ $sunset := $sunsetRaw | parseTime "2006-01-02T15:04" }} {{ $hours := div $daylight 3600 }} {{ $mins := div (mod $daylight 3600) 60 }}

{{ $sunrise.Format $timeLayout }}

Sunrise

{{ $hours }}h {{ $mins }}m

Daylight

{{ $sunset.Format $timeLayout }}

Sunset

{{ end }} {{ end }}