Tidy up precipitation (#3023)

Fixes #2953

This is an attempt to fix the issue with precipitation amount and
percentage mixup. I have created a separate
`precipitationPercentage`-variable where the probability of rain can be
stored.

The config options now has the old `showPrecipitationAmount` in addition
to a new setting: `showPrecipitationProbability` (shows the likelihood
of rain).

<details>
  <summary>Examples</summary>
  
  ### Yr

I tested the Yr weather provider for a Norwegian city Bergen that has a
lot of rain. I have removed properties that are irrelevant for this demo
from the config-samples below.

Config:
  ```js
{
	module: "weather",
	config: {
			weatherProvider: "yr",
			type: "current",
			showPrecipitationAmount: true,
			showPrecipitationProbability: true
	}
},
{
	module: "weather",
	config: {
			weatherProvider: "yr",
			type: "hourly",
			showPrecipitationAmount: true,
			showPrecipitationProbability: true
	}
},
{
	module: "weather",
	config: {
			weatherProvider: "yr",
			type: "daily",
			showPrecipitationAmount: true,
			showPrecipitationProbability: true
	}
}
  ```

Result:<br/>
<img width="444" alt="screenshot"
src="https://user-images.githubusercontent.com/34011212/216775423-4e37345c-f915-47e5-8551-7c544ebd24b1.png">

</details>

---------

Co-authored-by: Magnus Marthinsen <magmar@online.no>
Co-authored-by: Veeck <github@veeck.de>
This commit is contained in:
Magnus 2023-02-04 19:02:55 +01:00 committed by GitHub
parent 42d42ef452
commit bf279d9a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 265 additions and 192 deletions

View File

@ -27,6 +27,8 @@ _This release is scheduled to be released on 2023-04-01._
- Use develop as target branch for dependabot
- Update issue template and contributing doc
- The weather modules clearly separates precipation amount and probability (risk of rain/snow)
- This requires all providers that only supports probability to change the config from `showPrecipitationAmount` to `showPrecipitationProbability`.
- Update weather tests
- Changed updatenotification module for MagicMirror repo only: Send only notifications for `master` if there is a tag on a newer commit
- Update dates in Calendar widgets every minute

View File

@ -54,16 +54,21 @@
</div>
{% endif %}
</div>
{% if (config.showFeelsLike or config.showPrecipitationAmount) and not config.onlyTemp %}
{% if (config.showFeelsLike or config.showPrecipitationAmount or config.showPrecipitationProbability) and not config.onlyTemp %}
<div class="normal medium feelslike">
{% if config.showFeelsLike %}
<span class="dimmed">
{{ "FEELS" | translate({DEGREE: current.feelsLike() | roundValue | unit("temperature") | decimalSymbol }) }}
</span>
</span><br/>
{% endif %}
{% if config.showPrecipitationAmount %}
{% if config.showPrecipitationAmount and current.precipitationAmount %}
<span class="dimmed">
{{ "PRECIP" | translate }} {{ current.precipitation | unit("precip") }}
<span class="precipitationLeadText">{{ "PRECIP_AMOUNT" | translate }}</span> {{ current.precipitationAmount | unit("precip", current.precipitationUnits) }}
</span><br/>
{% endif %}
{% if config.showPrecipitationProbability and current.precipitationProbability %}
<span class="dimmed">
<span class="precipitationLeadText">{{ "PRECIP_POP" | translate }}</span> {{ current.precipitationProbability }}%
</span>
{% endif %}
</div>

View File

@ -23,15 +23,14 @@
{{ f.minTemperature | roundValue | unit("temperature") | decimalSymbol }}
</td>
{% if config.showPrecipitationAmount %}
{% if f.precipitationUnits %}
<td class="align-right bright precipitation">
{{ f.precipitation }}{{ f.precipitationUnits }}
</td>
{% else %}
<td class="align-right bright precipitation">
{{ f.precipitation | unit("precip") }}
<td class="align-right bright precipitationAmount">
{{ f.precipitationAmount | unit("precip", f.precipitationUnits) }}
</td>
{% endif %}
{% if config.showPrecipitationProbability %}
<td class="align-right bright precipitationProbability">
{{ f.precipitationProbability }}%
</td>
{% endif %}
</tr>
{% set currentStep = currentStep + 1 %}

View File

@ -11,15 +11,14 @@
{{ hour.temperature | roundValue | unit("temperature") }}
</td>
{% if config.showPrecipitationAmount %}
{% if hour.precipitationUnits %}
<td class="align-right bright precipitation">
{{ hour.precipitation }}{{ hour.precipitationUnits }}
</td>
{% else %}
<td class="align-right bright precipitation">
{{ hour.precipitation | unit("precip") }}
<td class="align-right bright precipitationAmount">
{{ hour.precipitationAmount | unit("precip", hour.precipitationUnits) }}
</td>
{% endif %}
{% if config.showPrecipitationProbability %}
<td class="align-right bright precipitationProbability">
{{ hour.precipitationProbability }}%
</td>
{% endif %}
</tr>
{% set currentStep = currentStep + 1 %}

View File

@ -230,12 +230,7 @@ WeatherProvider.register("envcanada", {
const foreGroup = ECdoc.querySelectorAll("siteData forecastGroup forecast");
// For simplicity, we will only accumulate precipitation and will not try to break out
// rain vs snow accumulations
weather.rain = null;
weather.snow = null;
weather.precipitation = null;
weather.precipitationAmount = null;
//
// The EC forecast is held in a 12-element array - Elements 0 to 11 - with each day encompassing
@ -343,9 +338,7 @@ WeatherProvider.register("envcanada", {
this.setMinMaxTemps(weather, foreGroup, stepDay, true, currentTemp);
weather.rain = null;
weather.snow = null;
weather.precipitation = null;
weather.precipitationAmount = null;
this.setPrecipitation(weather, foreGroup, stepDay);
@ -402,8 +395,7 @@ WeatherProvider.register("envcanada", {
const precipLOP = hourGroup[stepHour].querySelector("lop").textContent * 1.0;
if (precipLOP > 0) {
weather.precipitation = precipLOP;
weather.precipitationUnits = hourGroup[stepHour].querySelector("lop").getAttribute("units");
weather.precipitationProbability = precipLOP;
}
//
@ -508,27 +500,14 @@ WeatherProvider.register("envcanada", {
setPrecipitation(weather, foreGroup, today) {
if (foreGroup[today].querySelector("precipitation accumulation")) {
weather.precipitation = foreGroup[today].querySelector("precipitation accumulation amount").textContent * 1.0;
weather.precipitationUnits = " " + foreGroup[today].querySelector("precipitation accumulation amount").getAttribute("units");
if (this.config.units === "imperial") {
if (weather.precipitationUnits === " cm") {
weather.precipitation = (weather.precipitation * 0.394).toFixed(2);
weather.precipitationUnits = " in";
}
if (weather.precipitationUnits === " mm") {
weather.precipitation = (weather.precipitation * 0.0394).toFixed(2);
weather.precipitationUnits = " in";
}
}
weather.precipitationAmount = foreGroup[today].querySelector("precipitation accumulation amount").textContent * 1.0;
weather.precipitationUnits = foreGroup[today].querySelector("precipitation accumulation amount").getAttribute("units");
}
// Check Today element for POP
if (foreGroup[today].querySelector("abbreviatedForecast pop").textContent > 0) {
weather.precipitation = foreGroup[today].querySelector("abbreviatedForecast pop").textContent;
weather.precipitationUnits = foreGroup[today].querySelector("abbreviatedForecast pop").getAttribute("units");
weather.precipitationProbability = foreGroup[today].querySelector("abbreviatedForecast pop").textContent;
}
},

View File

@ -367,7 +367,7 @@ WeatherProvider.register("openmeteo", {
* `current_weather` object.
*/
const h = moment().hour();
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
const currentWeather = new WeatherObject();
currentWeather.date = weather.current_weather.time;
currentWeather.windSpeed = weather.current_weather.windspeed;
@ -381,7 +381,7 @@ WeatherProvider.register("openmeteo", {
currentWeather.humidity = parseFloat(weather.hourly[h].relativehumidity_2m);
currentWeather.rain = parseFloat(weather.hourly[h].rain);
currentWeather.snow = parseFloat(weather.hourly[h].snowfall * 10);
currentWeather.precipitation = parseFloat(weather.hourly[h].precipitation);
currentWeather.precipitationAmount = parseFloat(weather.hourly[h].precipitation);
return currentWeather;
},
@ -391,7 +391,7 @@ WeatherProvider.register("openmeteo", {
const days = [];
weathers.daily.forEach((weather, i) => {
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
const currentWeather = new WeatherObject();
currentWeather.date = weather.time;
currentWeather.windSpeed = weather.windspeed_10m_max;
@ -404,7 +404,7 @@ WeatherProvider.register("openmeteo", {
currentWeather.weatherType = this.convertWeatherType(weather.weathercode, currentWeather.isDayTime());
currentWeather.rain = parseFloat(weather.rain_sum);
currentWeather.snow = parseFloat(weather.snowfall_sum * 10);
currentWeather.precipitation = parseFloat(weather.precipitation_sum);
currentWeather.precipitationAmount = parseFloat(weather.precipitation_sum);
days.push(currentWeather);
});
@ -422,7 +422,7 @@ WeatherProvider.register("openmeteo", {
return;
}
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
const currentWeather = new WeatherObject();
const h = Math.ceil((i + 1) / 24) - 1;
currentWeather.date = weather.time;
@ -437,7 +437,7 @@ WeatherProvider.register("openmeteo", {
currentWeather.humidity = parseFloat(weather.relativehumidity_2m);
currentWeather.rain = parseFloat(weather.rain);
currentWeather.snow = parseFloat(weather.snowfall * 10);
currentWeather.precipitation = parseFloat(weather.precipitation);
currentWeather.precipitationAmount = parseFloat(weather.precipitation);
hours.push(currentWeather);
});

View File

@ -186,7 +186,7 @@ WeatherProvider.register("openweathermap", {
weather.maxTemperature = Math.max.apply(null, maxTemp);
weather.rain = rain;
weather.snow = snow;
weather.precipitation = weather.rain + weather.snow;
weather.precipitationAmount = (weather.rain ?? 0) + (weather.snow ?? 0);
// push weather information to days array
days.push(weather);
// create new weather-object
@ -216,22 +216,14 @@ WeatherProvider.register("openweathermap", {
minTemp.push(forecast.main.temp_min);
maxTemp.push(forecast.main.temp_max);
if (forecast.hasOwnProperty("rain")) {
if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) {
rain += forecast.rain["3h"] / 25.4;
} else if (!isNaN(forecast.rain["3h"])) {
if (forecast.hasOwnProperty("rain") && !isNaN(forecast.rain["3h"])) {
rain += forecast.rain["3h"];
}
}
if (forecast.hasOwnProperty("snow")) {
if (this.config.units === "imperial" && !isNaN(forecast.snow["3h"])) {
snow += forecast.snow["3h"] / 25.4;
} else if (!isNaN(forecast.snow["3h"])) {
if (forecast.hasOwnProperty("snow") && !isNaN(forecast.snow["3h"])) {
snow += forecast.snow["3h"];
}
}
}
// last day
// calculate minimum/maximum temperature, specify rain amount
@ -239,7 +231,7 @@ WeatherProvider.register("openweathermap", {
weather.maxTemperature = Math.max.apply(null, maxTemp);
weather.rain = rain;
weather.snow = snow;
weather.precipitation = weather.rain + weather.snow;
weather.precipitationAmount = (weather.rain ?? 0) + (weather.snow ?? 0);
// push weather information to days array
days.push(weather);
return days.slice(1);
@ -264,25 +256,18 @@ WeatherProvider.register("openweathermap", {
// forecast.rain not available if amount is zero
// The API always returns in millimeters
if (forecast.hasOwnProperty("rain")) {
if (this.config.units === "imperial" && !isNaN(forecast.rain)) {
weather.rain = forecast.rain / 25.4;
} else if (!isNaN(forecast.rain)) {
if (forecast.hasOwnProperty("rain") && !isNaN(forecast.rain)) {
weather.rain = forecast.rain;
}
}
// forecast.snow not available if amount is zero
// The API always returns in millimeters
if (forecast.hasOwnProperty("snow")) {
if (this.config.units === "imperial" && !isNaN(forecast.snow)) {
weather.snow = forecast.snow / 25.4;
} else if (!isNaN(forecast.snow)) {
if (forecast.hasOwnProperty("snow") && !isNaN(forecast.snow)) {
weather.snow = forecast.snow;
}
}
weather.precipitation = weather.rain + weather.snow;
weather.precipitationAmount = weather.rain + weather.snow;
weather.precipitationProbability = forecast.pop ? forecast.pop * 100 : undefined;
days.push(weather);
}
@ -310,23 +295,15 @@ WeatherProvider.register("openweathermap", {
current.weatherType = this.convertWeatherType(data.current.weather[0].icon);
current.humidity = data.current.humidity;
if (data.current.hasOwnProperty("rain") && !isNaN(data.current["rain"]["1h"])) {
if (this.config.units === "imperial") {
current.rain = data.current["rain"]["1h"] / 25.4;
} else {
current.rain = data.current["rain"]["1h"];
}
precip = true;
}
if (data.current.hasOwnProperty("snow") && !isNaN(data.current["snow"]["1h"])) {
if (this.config.units === "imperial") {
current.snow = data.current["snow"]["1h"] / 25.4;
} else {
current.snow = data.current["snow"]["1h"];
}
precip = true;
}
if (precip) {
current.precipitation = current.rain + current.snow;
current.precipitationAmount = (current.rain ?? 0) + (current.snow ?? 0);
}
current.feelsLikeTemp = data.current.feels_like;
}
@ -344,25 +321,18 @@ WeatherProvider.register("openweathermap", {
weather.windSpeed = hour.wind_speed;
weather.windFromDirection = hour.wind_deg;
weather.weatherType = this.convertWeatherType(hour.weather[0].icon);
weather.precipitationProbability = hour.pop ? hour.pop * 100 : undefined;
precip = false;
if (hour.hasOwnProperty("rain") && !isNaN(hour.rain["1h"])) {
if (this.config.units === "imperial") {
weather.rain = hour.rain["1h"] / 25.4;
} else {
weather.rain = hour.rain["1h"];
}
precip = true;
}
if (hour.hasOwnProperty("snow") && !isNaN(hour.snow["1h"])) {
if (this.config.units === "imperial") {
weather.snow = hour.snow["1h"] / 25.4;
} else {
weather.snow = hour.snow["1h"];
}
precip = true;
}
if (precip) {
weather.precipitation = weather.rain + weather.snow;
weather.precipitationAmount = (weather.rain ?? 0) + (weather.snow ?? 0);
}
hours.push(weather);
@ -383,25 +353,18 @@ WeatherProvider.register("openweathermap", {
weather.windSpeed = day.wind_speed;
weather.windFromDirection = day.wind_deg;
weather.weatherType = this.convertWeatherType(day.weather[0].icon);
weather.precipitationProbability = day.pop ? day.pop * 100 : undefined;
precip = false;
if (!isNaN(day.rain)) {
if (this.config.units === "imperial") {
weather.rain = day.rain / 25.4;
} else {
weather.rain = day.rain;
}
precip = true;
}
if (!isNaN(day.snow)) {
if (this.config.units === "imperial") {
weather.snow = day.snow / 25.4;
} else {
weather.snow = day.snow;
}
precip = true;
}
if (precip) {
weather.precipitation = weather.rain + weather.snow;
weather.precipitationAmount = (weather.rain ?? 0) + (weather.snow ?? 0);
}
days.push(weather);

View File

@ -94,16 +94,11 @@ WeatherProvider.register("pirateweather", {
weather.rain = 0;
let precip = 0;
// The API will return centimeters if units is 'si' and will return inches for 'us'
if (forecast.hasOwnProperty("precipAccumulation")) {
if (this.config.units === "imperial" && !isNaN(forecast.precipAccumulation)) {
precip = forecast.precipAccumulation;
} else if (!isNaN(forecast.precipAccumulation)) {
precip = forecast.precipAccumulation * 10;
}
}
weather.precipitation = precip;
weather.precipitationAmount = precip;
if (forecast.hasOwnProperty("precipType")) {
if (forecast.precipType === "snow") {
weather.snow = precip;

View File

@ -157,19 +157,19 @@ WeatherProvider.register("smhi", {
// 0 = No precipitation
case 1: // Snow
currentWeather.snow += precipitationValue;
currentWeather.precipitation += precipitationValue;
currentWeather.precipitationAmount += precipitationValue;
break;
case 2: // Snow and rain, treat it as 50/50 snow and rain
currentWeather.snow += precipitationValue / 2;
currentWeather.rain += precipitationValue / 2;
currentWeather.precipitation += precipitationValue;
currentWeather.precipitationAmount += precipitationValue;
break;
case 3: // Rain
case 4: // Drizzle
case 5: // Freezing rain
case 6: // Freezing drizzle
currentWeather.rain += precipitationValue;
currentWeather.precipitation += precipitationValue;
currentWeather.precipitationAmount += precipitationValue;
break;
}
@ -202,7 +202,7 @@ WeatherProvider.register("smhi", {
currentWeather.maxTemperature = -Infinity;
currentWeather.snow = 0;
currentWeather.rain = 0;
currentWeather.precipitation = 0;
currentWeather.precipitationAmount = 0;
result.push(currentWeather);
}
@ -221,7 +221,7 @@ WeatherProvider.register("smhi", {
currentWeather.maxTemperature = Math.max(currentWeather.maxTemperature, weatherObject.temperature);
currentWeather.snow += weatherObject.snow;
currentWeather.rain += weatherObject.rain;
currentWeather.precipitation += weatherObject.precipitation;
currentWeather.precipitationAmount += weatherObject.precipitationAmount;
}
return result;

View File

@ -100,7 +100,7 @@ WeatherProvider.register("ukmetoffice", {
currentWeather.humidity = rep.H;
currentWeather.temperature = rep.T;
currentWeather.feelsLikeTemp = rep.F;
currentWeather.precipitation = parseInt(rep.Pp);
currentWeather.precipitationProbability = parseInt(rep.Pp);
currentWeather.windSpeed = WeatherUtils.convertWindToMetric(rep.S);
currentWeather.windFromDirection = WeatherUtils.convertWindDirection(rep.D);
currentWeather.weatherType = this.convertWeatherType(rep.W);
@ -138,7 +138,7 @@ WeatherProvider.register("ukmetoffice", {
weather.minTemperature = period.Rep[1].Nm;
weather.maxTemperature = period.Rep[0].Dm;
weather.weatherType = this.convertWeatherType(period.Rep[0].W);
weather.precipitation = parseInt(period.Rep[0].PPd);
weather.precipitationProbability = parseInt(period.Rep[0].PPd);
days.push(weather);
}

View File

@ -134,7 +134,7 @@ WeatherProvider.register("ukmetofficedatahub", {
currentWeather.humidity = forecastDataHours[hour].screenRelativeHumidity;
currentWeather.rain = forecastDataHours[hour].totalPrecipAmount;
currentWeather.snow = forecastDataHours[hour].totalSnowAmount;
currentWeather.precipitation = forecastDataHours[hour].probOfPrecipitation;
currentWeather.precipitationProbability = forecastDataHours[hour].probOfPrecipitation;
currentWeather.feelsLikeTemp = forecastDataHours[hour].feelsLikeTemperature;
// Pass on full details, so they can be used in custom templates
@ -206,7 +206,7 @@ WeatherProvider.register("ukmetofficedatahub", {
forecastWeather.windSpeed = forecastDataDays[day].midday10MWindSpeed;
forecastWeather.windFromDirection = forecastDataDays[day].midday10MWindDirection;
forecastWeather.weatherType = this.convertWeatherType(forecastDataDays[day].daySignificantWeatherCode);
forecastWeather.precipitation = forecastDataDays[day].dayProbabilityOfPrecipitation;
forecastWeather.precipitationProbability = forecastDataDays[day].dayProbabilityOfPrecipitation;
forecastWeather.temperature = forecastDataDays[day].dayMaxScreenTemperature;
forecastWeather.humidity = forecastDataDays[day].middayRelativeHumidity;
forecastWeather.rain = forecastDataDays[day].dayProbabilityOfRain;

View File

@ -125,7 +125,8 @@ WeatherProvider.register("weatherbit", {
weather.date = moment(forecast.datetime, "YYYY-MM-DD");
weather.minTemperature = forecast.min_temp;
weather.maxTemperature = forecast.max_temp;
weather.precipitation = forecast.precip;
weather.precipitationAmount = forecast.precip;
weather.precipitationProbability = forecast.pop;
weather.weatherType = this.convertWeatherType(forecast.weather.icon);
days.push(weather);

View File

@ -55,6 +55,7 @@ WeatherProvider.register("weatherflow", {
weather.date = moment.unix(forecast.day_start_local);
weather.minTemperature = forecast.air_temp_low;
weather.maxTemperature = forecast.air_temp_high;
weather.precipitationProbability = forecast.precip_probability;
weather.weatherType = forecast.icon;
weather.snow = 0;

View File

@ -210,9 +210,7 @@ WeatherProvider.register("weathergov", {
currentWeather.minTemperature = currentWeatherData.minTemperatureLast24Hours.value;
currentWeather.maxTemperature = currentWeatherData.maxTemperatureLast24Hours.value;
currentWeather.humidity = Math.round(currentWeatherData.relativeHumidity.value);
currentWeather.rain = null;
currentWeather.snow = null;
currentWeather.precipitation = this.convertLength(currentWeatherData.precipitationLastHour.value);
currentWeather.precipitationAmount = currentWeatherData.precipitationLastHour.value;
if (currentWeatherData.heatIndex.value !== null) {
currentWeather.feelsLikeTemp = currentWeatherData.heatIndex.value;
} else if (currentWeatherData.windChill.value !== null) {
@ -240,6 +238,8 @@ WeatherProvider.register("weathergov", {
* fetch forecast information for daily forecast.
*/
fetchForecastDaily(forecasts) {
const precipitationProbabilityRegEx = "Chance of precipitation is ([0-9]+?)%";
// initial variable declaration
const days = [];
// variables for temperature range and rain
@ -248,7 +248,6 @@ WeatherProvider.register("weathergov", {
// variable for date
let date = "";
let weather = new WeatherObject();
weather.precipitation = 0;
for (const forecast of forecasts) {
if (date !== moment(forecast.startTime).format("YYYY-MM-DD")) {
@ -263,7 +262,8 @@ WeatherProvider.register("weathergov", {
minTemp = [];
maxTemp = [];
weather.precipitation = 0;
const precipitation = new RegExp(precipitationProbabilityRegEx, "g").exec(forecast.detailedForecast);
if (precipitation) weather.precipitationProbability = precipitation[1];
// set new date
date = moment(forecast.startTime).format("YYYY-MM-DD");
@ -295,18 +295,6 @@ WeatherProvider.register("weathergov", {
return days.slice(1);
},
/*
* Unit conversions
*/
// conversion to inches
convertLength(meters) {
if (this.config.units === "imperial") {
return meters * 39.3701;
} else {
return meters;
}
},
/*
* Convert the icons to a more usable name.
*/

View File

@ -65,7 +65,8 @@ WeatherProvider.register("yr", {
}
const forecastXHours = this.getForecastForXHoursFrom(forecast.data);
forecast.weatherType = this.convertWeatherType(forecastXHours.summary.symbol_code, forecast.time);
forecast.precipitation = forecastXHours.details?.precipitation_amount;
forecast.precipitationAmount = forecastXHours.details?.precipitation_amount;
forecast.precipitationProbability = forecastXHours.details?.probability_of_precipitation;
forecast.minTemperature = forecastXHours.details?.air_temperature_min;
forecast.maxTemperature = forecastXHours.details?.air_temperature_max;
return this.getWeatherDataFrom(forecast, stellarData, weatherData.properties.meta.units);
@ -358,7 +359,7 @@ WeatherProvider.register("yr", {
},
getWeatherDataFrom(forecast, stellarData, units) {
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
const weather = new WeatherObject();
const stellarTimesToday = stellarData?.today ? this.getStellarTimesFrom(stellarData.today, moment().format("YYYY-MM-DD")) : undefined;
const stellarTimesTomorrow = stellarData?.tomorrow ? this.getStellarTimesFrom(stellarData.tomorrow, moment().add(1, "days").format("YYYY-MM-DD")) : undefined;
@ -370,7 +371,8 @@ WeatherProvider.register("yr", {
weather.maxTemperature = forecast.maxTemperature;
weather.weatherType = forecast.weatherType;
weather.humidity = forecast.data.instant.details.relative_humidity;
weather.precipitation = forecast.precipitation;
weather.precipitationAmount = forecast.precipitationAmount;
weather.precipitationProbability = forecast.precipitationProbability;
weather.precipitationUnits = units.precipitation_amount;
if (stellarTimesToday) {
@ -554,7 +556,8 @@ WeatherProvider.register("yr", {
for (const forecast of weatherData.properties.timeseries) {
forecast.symbol = forecast.data.next_1_hours?.summary?.symbol_code;
forecast.precipitation = forecast.data.next_1_hours?.details?.precipitation_amount;
forecast.precipitationAmount = forecast.data.next_1_hours?.details?.precipitation_amount;
forecast.precipitationProbability = forecast.data.next_1_hours?.details?.probability_of_precipitation;
forecast.minTemperature = forecast.data.next_1_hours?.details?.air_temperature_min;
forecast.maxTemperature = forecast.data.next_1_hours?.details?.air_temperature_max;
forecast.weatherType = this.convertWeatherType(forecast.symbol, forecast.time);
@ -599,7 +602,8 @@ WeatherProvider.register("yr", {
const forecastXHours = forecast.data.next_12_hours ?? forecast.data.next_6_hours ?? forecast.data.next_1_hours;
if (forecastXHours) {
forecast.symbol = forecastXHours.summary?.symbol_code;
forecast.precipitation = forecastXHours.details?.precipitation_amount;
forecast.precipitationAmount = forecastXHours.details?.precipitation_amount ?? forecast.data.next_6_hours?.details?.precipitation_amount; // 6 hours is likely to have precipitation amount even if 12 hours does not
forecast.precipitationProbability = forecastXHours.details?.probability_of_precipitation;
forecast.minTemperature = minTemperature;
forecast.maxTemperature = maxTemperature;

View File

@ -26,6 +26,7 @@ Module.register("weather", {
showPeriod: true,
showPeriodUpper: false,
showPrecipitationAmount: false,
showPrecipitationProbability: false,
showSun: true,
showWindDirection: true,
showWindDirectionAsArrow: false,
@ -230,7 +231,7 @@ Module.register("weather", {
this.nunjucksEnvironment().addFilter(
"unit",
function (value, type) {
function (value, type, valueUnit) {
if (type === "temperature") {
value = this.roundValue(WeatherUtils.convertTemp(value, this.config.tempUnits)) + "°";
if (this.config.degreeLabel) {
@ -246,11 +247,7 @@ Module.register("weather", {
if (value === null || isNaN(value) || value === 0 || value.toFixed(2) === "0.00") {
value = "";
} else {
if (this.config.weatherProvider === "ukmetoffice" || this.config.weatherProvider === "ukmetofficedatahub") {
value += "%";
} else {
value = `${value.toFixed(2)} ${this.config.units === "imperial" ? "in" : "mm"}`;
}
value = WeatherUtils.convertPrecipitationUnit(value, valueUnit, this.config.units);
}
} else if (type === "humidity") {
value += "%";

View File

@ -30,10 +30,9 @@ class WeatherObject {
this.maxTemperature = null;
this.weatherType = null;
this.humidity = null;
this.rain = null;
this.snow = null;
this.precipitation = null;
this.precipitationAmount = null;
this.precipitationUnits = null;
this.precipitationProbability = null;
this.feelsLikeTemp = null;
}

View File

@ -22,6 +22,27 @@ const WeatherUtils = {
return 12;
},
/**
* Convert a value in a given unit to a string with a converted
* value and a postfix matching the output unit system.
*
* @param {number} value - The value to convert.
* @param {string} valueUnit - The unit the values has. Default is mm.
* @param {string} outputUnit - The unit system (imperial/metric) the return value should have.
* @returns {string} - A string with tha value and a unit postfix.
*/
convertPrecipitationUnit(value, valueUnit, outputUnit) {
if (outputUnit === "imperial") {
if (valueUnit && valueUnit.toLowerCase() === "cm") value = value * 0.3937007874;
else value = value * 0.03937007874;
valueUnit = "in";
} else {
valueUnit = valueUnit ? valueUnit : "mm";
}
return `${value.toFixed(2)} ${valueUnit}`;
},
/**
* Convert temp (from degrees C) into imperial or metric unit depending on
* your config

View File

@ -15,7 +15,8 @@ let config = {
location: "Munich",
mockData: '"#####WEATHERDATA#####"',
weatherEndpoint: "/forecast/daily",
decimalSymbol: "_"
decimalSymbol: "_",
showPrecipitationAmount: true
}
}
]

View File

@ -0,0 +1,27 @@
/* MagicMirror² Test config hourly weather
*
* By rejas https://github.com/rejas
* MIT Licensed.
*/
let config = {
timeFormat: 12,
modules: [
{
module: "weather",
position: "bottom_bar",
config: {
type: "hourly",
location: "Berlin",
mockData: '"#####WEATHERDATA#####"',
showPrecipitationAmount: true,
showPrecipitationProbability: true
}
}
]
};
/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = config;
}

View File

@ -81,11 +81,12 @@ describe("Weather module: Weather Forecast", () => {
});
});
describe("Forecast weather units", () => {
describe("Forecast weather with imperial units", () => {
beforeAll(async () => {
await weatherFunc.startApp("tests/configs/modules/weather/forecastweather_units.js", {});
});
describe("Temperature units", () => {
const temperatures = ["75_9°", "69_8°", "73_2°", "74_1°", "69_1°"];
for (const [index, temp] of temperatures.entries()) {
it("should render custom decimalSymbol = '_' for temp " + temp, async () => {
@ -93,4 +94,16 @@ describe("Weather module: Weather Forecast", () => {
});
}
});
describe("Precipitation units", () => {
const precipitations = [undefined, "0.10 in"];
for (const [index, precipitation] of precipitations.entries()) {
if (precipitation) {
it("should render precipitation value " + precipitation, async () => {
await weatherFunc.getText(`.weather table.small tr:nth-child(${index + 1}) td.precipitationAmount`, precipitation);
});
}
}
});
});
});

View File

@ -33,4 +33,32 @@ describe("Weather module: Weather Hourly Forecast", () => {
}
});
});
describe("Show precipitations", () => {
beforeAll(async () => {
await weatherFunc.startApp("tests/configs/modules/weather/hourlyweather_showPrecipitation.js", {});
});
describe("Shows precipitation amount", () => {
const amounts = [undefined, undefined, undefined, "0.13 mm", "0.13 mm"];
for (const [index, amount] of amounts.entries()) {
if (amount) {
it(`should render precipitation amount ${amount}`, async () => {
await weatherFunc.getText(`.weather table.small tr:nth-child(${index + 1}) td.precipitationAmount`, amount);
});
}
}
});
describe("Shows precipitation propability", () => {
const propabilities = [undefined, undefined, "12%", "36%", "44%"];
for (const [index, pop] of propabilities.entries()) {
if (pop) {
it(`should render propability ${pop}`, async () => {
await weatherFunc.getText(`.weather table.small tr:nth-child(${index + 1}) td.precipitationProbability`, pop);
});
}
}
});
});
});

View File

@ -64,7 +64,9 @@
],
"speed": 2.21,
"deg": 81,
"clouds": 100
"clouds": 100,
"pop": 0.7,
"rain": 2.51
},
{
"dt": 1568545200,

View File

@ -0,0 +1,45 @@
const weather = require("../../../../../modules/default/weather/weatherutils.js");
describe("Weather utils tests", () => {
describe("convertPrecipitationUnit tests", () => {
it("Should keep value and unit if outputUnit is undefined", () => {
const values = [1, 2];
const units = ["mm", "cm"];
for (let i = 0; i < values.length; i++) {
var result = weather.convertPrecipitationUnit(values[i], units[i], undefined);
expect(result).toBe(`${values[i].toFixed(2)} ${units[i]}`);
}
});
it("Should keep value and unit if outputUnit is metric", () => {
const values = [1, 2];
const units = ["mm", "cm"];
for (let i = 0; i < values.length; i++) {
var result = weather.convertPrecipitationUnit(values[i], units[i], "metric");
expect(result).toBe(`${values[i].toFixed(2)} ${units[i]}`);
}
});
it("Should use mm unit if input unit is undefined", () => {
const values = [1, 2];
for (let i = 0; i < values.length; i++) {
var result = weather.convertPrecipitationUnit(values[i], undefined, "metric");
expect(result).toBe(`${values[i].toFixed(2)} mm`);
}
});
it("Should convert value and unit if outputUnit is imperial", () => {
const values = [1, 2];
const units = ["mm", "cm"];
const expectedValues = [0.04, 0.79];
for (let i = 0; i < values.length; i++) {
var result = weather.convertPrecipitationUnit(values[i], units[i], "imperial");
expect(result).toBe(`${expectedValues[i]} in`);
}
});
});
});

View File

@ -26,7 +26,7 @@
"NNW": "SSZ",
"FEELS": "Pocitově {DEGREE}",
"PRECIP": "Pravděpodobnost deště",
"PRECIP_POP": "Pravděpodobnost deště",
"NEWSFEED_NO_ITEMS": "Žádné zprávy.",

View File

@ -26,7 +26,7 @@
"NNW": "NNV",
"FEELS": "Føles som {DEGREE}",
"PRECIP": "Sandsynlighed for nedbør",
"PRECIP_POP": "Sandsynlighed for nedbør",
"MODULE_CONFIG_CHANGED": "Konfigurationsmulighederne for {MODULE_NAME} modulet er ændret.\nSe venligst dokumentationen.",
"MODULE_CONFIG_ERROR": "Fejl i {MODULE_NAME} modulet. {ERROR}",

View File

@ -26,7 +26,8 @@
"NNW": "NNW",
"FEELS": "Gefühlt {DEGREE}",
"PRECIP": "Niederschlagswahrscheinlichkeit",
"PRECIP_POP": "Niederschlagswahrscheinlichkeit",
"PRECIP_AMOUNT": "Niederschlagsmenge",
"MODULE_CONFIG_CHANGED": "Die Konfigurationsoptionen für das Modul „{MODULE_NAME}“ haben sich geändert. \nBitte überprüfen Sie die Dokumentation.",
"MODULE_CONFIG_ERROR": "Fehler im Modul „{MODULE_NAME}“. {ERROR}",

View File

@ -25,7 +25,8 @@
"NNW": "NNW",
"FEELS": "Feels like {DEGREE}",
"PRECIP": "PoP",
"PRECIP_POP": "PoP",
"PRECIP_AMOUNT": "Precipitation amount",
"MODULE_CONFIG_CHANGED": "The configuration options for the {MODULE_NAME} module have changed.\nPlease check the documentation.",
"MODULE_CONFIG_ERROR": "Error in the {MODULE_NAME} module. {ERROR}",

View File

@ -26,7 +26,7 @@
"NNW": "NNO",
"FEELS": "Sensación térmica de {DEGREE}",
"PRECIP": "Precipitación",
"PRECIP_POP": "Precipitación",
"MODULE_CONFIG_CHANGED": "Las opciones de configuración para el módulo {MODULE_NAME} han cambiado. \nVerifique la documentación.",

View File

@ -26,7 +26,7 @@
"NNW": "PPL",
"FEELS": "Tuntuu kuin {DEGREE}",
"PRECIP": "Sateen todennäköisyys",
"PRECIP_POP": "Sateen todennäköisyys",
"UPDATE_NOTIFICATION": "MagicMirror² päivitys saatavilla.",
"UPDATE_NOTIFICATION_MODULE": "Päivitys saatavilla moduulille {MODULE_NAME}.",

View File

@ -26,7 +26,7 @@
"NNW": "NNO",
"FEELS": "Ressenti {DEGREE}",
"PRECIP": "Probabilité de précipitations",
"PRECIP_POP": "Probabilité de précipitations",
"MODULE_CONFIG_CHANGED": "Les options de configuration du module {MODULE_NAME} ont changé.\nVeuillez consulter la documentation.",
"MODULE_CONFIG_ERROR": "Erreur dans le module {MODULE_NAME}. {ERROR}",

View File

@ -35,5 +35,5 @@
"UPDATE_INFO_MULTIPLE": "A instalación actual está {COMMIT_COUNT} commits detrás da rama {BRANCH_NAME}.",
"FEELS": "Semella como {DEGREE}",
"PRECIP": "Precipitacións"
"PRECIP_POP": "Precipitacións"
}

View File

@ -26,7 +26,7 @@
"NNW": "ઉઉપ",
"FEELS": "{DEGREE} જેવું લાગશે",
"PRECIP": "PoP",
"PRECIP_POP": "PoP",
"MODULE_CONFIG_CHANGED": "{MODULE_NAME} મોડ્યુલ માટે ગોઠવણી વિકલ્પો બદલાયા છે. \nકૃપા કરીને દસ્તાવેજોને તપાસો.",

View File

@ -26,7 +26,7 @@
"NNW": "צ-צ-מע",
"FEELS": "מרגיש כמו {DEGREE}",
"PRECIP": "משקעים",
"PRECIP_POP": "משקעים",
"UPDATE_NOTIFICATION": "עדכון זמין ל-MagicMirror²",
"UPDATE_NOTIFICATION_MODULE": "עדכון זמין ב-{MODULE_NAME} מודול",

View File

@ -26,7 +26,7 @@
"NNW": "उउप",
"FEELS": "{DEGREE} की तरह लगना",
"PRECIP": "PoP",
"PRECIP_POP": "PoP",
"MODULE_CONFIG_CHANGED": "{MODULE_NAME} मॉड्यूल के लिए कॉन्फ़िगरेशन विकल्प बदल गए हैं। n कृपया दस्तावेज़ देखें।",

View File

@ -26,7 +26,7 @@
"NNW": "북북서풍",
"FEELS": "체감온도 {DEGREE}",
"PRECIP": "PoP",
"PRECIP_POP": "PoP",
"MODULE_CONFIG_CHANGED": "모듈 {MODULE_NAME}의 설정값이 바뀌었습니다.\n매뉴얼을 참고하세요.",
"MODULE_CONFIG_ERROR": "에러 : {MODULE_NAME} - {ERROR}",

View File

@ -26,7 +26,7 @@
"NNW": "ŠŠV",
"FEELS": "Jutiminė temp. {DEGREE}",
"PRECIP": "Krituliai",
"PRECIP_POP": "Krituliai",
"UPDATE_NOTIFICATION": "Galimas MagicMirror² naujinimas.",
"UPDATE_NOTIFICATION_MODULE": "Galimas {MODULE_NAME} naujinimas.",

View File

@ -26,6 +26,8 @@
"NNW": "NNV",
"FEELS": "Føles som {DEGREE}",
"PRECIP_POP": "Sannsynlighet for nedbør",
"PRECIP_AMOUNT": "Nedbørsmengde",
"UPDATE_NOTIFICATION": "MagicMirror²-oppdatering er tilgjengelig.",
"UPDATE_NOTIFICATION_MODULE": "Oppdatering tilgjengelig for modulen {MODULE_NAME}.",

View File

@ -26,7 +26,7 @@
"NNW": "NNW",
"FEELS": "Voelt als {DEGREE}",
"PRECIP": "Neerslagkans",
"PRECIP_POP": "Neerslagkans",
"MODULE_CONFIG_CHANGED": "De configuratie opties voor de module {MODULE_NAME} zijn gewijzigd.\nControleer de documentatie.",
"MODULE_CONFIG_ERROR": "Fout in de {MODULE_NAME} module. {ERROR}",

View File

@ -26,7 +26,7 @@
"NNW": "NNW",
"FEELS": "Odczuwalna {DEGREE}",
"PRECIP": "Szansa opadów",
"PRECIP_POP": "Szansa opadów",
"UPDATE_NOTIFICATION": "Dostępna jest aktualizacja MagicMirror².",
"UPDATE_NOTIFICATION_MODULE": "Dostępna jest aktualizacja modułu {MODULE_NAME}.",

View File

@ -24,7 +24,7 @@
"NNW": "NNO",
"FEELS": "Percebida {DEGREE}",
"PRECIP": "PoP",
"PRECIP_POP": "PoP",
"UPDATE_NOTIFICATION": "Nova atualização para MagicMirror² disponível.",
"UPDATE_NOTIFICATION_MODULE": "Atualização para o módulo {MODULE_NAME} disponível.",

View File

@ -26,7 +26,7 @@
"NNW": "NNO",
"FEELS": "Sentida {DEGREE}",
"PRECIP": "Prob. Precipitação",
"PRECIP_POP": "Prob. Precipitação",
"MODULE_CONFIG_CHANGED": "As opções na configuração do módulo {MODULE_NAME} foram alteradas.\nPor favor, verifica a documentação.",

View File

@ -26,7 +26,7 @@
"NNW": "KKB",
"FEELS": "Hissedilen {DEGREE}",
"PRECIP": "Yağış",
"PRECIP_POP": "Yağış",
"UPDATE_NOTIFICATION": "MagicMirror² güncellemesi mevcut.",
"UPDATE_NOTIFICATION_MODULE": "{MODULE_NAME} modulü için güncelleme mevcut.",

View File

@ -26,7 +26,7 @@
"NNW": "ПнПнЗх",
"FEELS": "Відчувається як {DEGREE}",
"PRECIP": "Опади",
"PRECIP_POP": "Опади",
"UPDATE_NOTIFICATION": "Є оновлення для MagicMirror².",
"UPDATE_NOTIFICATION_MODULE": "Є оновлення для модуля {MODULE_NAME}.",

View File

@ -26,7 +26,7 @@
"NNW": "北偏西風",
"FEELS": "體感溫度 {DEGREE}",
"PRECIP": "降雨機率",
"PRECIP_POP": "降雨機率",
"MODULE_CONFIG_CHANGED": "模組 {MODULE_NAME} 的設定檔選項已更改。\n請參見說明文件。",
"MODULE_CONFIG_ERROR": "{MODULE_NAME} 模組發生錯誤。{ERROR}",