Log both Sense HAT thermometers, and migrate schemas that predate them

The board carries two independent thermometers and the code averaged them
into temp_raw without ever recording either. Measured over 12 samples on a
real station: HTS221 30.973 C at sd 0.060, LPS25HB 29.810 C at sd 0.443, a
standing gradient of 1.163 C with the SoC at 44.55 C.

Two things follow from that and neither is possible without the raw channels.
A plain average of a quiet sensor and one seven times noisier lands at sd
0.223 where inverse-variance weighting reaches 0.060, and the gradient between
two chips at different distances from the SoC is a second observation of
self-heating that could identify the compensator's k with no reference
thermometer. Both need history, and history cannot be backfilled, so the
columns land on their own ahead of the work that consumes them.

CREATE TABLE IF NOT EXISTS is a no-op against a table that already exists, so
adding to COLUMNS would have reached a fresh install and silently missed every
station already running, then surfaced as an OperationalError inside
insert_telemetry. That sits on the sample loop, so it takes a station down
rather than leaving a gap. Store now reconciles the table against COLUMNS on
open, which makes every future column addition safe rather than just this one.

The simulator gains the same two channels, with couplings solved so their
forward models average to exactly the k = 0.55 the compensator is tuned
against. Aggregate behaviour is unchanged; only the per-channel detail is new.
Simulated temp_raw noise does rise from 0.05 to 0.223, which is not a
regression but the end of an over-optimistic figure: it was modelling the
quiet sensor and calling it the average.

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
2026-08-19 18:41:46 +01:00
co-authored by Claude Opus 5
parent 485affe956
commit 154071a791
5 changed files with 159 additions and 6 deletions
+20 -3
View File
@@ -58,6 +58,12 @@ from ashvale.physics import ( # noqa: E402
sea_level_pressure,
solar_position,
)
from ashvale.sensors import ( # noqa: E402
K_HTS221,
K_LPS25HB,
SD_HTS221,
SD_LPS25HB,
)
from ashvale.storage import Store # noqa: E402
@@ -130,8 +136,15 @@ def generate(days: float, step_s: int, lat: float, lon: float,
# model must be its exact inverse: T_raw = (T + k T_cpu) / (1 + k).
# Generating it any other way bakes a bias into the synthetic data that
# no amount of calibration can remove, and quietly caps your skill score.
k_true = 0.55
temp_raw = (temp + k_true * cpu) / (1.0 + k_true) + 0.05 * rng.normal(size=n)
# Two thermometers, not one, because the board has two. Their forward
# models average to the k = 0.55 case this used to generate directly, so
# temp_raw is unchanged in expectation. Its noise is not: a real board
# averages sd 0.060 with sd 0.443 and lands at 0.223, where this used to
# claim 0.05. Simulating the quiet sensor and calling it the average is
# what let an over-optimistic measurement noise go unnoticed.
temp_h = (temp + K_HTS221 * cpu) / (1.0 + K_HTS221) + SD_HTS221 * rng.normal(size=n)
temp_p = (temp + K_LPS25HB * cpu) / (1.0 + K_LPS25HB) + SD_LPS25HB * rng.normal(size=n)
temp_raw = (temp_h + temp_p) / 2.0
# If the compensator will move RH from the element temperature onto the air
# temperature, the forward model here must be its exact inverse, or the
# synthetic data bakes in a bias no calibration can remove. Same trap as the
@@ -146,7 +159,9 @@ def generate(days: float, step_s: int, lat: float, lon: float,
press_station += 0.05 * rng.normal(size=n)
return {
"ts": ts, "temp": temp, "temp_raw": temp_raw, "rh": rh_sensor + 0.4 * rng.normal(size=n),
"ts": ts, "temp": temp, "temp_raw": temp_raw,
"temp_h": temp_h, "temp_p": temp_p,
"rh": rh_sensor + 0.4 * rng.normal(size=n),
"press": press_station, "press_slp": press_slp, "cpu": cpu,
"lux": lux * (0.85 + 0.3 * rng.random(n)), "dew": dew, "cloud": cloud,
}
@@ -201,6 +216,8 @@ def main() -> None:
store.insert_telemetry({
"ts": ts,
"temp_raw": data["temp_raw"][i],
"temp_h": data["temp_h"][i],
"temp_p": data["temp_p"][i],
"temp_c": est["temp_c"],
"temp_smooth": est["temp_smooth"],
"temp_rate": est["temp_rate"],