Ik heb een homey script laten maken door mijn vriend chatgpt waarmee nl-alerts worden opgehaald. deze informatie kun je dan weer gebruiken om apparaten aan te sturen. In mijn geval:
-De eerste paar zinnen van het bericht worden uitgesproken via GH-speakers
-Geautomatiseerder ramen worden gesloten.
-Handmatige ramen en deuren worden geconroleerd en gecorrigeerd door een spraakbericht via google home speakers. wanneer een deur of raam opent terwijl de alert actief is word hiervoor ook gewaarschuwd.
-Mechanische ventilatie word uitgeschakeld.
Bij een testbericht gebeurd dit alles ook, maar word het naar 1 minuut hersteld.
Mocht iemand geintereseerd zijn:
// Coƶrdinaten van je adres : maps in browser > rechtermuisknop op je huis >copy/paste > beperk tot 5 cijfers achter de comma > hieronder invoeren
const myLat = 52.XXXXX;
const myLon = 4.XXXXX;// Haversine afstandsfunctie
function getDistance(lat1, lon1, lat2, lon2) {
const R = 6371000;
const toRad = deg => deg * Math.PI / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat/2)**2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon/2)**2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}// Datum formatter
function formatDate(isoString) {
const date = new Date(isoString);
const pad = n => n.toString().padStart(2, ā0ā);
return${pad(date.getDate())}-${pad(date.getMonth() + 1)}-${date.getFullYear()} ${pad(date.getHours())}:${pad(date.getMinutes())}
;
}try {
// Fetch alerts
const URL = āhttps://api.public-warning.app/api/v1/providers/nl-alert/alertsā;
const response = await fetch(URL);
if (response.status !== 200) throw Error(āInvalid responseā);const json = await response.json();
const alerts = json.data || json;if (!Array.isArray(alerts) || alerts.length === 0) throw Error(āGeen alerts gevondenā);
let foundMessage = null;
let foundTimestamp = null;for (const alert of alerts) {
const info = alert.message || alert.info?.[0]?.description;
const polygon = alert.area?.[0];
if (!polygon) continue;const firstPoint = polygon.trim().split(' ')[0]; const [lat, lon] = firstPoint.split(',').map(Number); const dist = getDistance(myLat, myLon, lat, lon);
const alertTime = new Date(alert.start_at);
const now = new Date();
const ageInMs = now - alertTime;if (dist <= 10000 && ageInMs <= 24 * 60 * 60 * 1000) {
foundMessage = info || āNL-Alert actiefā;
foundTimestamp = formatDate(alert.start_at || now.toISOString());
break;
}
}if (!foundMessage) {
foundMessage = āGeen relevante NL-Alert in jouw buurtā;
foundTimestamp = formatDate(new Date().toISOString());
}// Tekst inkorten tot max 100 tekens, netjes eindigend op een punt als mogelijk
if (foundMessage.length > 100) {
const cutoff = foundMessage.substring(0, 100);
const lastDot = cutoff.lastIndexOf(ā.ā);
if (lastDot > 20) {
foundMessage = cutoff.substring(0, lastDot + 1); // netjes tot punt
} else {
const lastSpace = cutoff.lastIndexOf(ā ');
foundMessage = cutoff.substring(0, lastSpace > 0 ? lastSpace : 97) + āā¦ā;
}
}// Haal alle logic variables op en zet ze in een array
const varsObj = await Homey.logic.getVariables();
const vars = Object.values(varsObj);// Vind de variables op naam
const messageVar = vars.find(v => v.name === ānlAlertMessageā);
const timestampVar = vars.find(v => v.name === ānlAlertTimestampā);if (!messageVar || !timestampVar) {
throw new Error(āVariabelen nlAlertMessage of nlAlertTimestamp niet gevondenā);
}// Update de variables
await Homey.logic.updateVariable({ id: messageVar.id, variable: { value: foundMessage } });
await Homey.logic.updateVariable({ id: timestampVar.id, variable: { value: foundTimestamp } });return
NL-Alert bijgewerkt: ${foundMessage} (Ontvangen op ${foundTimestamp})
;} catch (error) {
returnFout: ${error.message}
;
}