From a314ea1aa3036f81c6e802b669427af7538a772d Mon Sep 17 00:00:00 2001 From: buxxi Date: Sun, 18 Aug 2019 12:44:28 +0200 Subject: [PATCH] Fixing weatherforecast module not displaying rain amount if using fallback endpoint --- CHANGELOG.md | 1 + .../weatherforecast/weatherforecast.js | 35 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0278c75..19616f46 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Updatenotification module: Properly handle race conditions, prevent crash. - Send `NEWS_FEED` notification also for the first news messages which are shown - Fixed issue where weather module would not refresh data after a network or API outage [#1722](https://github.com/MichMich/MagicMirror/issues/1722) +- Fixed weatherforecast module not displaying rain amount on fallback endpoint ## [2.8.0] - 2019-07-01 diff --git a/modules/default/weatherforecast/weatherforecast.js b/modules/default/weatherforecast/weatherforecast.js index b6b57d8c..c4d43ede 100644 --- a/modules/default/weatherforecast/weatherforecast.js +++ b/modules/default/weatherforecast/weatherforecast.js @@ -353,7 +353,7 @@ Module.register("weatherforecast",{ icon: this.config.iconTable[forecast.weather[0].icon], maxTemp: this.roundValue(forecast.temp.max), minTemp: this.roundValue(forecast.temp.min), - rain: forecast.rain + rain: this.processRain(forecast, data.list) }; this.forecast.push(forecastData); @@ -434,5 +434,38 @@ Module.register("weatherforecast",{ roundValue: function(temperature) { var decimals = this.config.roundTemp ? 0 : 1; return parseFloat(temperature).toFixed(decimals); + }, + + /* processRain(forecast, allForecasts) + * Calculates the amount of rain for a whole day even if long term forecasts isn't available for the appid. + * + * When using the the fallback endpoint forecasts are provided in 3h intervals and the rain-property is an object instead of number. + * That object has a property "3h" which contains the amount of rain since the previous forecast in the list. + * This code finds all forecasts that is for the same day and sums the amount of rain and returns that. + */ + processRain: function(forecast, allForecasts) { + //If the amount of rain actually is a number, return it + if (!isNaN(forecast.rain)) { + return forecast.rain; + } + + //Find all forecasts that is for the same day + var checkDateTime = (!!forecast.dt_txt) ? moment(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss") : moment(forecast.dt, "X"); + var daysForecasts = allForecasts.filter(function(item) { + var itemDateTime = (!!item.dt_txt) ? moment(item.dt_txt, "YYYY-MM-DD hh:mm:ss") : moment(item.dt, "X"); + return itemDateTime.isSame(checkDateTime, "day") && item.rain instanceof Object; + }); + + //If no rain this day return undefined so it wont be displayed for this day + if (daysForecasts.length == 0) { + return undefined; + } + + //Summarize all the rain from the matching days + return daysForecasts.map(function(item) { + return Object.values(item.rain)[0]; + }).reduce(function(a, b) { + return a + b; + }, 0); } });