nwsForecast: weather icons not loading

Post Reply
danb35
Observer
Observer
Posts: 20
Joined: Sat Jan 20, 2018 4:14 pm
Station model: Davis Pro 2
Software: WeeWX

nwsForecast: weather icons not loading

Post by danb35 » Mon Oct 06, 2025 6:35 pm

I'm not sure if this is a common cause with my other thread, so I've made a new one. The nwsForecast block is loading the forecast data OK, but the images representing the forecast aren't:
Screenshot 2025-10-06 at 14.33.08.png
Screenshot 2025-10-06 at 14.33.08.png (55.7 KiB) Viewed 7038 times
MT 19.0. Where should I be looking for this problem?

FSC830
Forecaster
Forecaster
Posts: 195
Joined: Thu Aug 02, 2018 11:40 am
Station model: Davis Vantage Pro2
Software: Meteobridge

Re: nwsForecast: weather icons not loading

Post by FSC830 » Wed Oct 08, 2025 6:09 am

Hi,
I dont use this block, but it looks like the path to the icons is not correct.
Or it is may be an issue at browser side.

Oh, just seen in your other post that you did a new install, so may be here are some PHP8 issues possible to.

Regards
Image

danb35
Observer
Observer
Posts: 20
Joined: Sat Jan 20, 2018 4:14 pm
Station model: Davis Pro 2
Software: WeeWX

Re: nwsForecast: weather icons not loading

Post by danb35 » Wed Oct 08, 2025 10:10 am

Well, I'm pretty sure this is at least part of the problem--it looks as though the icons are loaded via http. From line 179 of nwsForecastBlock.php:

Code: Select all

							<img src="http://forecast.weather.gov/<?php echo $iconsSevenDay[1][$i]?>" class="nwsForecastIconImg">
Since I serve my site via HTTPS, this is likely to be a problem. Maybe not the whole problem, but almost certainly a problem.

But that doesn't seem to be everything; the "echo $iconsSevenDay" part doesn't seem to be returning anything. Here's what's in the HTML:

Code: Select all

<img src="http://forecast.weather.gov/" class="nwsForecastIconImg">
Edit: Well, the block's change log shows the last update in early 2018, so that may be the rest of the problem.

Edit 2: The current block operates by scraping HTML downloaded from https://forecast.weather.gov. I don't know if it was available in early 2018 when the block was last updated, but NWS now has an API:
https://www.weather.gov/documentation/services-web-api

A query to https://api.weather.gov/points/(lat),(long) will return a JSON object including (among other things) a URI for the 7-day forecast; a query to that URI will return the entire forecast, again as a JSON object. All the data currently displayed by the block is there, including HTTPS URLs to the icons. Seems it ought to be pretty straightforward to use this for the block rather than the HTML scrape, but that's easy for me to say.

danb35
Observer
Observer
Posts: 20
Joined: Sat Jan 20, 2018 4:14 pm
Station model: Davis Pro 2
Software: WeeWX

Re: nwsForecast: weather icons not loading

Post by danb35 » Wed Oct 08, 2025 11:55 am

ChatGPT came up with this as code to retrieve and parse the NWS API:

Code: Select all

// Fetch grid and forecast URL from NWS API
$pointsUrl = "https://api.weather.gov/points/{$lat},{$lon}";
$ch = curl_init($pointsUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "MyWeatherApp (youremail@example.com)"); // required by NWS
$pointsResponse = curl_exec($ch);
curl_close($ch);

if ($pointsResponse === false) {
    die("Error retrieving point data");
}

$pointsData = json_decode($pointsResponse, true);
if (!isset($pointsData['properties']['forecast'])) {
    die("No forecast URL found for this location");
}

$forecastUrl = $pointsData['properties']['forecast'];

// Now fetch the forecast itself
$ch = curl_init($forecastUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "MyWeatherApp (youremail@example.com)");
$forecastResponse = curl_exec($ch);
curl_close($ch);

if ($forecastResponse === false) {
    die("Error retrieving forecast data");
}

$forecastData = json_decode($forecastResponse, true);
if (!isset($forecastData['properties']['periods'])) {
    die("No forecast periods found");
}

$periods = $forecastData['properties']['periods'];
$data = [];

// Build the $data array to match your existing structure
foreach ($periods as $period) {
    $data[] = [
        'time'        => $period['name'],
        'temp'        => $period['temperature'] . ' ' . $period['temperatureUnit'],
        'forecast'    => $period['shortForecast'],
        'wind'        => $period['windDirection'] . ' ' . $period['windSpeed'],
        'detailed'    => $period['detailedForecast']
    ];
}
It's going to take some work yet to integrate it into the rest of the block though.

danb35
Observer
Observer
Posts: 20
Joined: Sat Jan 20, 2018 4:14 pm
Station model: Davis Pro 2
Software: WeeWX

Re: nwsForecast: weather icons not loading

Post by danb35 » Fri Oct 10, 2025 10:49 am

Almost have it:
Screenshot 2025-10-10 at 06.48.33.png
Screenshot 2025-10-10 at 06.48.33.png (143.28 KiB) Viewed 6923 times
Just need to work a little more on parsing that valid date range.

danb35
Observer
Observer
Posts: 20
Joined: Sat Jan 20, 2018 4:14 pm
Station model: Davis Pro 2
Software: WeeWX

Re: nwsForecast: weather icons not loading

Post by danb35 » Fri Oct 10, 2025 12:28 pm

All right, done, at least as far as I can tell. The updated block is here:
https://github.com/danb35/MTBlocks/tree ... wsForecast

User avatar
Gus
Observer
Observer
Posts: 10
Joined: Sun Aug 20, 2017 11:17 pm
Location: Halethorpe, Maryland
Station model: Davis Vantage Pro 2+
Software: Weather Display
Contact:

Re: nwsForecast: weather icons not loading

Post by Gus » Sat Oct 11, 2025 12:14 am

Updated NWS forecast block working well.
Thank you
Davis VP2+, LabJack w/temp sensors & 1-Wire lightning counter on Trigkey G4 Mini PC, 12th Gen Intel Alder Lake N95 Processor, 16GB SDRAM, and 500GB SSD, Windows11
Image

Post Reply