Skip to content

chore(nws): offset the zone request and place the alerts locally - #448

Open
systems-monitor wants to merge 4 commits into
seevee:mainfrom
systems-monitor:feat/nws-offset-placing
Open

systems-monitor wants to merge 4 commits into
seevee:mainfrom
systems-monitor:feat/nws-offset-placing

Conversation

@systems-monitor

@systems-monitor systems-monitor commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Round 3 of #316, first part. Two modules and the parity script. Nothing calls the modules yet, so this changes nothing for any entry.

What is here

Commit What it adds
chore(nws): calculate the zone request from an offset position providers/nws_offset.py: where the zone request is centred, and the record of that choice.
chore(nws): ask about every zone in range and place each alert providers/nws_matching.py: which zones a request names, and where each alert in the reply lies relative to the position.
chore(scripts): compare the placing with point= on the live service scripts/nws_zone_parity.py makes the request the mode will make and compares the result with point=.
chore(scripts): count an extra alert at a zone's edge as a match The check stops calling it a difference when the point is within the tolerance of the edge of a zone that contains it. See the live result below.

Neither module imports Home Assistant. Each commit passes the gates on its own.

Two things that changed since #435

1. The radius means every alert within it. Earlier drafts kept only the alerts for the exact point and asked about a few zones per layer. I use this to follow weather that is on its way, at about 100 miles, so the request now names every zone in the circle and each alert in the reply is placed:

  • here: what point= returns. An alert with a polygon when the point is inside it; an alert with zones only when one of them is within its simplification tolerance of the point (567 m; 2.2 km in the states the dataset lists), with the land and sea rule banded the same way;
  • nearby: within the radius, with its distance to the nearest edge;
  • beyond the radius: the only ones discarded.
    An alert that cannot be placed is kept. How nearby alerts show up in entities is a later piece and is yours to shape; it touches Feat - Differentiating your current zone versus neighboring zones. #430.

2. The request is not made from the position. A zone list calculated from the exact position can be turned back into an area of about 2 to 430 km² by anyone who holds the zone shapes, which are public, and a wider radius can make that area smaller. So the request is centred somewhere else:

  • the centre is the position moved in a random direction by 15 to 35% of the radius, and never by less than 15 to 35 miles;
  • the request radius is enlarged by the largest offset that could have been drawn plus the hold distance, so the circle asked about always covers the radius around the position;
  • the offset is drawn once for a place and kept (a "lock record") until the position moves beyond a hold distance. A new draw for the same place would let the requests be averaged back to it;
  • the zone codes are sorted, so their order says nothing about the centre.

Your question about a cap on the radius

I have left the radius without an upper limit in these modules, with 100 km as the intended default. The cost grows with the radius and I would leave that choice to the user. If you want a cap, tell me the number: it is one line in the form, which comes in the last piece.

What a poll's local work costs, on the real dataset (11,651 zones, c985ed4f), on an x86 machine, with an alert on every zone as the worst case, over 162 polls at nine places:

Radius Requests to NWS per poll Alerts in the reply
16 km 1 3 to 29
100 km 1 5 to 135
161 km 1 to 2 11 to 236
400 km 1 to 10 15 to 1,172

Building the request took 5.4 ms at the median and 164 ms at worst; placing the reply took 2.7 ms at the median and 77 ms at worst. The worst cases are the 400 km ones. The later piece that calls these runs them in the executor, never on the event loop. I have not measured this on a Yellow or a Pi.

A request is at most 300 zones. NWS accepted 426 zones in one request and answered HTTP 500 at 462, so a longer list is divided and the replies merged.

How it was checked

  • Gates at the head of the branch: pytest 2808 passed, 20 skipped; cov 98%; flow cov 100%; ruff check ok; ruff format ok; mypy ok.
  • 327 tests in this pull request: 183 for the offset, 135 for the placing, 9 for the parity script against a stand-in for the service. They include property tests: every offset drawn lies inside its range, a store loads from whatever text it is given, a long list is divided without losing a zone, and a record that cannot be read never costs the reply its other alerts.
  • Two further tests on the real zones, in tests/test_zone_dataset_acceptance.py, which skip without the shapefiles. With the 16 April shapefiles they pass here.
  • With the later piece that calls these modules, on the real dataset: over 162 polls, placing through the offset request gave the same alerts as placing the whole country directly.
  • Live, 25 November 2026, with the pinned dataset: 310 points over five runs of the check, some aimed at the areas with polygon warnings. 307 matched point=, some with extra alerts that the tolerance band explains (32 of the 228 points in the last two runs).

What the live check found

Two points differed every time they were asked, four times each. Both are on the coast of Santa Catalina Island (33.3205,-118.3785 and 33.4777,-118.5824). The placing kept a Coastal Flood Advisory and a High Surf Advisory there that point= did not return. Nothing point= returned was missing.

The two points lie 72 m and 90 m inside the dataset's outline of the island's zones (CAZ087, CAC037, CAZ287). The shapes are simplified to within 567 m, and the service decides on the shape as surveyed, so the outline reaches a little past the real coastline and a point in that margin is on the island for the dataset and in the water for the service. A point 3.7 km inland agrees with point=.

This is the tolerance band seen from the other side. The band covers a point just outside a zone; this is a point just inside one. Either way the entry shows an alert for a place a few hundred metres away, and the marine zone counts at those two points as well. The placing is unchanged. The last commit changes the check: an extra alert for a zone whose edge is within the tolerance of the point is reported as "extra, at a zone's edge" and passes. An extra alert still fails when the point is inside the alert's own polygon or deeper inside one of its zones than the tolerance. Asked again with that commit, the two points read MATCH+ and the inland one MATCH.

One further point differed in the first run. I did not capture it, and I cannot say whether it was the same thing.

A note for anyone running the check: the two replies the service gives for one point were stamped up to 288 seconds apart, with a median of zero, so an alert issued or ended between them can also show as a difference. None of the differences I captured was of that kind.

What comes after

So you can see where this leads. Each is a separate pull request, and nothing is reachable by a user until the last.

  1. Offset position and 2. Placing: this pull request.
  2. Local mode in the provider: a poll that asks by zone list around the offset position and places the reply, with the lock records kept on disk. No switch yet.
  3. What the entry shows: a count at the position, a count nearby, and a distance on each alert.
  4. Zone data delivery: the download of the dataset, and its removal with the last entry that uses it, as you asked in chore(nws): load the zone dataset from a fixed path, known files only #434.
  5. The switch: the option in the forms, and the README.
    Refs NWS: area-scoped GPS/tracker mode that never sends coordinates (local point matching, multiple areas) #316

Round 3 of seevee#316. A zone
request calculated from the position itself can be turned back into
the position by anyone who has the zone shapes. This module gives
the request a different centre. Nothing calls it yet, and it has no
Home Assistant imports.

The centre is the position moved in a random direction by 15 to 35
percent of the radius, and never by less than 15 to 35 miles. The
request radius is enlarged by the largest offset that could have
been drawn plus the hold distance, so the circle asked about always
covers the radius around the position.

An offset is drawn once for a place and kept as a lock record:
where it was drawn, how far the position may move before it is
drawn again, and the offset. A position inside a record's hold
radius gets that record's offset. A new draw for the same place
would let the requests be averaged back to it. The store holds at
most 20,000 records and remembering can be switched off.

Records can be written out and read back. A stored record that
could not have been made is left out and the rest are kept, and
records still written whole in a damaged text are recovered.

Refs: seevee#316
Round 3 of seevee#316.
providers/nws_matching.py decides, on this system, what a zone
request should name and where each alert in the reply lies. Nothing
calls it yet, and it has no Home Assistant imports.

request_codes() names every zone in the circle it is given, with
the codes sorted so that the order says nothing about the centre,
and split_codes() divides a list too long for one request.

place_alerts() marks each alert as here, nearby with its distance
to the nearest edge, or beyond the radius, and only the last is
discarded. Here is what a request by point returns: an alert with a
polygon when the point is inside it, and an alert with zones only
when one of them is within its simplification tolerance of the
point, with the land and sea rule banded the same way. An alert
that cannot be placed is kept.

Refs: seevee#316
Round 3 of seevee#316. The
parity script now makes the request the local mode will make: zones
around an offset centre, over the enlarged circle, split when the
list is long. It places the reply with the integration's own
matching and compares the alerts placed at the point with what
point= returns. The verdicts are MATCH, MATCH+ when the tolerance
band alone admitted the extra alerts, and DIFFER, which is the only
one that fails.

tests/test_nws_zone_parity.py runs the check against a stand-in for
the service. tests/test_zone_dataset_acceptance.py gains two checks
on the real zones, skipped without the shapefiles: that a request
from an offset centre names every zone a position needs, and that a
tracker's minimum hold shows only the position's own alerts where
zones are smaller than it.

Refs: seevee#316
Round 3 of seevee#316. The
parity check called it a difference whenever the local placing kept
an alert that point= did not return and a zone containing the point
explained it. On 25 November 2026 that happened at two points on
the coast of Santa Catalina Island, every time they were asked: each
lies 70 to 90 m inside the dataset's outline of the island's zones,
and the service did not return two advisories that the placing kept.

The dataset's shapes are simplified to within each zone's
tolerance, 567 m here. Nearer an edge than that, the dataset cannot
say which side of the surveyed edge a point is on, and the service
decides on the surveyed one. An alert kept there is the tolerance's
cost, the same as an alert the band admits from just outside a
zone.

The check now reports such an alert as "extra, at a zone's edge",
lists the zones concerned, and gives MATCH+. An extra alert still
fails when the point is inside the alert's own polygon, which comes
from the service as drawn, or deeper inside one of its zones than
the tolerance. Something point= returned and the placing lacks
fails as before.

Refs: seevee#316
@systems-monitor systems-monitor changed the title chore(nws): fetch the zone dataset, verify it, then install it chore(nws): offset the zone request and place the alerts locally Nov 25, 2026
@seevee

seevee commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks, this is a big piece and it reads carefully. Catalina is a nice catch, and I agree with passing an extra alert at a zone's edge.

On the radius, I'd like the default at 0. Local mode should give what point= gives today unless someone asks for more, so nearby alerts are opt-in by setting a radius.

On the cap, a soft one. The form takes anything up to 400 km, the largest you've measured, and above 200 km it asks the user to confirm with the cost spelled out. Does that work for you? Since you're not running a dev instance, I'll take the form piece when we get there, confirm step included, and load each piece before that on my dev box as it comes in.

For the docs I still need reply size in bytes per poll on a busy day, and timings on small hardware. I can run the Yellow side if you give me a script.

Two things before I merge:

Last one, small. test_the_request_from_the_apparent_centre_names_every_zone_in_range runs on the 36 to 44 north lattice only. Could it run on northern too? That's where the projection is worst, and CI never sees the real-zone version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants