Ich habe den Chronjob und Alexa raus genommen!
Bitte schaut euch die Äderung an und passt zumindest den Teil mit dem Chronjob an!
Das script startet nun nicht zur vollen Viertelstunde sondern einmal beim start und dann alle 15 Minuten.
Ich hoffe damit die Abfragen auf die API https://verkehr.autobahn.de zeitmäßig zu verteilen!
Getestet, funktioniert:
Spoiler
/*
Beschreibung
Dieses Skript überwacht Verkehrswarnungen über die Autobahn‑API für die in der Variable `roadId` konfigurierte Autobahn.
Es filtert Meldungen nach dem definierten Koordinatenbereich (coordsArea).
Wichtig: Vor dem ersten Start unbedingt die Variable `roadId` auf die gewünschte Autobahn setzen (z. B. "A45", "A3" etc.).
Ausgaben (einzeln schaltbar):
- telegramBasicEnabled : Telegram‑Nachricht ohne Link
- telegramWithLinkEnabled : Telegram‑Nachricht mit Google‑Maps‑Link
- dpAlexaEnabled : Klartext‑Datenpunkt (z. B. idAlexa) für Alexa, OHNE Maps‑Link
- dpJsonEnabled : JSON‑Datenpunkt (z. B. idJSON) für Auswertungen, MIT Maps‑Link
Verhalten:
- Datenpunkte werden nur angelegt, wenn der jeweilige Schalter aktiviert ist.
- Beim Start führt das Skript einmal checkTraffic() aus und wiederholt dann alle 15 Minuten (relativ zur Startzeit).
- Maps‑Link wird nur erzeugt, wenn Koordinaten vorhanden sind.
Hinweise:
- Stelle diese Variablen korrekt ein:
- roadId
- coordsArea
- telegramInstance (normalerweise ist es telegram.0)
- telegramChatId
- idAlexa
- idJSON
- Passe die Schalter an
*/
// =========================
// Konfiguration
// =========================
const roadId = "A45";
const coordsArea = {
minLat: 50.6181514,
maxLat: 52.2742993,
minLon: 7.2582769,
maxLon: 8.3822071
};
// Schalter (0 = aus, 1 = an)
const telegramBasicEnabled = 1; // Telegram: Nachricht ohne Link
const telegramWithLinkEnabled = 1; // Telegram: Nachricht mit Maps-Link
const dpAlexaEnabled = 1; // Klartext-Datenpunkt (Alexa) ohne Link
const dpJsonEnabled = 1; // JSON-Datenpunkt (mit Link)
// Telegram-Konfiguration
const telegramInstance = "telegram.0";
const telegramChatId = 123456789;
// Datenpunkte (nur anlegen, wenn aktiviert)
const idAlexa = "0_userdata.0.Verkehr_Alexa";
const idJSON = "0_userdata.0.Verkehr_JSON";
if (dpAlexaEnabled === 1) {
createState(idAlexa, "", false, {
name: "Verkehrsmeldung Klartext",
type: "string",
role: "value"
});
}
if (dpJsonEnabled === 1) {
createState(idJSON, "[]", false, {
name: "Verkehrsmeldung JSON",
type: "string",
role: "json"
});
}
// API-URL
const url = `https://verkehr.autobahn.de/o/autobahn/${roadId}/services/warning`;
// =========================
// Hilfsfunktionen
// =========================
function isInArea(lat, lon, area) {
return lat >= area.minLat && lat <= area.maxLat && lon >= area.minLon && lon <= area.maxLon;
}
function warningHitsArea(w, area) {
if (w.coordinate && typeof w.coordinate.lat === "number" && typeof w.coordinate.long === "number") {
if (isInArea(w.coordinate.lat, w.coordinate.long, area)) return true;
}
if (w.geometry && Array.isArray(w.geometry.coordinates)) {
for (const c of w.geometry.coordinates) {
if (Array.isArray(c) && c.length >= 2) {
const lon = c[0], lat = c[1];
if (isInArea(lat, lon, area)) return true;
}
}
}
return false;
}
function buildDescriptionText(desc) {
if (Array.isArray(desc)) return desc.filter(d => d && String(d).trim() !== "").join("\n");
return String(desc || "").trim();
}
// =========================
// Hauptlogik
// =========================
async function checkTraffic() {
try {
console.log("Hole Daten von:", url);
const axios = require("axios");
const res = await axios.get(url);
const data = res.data;
const warnings = data.warnings || data.warning;
if (!warnings || !Array.isArray(warnings) || warnings.length === 0) {
console.log("Keine Warnungen gefunden.");
if (dpAlexaEnabled === 1) setState(idAlexa, "Keine Warnungen auf " + roadId);
if (dpJsonEnabled === 1) setState(idJSON, "[]");
return;
}
const relevant = warnings.filter(w => warningHitsArea(w, coordsArea));
if (!relevant || relevant.length === 0) {
console.log("Keine Warnungen im Bereich.");
if (dpAlexaEnabled === 1) setState(idAlexa, "Keine Warnungen im Bereich auf " + roadId);
if (dpJsonEnabled === 1) setState(idJSON, "[]");
return;
}
let alexaText = "";
const jsonOut = [];
for (const w of relevant) {
const desc = buildDescriptionText(w.description);
// Koordinaten extrahieren (verschiedene Felder)
let lat = null, lon = null;
if (w.coordinate) { lat = w.coordinate.lat; lon = w.coordinate.long; }
if ((!lat || !lon) && w.point) {
const p = String(w.point).split(",");
if (p.length === 2) { lat = p[0]; lon = p[1]; }
}
const mapLink = (lat && lon) ? `https://www.google.com/maps/@${lat},${lon},13.13z` : "";
// Telegram: Basic (ohne Link)
if (telegramBasicEnabled === 1) {
const msg = `⚠️ Störung auf ${roadId}:\n${w.title}\n${desc}`;
sendTo(telegramInstance, "send", { chatId: telegramChatId, text: msg });
}
// Telegram: With Link
if (telegramWithLinkEnabled === 1 && mapLink) {
const msg = `⚠️ Störung auf ${roadId}:\n${w.title}\n${desc}\n\n📍 ${mapLink}`;
sendTo(telegramInstance, "send", { chatId: telegramChatId, text: msg });
}
// Alexa-Klartext (ohne Link)
if (dpAlexaEnabled === 1) {
alexaText += `Störung auf ${roadId}: ${w.title}. ${desc}\n\n`;
}
// JSON-Ausgabe (mit Link wenn vorhanden)
if (dpJsonEnabled === 1) {
jsonOut.push({
road: roadId,
title: w.title,
description: desc,
start: w.startTimestamp || null,
coordinate: lat && lon ? { lat: lat, lon: lon } : null,
mapLink: mapLink || null
});
}
}
if (dpAlexaEnabled === 1) setState(idAlexa, alexaText.trim());
if (dpJsonEnabled === 1) setState(idJSON, JSON.stringify(jsonOut, null, 2));
} catch (err) {
console.error("Fehler beim Abruf:", err && err.message ? err.message : err);
if (telegramBasicEnabled === 1 || telegramWithLinkEnabled === 1) {
sendTo(telegramInstance, "send", {
chatId: telegramChatId,
text: `❌ Fehler beim Abruf der Verkehrsdaten: ${err && err.message ? err.message : err}`
});
}
if (dpAlexaEnabled === 1) setState(idAlexa, "Fehler beim Abruf der Verkehrsdaten");
if (dpJsonEnabled === 1) setState(idJSON, "[]");
}
}
// =========================
// Start sofort und Intervall alle 15 Minuten (relativ zur Startzeit)
// =========================
// sofort einmal ausführen
checkTraffic();
// danach alle 15 Minuten relativ zur Startzeit wiederholen
const intervalMs = 15 * 60 * 1000;
setInterval(checkTraffic, intervalMs);
Edit: Beschreibung in das Script geschrieben.