Stations: drop impossible sensor readings; chart ticks always fit
A sensor once reported 21948 °C / 0 % for 20 minutes, and every chart range covering it failed with a server error because no tick step fit. A reading outside -50..70 °C is now dropped together with its humidity, humidity outside 0-100 % on its own (sensors.plausible), and the chart's tick step falls back to a power of ten. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ad4b6aa22f
commit
105698082b
2 changed files with 20 additions and 2 deletions
|
|
@ -22,6 +22,22 @@ from . import sources
|
||||||
|
|
||||||
SAVE_EVERY_N_POLLS = 10 # ~5 min at 30 s polls
|
SAVE_EVERY_N_POLLS = 10 # ~5 min at 30 s polls
|
||||||
HISTORY_MAX_AGE_S = 24 * 3600 # discard older points on load
|
HISTORY_MAX_AGE_S = 24 * 3600 # discard older points on load
|
||||||
|
TEMP_OK = (-50.0, 70.0) # °C a room or the outdoors can be; outside = a failed read
|
||||||
|
|
||||||
|
|
||||||
|
def plausible(t, h):
|
||||||
|
"""A failed sensor read (22 Oct 2026: "Shelf" sent 21948 °C / 0 % for 20 min) → no reading at all:
|
||||||
|
an impossible temperature drops the humidity of that read too; humidity outside 0–100 % alone."""
|
||||||
|
try:
|
||||||
|
t = None if t is None else float(t)
|
||||||
|
h = None if h is None else float(h)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None, None
|
||||||
|
if t is not None and not TEMP_OK[0] <= t <= TEMP_OK[1]:
|
||||||
|
return None, None
|
||||||
|
return t, (h if h is None or 0 <= h <= 100 else None)
|
||||||
|
|
||||||
|
|
||||||
wake = threading.Event() # Settings ▸ Sensors saved: poll now
|
wake = threading.Event() # Settings ▸ Sensors saved: poll now
|
||||||
source_status = {} # source id → "ok · 14:05" / what went wrong
|
source_status = {} # source id → "ok · 14:05" / what went wrong
|
||||||
|
|
||||||
|
|
@ -68,7 +84,8 @@ def sensors_worker(store):
|
||||||
if r is None:
|
if r is None:
|
||||||
s.update(None, None, None, None) # offline
|
s.update(None, None, None, None) # offline
|
||||||
else:
|
else:
|
||||||
s.update(getattr(s, "fixed_name", "") or r["name"], r["t"], r["h"], r["age"])
|
t, h = plausible(r["t"], r["h"])
|
||||||
|
s.update(getattr(s, "fixed_name", "") or r["name"], t, h, r["age"])
|
||||||
bad = [v for v in status.values() if not v.startswith("ok")]
|
bad = [v for v in status.values() if not v.startswith("ok")]
|
||||||
store.sensors_status = ("ok · " + datetime.now().strftime("%H:%M")) if not bad else \
|
store.sensors_status = ("ok · " + datetime.now().strftime("%H:%M")) if not bad else \
|
||||||
(bad[0] if len(status) == 1 else f"{len(status) - len(bad)} of {len(status)} sources ok — {bad[0]}")
|
(bad[0] if len(status) == 1 else f"{len(status) - len(bad)} of {len(status)} sources ok — {bad[0]}")
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,8 @@ def chart(points, rng, span, step, now, units):
|
||||||
parts.append(f"{p[4]:.0f}%")
|
parts.append(f"{p[4]:.0f}%")
|
||||||
strips.append({"x": x(p[0]), "w": round(w, 1), "tip": " · ".join(parts)})
|
strips.append({"x": x(p[0]), "w": round(w, 1), "tip": " · ".join(parts)})
|
||||||
import math
|
import math
|
||||||
gap = next(g for g in (1, 2, 5, 10, 20) if (hi - lo) / g <= 5) # round steps, at most 5 lines
|
gap = next((g for g in (1, 2, 5, 10, 20) if (hi - lo) / g <= 5), # round steps, at most 5 lines
|
||||||
|
10 ** math.ceil(math.log10((hi - lo) / 5))) # (a range no sensor makes: never a crash)
|
||||||
yt = [v * gap for v in range(math.ceil(lo / gap), math.floor(hi / gap) + 1)]
|
yt = [v * gap for v in range(math.ceil(lo / gap), math.floor(hi / gap) + 1)]
|
||||||
return {"w": CHART_W, "h": CHART_H, "temp": temp_paths, "hum": hum_paths, "band": bands, "strips": strips,
|
return {"w": CHART_W, "h": CHART_H, "temp": temp_paths, "hum": hum_paths, "band": bands, "strips": strips,
|
||||||
"yticks": [{"y": y(v), "v": f"{v:.0f}{u}"} for v in yt],
|
"yticks": [{"y": y(v), "v": f"{v:.0f}{u}"} for v in yt],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue