Skip to content

Commit 31eabe3

Browse files
fix: improve alert filtering with time window and deduplication
- Add showAlertsHours config option (default: 12 hours) - Filter alerts to only show those starting within configured time window - Remove duplicate alert names when same alert spans multiple days - Change showAlerts default from false to true - Update README documentation with new option and examples This fixes the issue where duplicate alerts were shown (e.g., "Winter Weather Advisory" appearing 3 times for alerts spanning today and tomorrow).
1 parent 22f3fdb commit 31eabe3

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

MMM-OneCallWeather.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Module.register('MMM-OneCallWeather', {
4242
showCurrent: true,
4343
showForecast: true,
4444
showAlerts: true,
45+
showAlertsHours: 12,
4546
forecastLayout: 'columns', // "columns" (days as columns) or "rows" (days as rows)
4647
arrangement: 'vertical', // "vertical" (forecast below current) or "horizontal" (forecast next to current)
4748

@@ -694,13 +695,23 @@ Module.register('MMM-OneCallWeather', {
694695
currentCell4.colSpan = colspan
695696
currentCell4.className = 'alert'
696697

698+
const now = Date.now() / 1000
699+
const alertWindow = now + (this.config.showAlertsHours * 3600)
700+
697701
const validAlerts = currentWeather.alerts
698-
.filter(alert => alert?.event)
702+
.filter(alert =>
703+
alert?.event
704+
&& alert.start < alertWindow // Starts within the configured time window
705+
&& alert.end > now, // Is still active (not expired)
706+
)
699707
.map(alert => alert.event)
700708

701-
if (validAlerts.length > 0) {
709+
// Remove duplicates (e.g., same alert for today and tomorrow)
710+
const uniqueAlerts = [...new Set(validAlerts)]
711+
712+
if (uniqueAlerts.length > 0) {
702713
const fragment = document.createDocumentFragment()
703-
for (const [index, alertEvent] of validAlerts.entries()) {
714+
for (const [index, alertEvent] of uniqueAlerts.entries()) {
704715
if (index > 0) {
705716
fragment.appendChild(document.createElement('br'))
706717
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ The following properties can be configured:
9292
| `windUnits` | The units to use for wind speed.<br><br>**Possible values:** `"mph"` (miles per hour), `"kmph"` (kilometers per hour), `"ms"` (meters per second)<br>**Default value:** `"mph"`<br>This value is optional. By default the weatherforecast module will display wind speed in miles per hour. |
9393
| `roundTemp` | Round temperature values to nearest integer.<br><br>**Possible values:** `true` (round to integer) or `false` (display exact value with decimal point)<br>**Default value:** `false` |
9494
| `showCurrent` | Show the current weather section.<br><br>**Possible values:** `true` or `false`<br>**Default value:** `true` |
95-
| `showAlerts` | Show weather alerts from the OpenWeatherMap API in the current weather section. Displays the alert event names (e.g., "Severe Thunderstorm Warning"). Multiple alerts are shown on separate lines.<br><br>**Possible values:** `true` or `false`<br>**Default value:** `false`<br>**Note:** Alerts are only available with certain OpenWeatherMap API plans and for locations where alerts are issued. |
95+
| `showAlerts` | Show weather alerts from the OpenWeatherMap API in the current weather section. Displays the alert event names (e.g., "Severe Thunderstorm Warning"). Multiple alerts are shown on separate lines.<br><br>**Possible values:** `true` or `false`<br>**Default value:** `true`<br>**Note:** Alerts are only available with certain OpenWeatherMap API plans and for locations where alerts are issued. Duplicate alerts are automatically filtered. |
96+
| `showAlertsHours` | Time window (in hours) for displaying weather alerts. Only alerts that start within this time window and are still active are shown.<br><br>**Possible values:** Any positive number<br>**Default value:** `12` (shows alerts for the next 12 hours)<br>**Example:** Set to `24` to see alerts for the next day, or `6` for only the next 6 hours. |
9697
| `showForecast` | Show the forecast section.<br><br>**Possible values:** `true` or `false`<br>**Default value:** `true` |
9798
| `forecastLayout` | Defines how the forecast is structured internally.<br><br>**Possible values:** `"columns"` (days displayed as table columns), `"rows"` (days displayed as table rows)<br>**Default value:** `"columns"` |
9899
| `arrangement` | How current weather and forecast are positioned relative to each other (only relevant when both `showCurrent` and `showForecast` are `true`).<br><br>**Possible values:** `"vertical"` (forecast below current weather), `"horizontal"` (forecast next to current weather)<br>**Default value:** `"vertical"` |

0 commit comments

Comments
 (0)