Weather: forecast daily, current temp from HA on each request, sensor poll stays 5s for testing

This commit is contained in:
James 2026-02-14 03:51:59 -05:00
parent 470f83bc5a
commit 94fa7db000
2 changed files with 19 additions and 2 deletions

View File

@ -377,7 +377,7 @@ async function updateWeather() {
} catch(e) {}
}
updateWeather();
setInterval(updateWeather, 1800000);
setInterval(updateWeather, 86400000); // once per day
// === ROOM SENSORS (7pm - 8am only) ===
const sensorEl = document.getElementById('room-sensors');

View File

@ -101,7 +101,24 @@ let weatherCache = { data: null, ts: 0 };
const SEVERE_EVENTS = ['hurricane', 'tropical storm', 'tornado', 'severe thunderstorm', 'flash flood', 'storm surge', 'tsunami', 'extreme wind'];
app.get('/api/weather', async (req, res) => {
if (weatherCache.data && Date.now() - weatherCache.ts < 1800000) {
// Refresh current temp from HA every request (cheap, local), forecast/NHC daily
const forecastFresh = weatherCache.data && Date.now() - weatherCache.ts < 86400000;
if (forecastFresh) {
// Just update current temp from HA
try {
const haData = await new Promise((resolve, reject) => {
const r = http.get(`${HA_URL}/api/states/sensor.air_temp`, {
headers: { 'Authorization': `Bearer ${HA_TOKEN}` }
}, (resp) => {
let body = '';
resp.on('data', c => body += c);
resp.on('end', () => { try { resolve(JSON.parse(body)); } catch(e) { reject(e); } });
});
r.on('error', reject);
r.setTimeout(3000, () => { r.destroy(); reject(new Error('timeout')); });
});
weatherCache.data.current = parseInt(haData.state);
} catch(e) {}
return res.json(weatherCache.data);
}
try {