@borsti84 here you go - IOBroker JavaScript. Einmal via Datenpunkt und ebenso UDP send to Loxone
// === Konfiguration ===
const ip = '192.168.178.xx';
const port = 8214;
const password = '999999';
const abfrageIntervall = 10 * 1000; // 10 Sekunden
// === UDP-Konfiguration für Loxone ===
const udpHost = '192.168.178.xx';
const udpPort = 7000;
// === interner Zustand zum Vergleich bei Änderungen ===
const lastValues = {
Heizung: null,
Warmwasser: null,
Gesamt: null
};
// === UDP-Funktion ===
function sendeUDP(name, wert) {
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
const formatted = wert.toFixed(2); // Punkt als Dezimaltrennzeichen
const befehl = `WP.${name}=${formatted}`;
const message = Buffer.from(befehl);
client.send(message, 0, message.length, udpPort, udpHost, (err) => {
if (err) {
console.error(`❌ Fehler beim Senden an Loxone (${name}):`, err.message);
} else {
console.log(`📤 UDP gesendet: ${befehl}`);
}
client.close();
});
}
let leistungsaufnahmeId = null;
// === WebSocket einrichten ===
const WebSocket = require('ws');
const ws = new WebSocket(`ws://${ip}:${port}`, 'Lux_WS');
ws.on('open', () => {
console.log('✅ WebSocket verbunden – Sende Login...');
ws.send(`LOGIN;${password}`);
});
ws.on('message', async (data) => {
let raw = "";
if (Buffer.isBuffer(data)) {
raw = data.toString('utf8');
} else if (typeof data === "string") {
raw = data;
} else {
console.error("❌ Unbekannter Nachrichtentyp:", typeof data);
return;
}
try {
const msg = JSON.parse(raw);
if (msg.type === "Navigation") {
leistungsaufnahmeId = findIdByName(msg.items, "Leistungsaufnahme");
if (leistungsaufnahmeId) {
ws.send(`GET;${leistungsaufnahmeId}`);
setInterval(() => {
ws.send(`GET;${leistungsaufnahmeId}`);
}, abfrageIntervall);
} else {
console.error("❌ Konnte ID für 'Leistungsaufnahme' nicht finden!");
}
}
if (msg.type === "Content" && msg.name === "Leistungsaufnahme" && msg.items) {
for (const item of msg.items) {
const name = item.name.trim(); // z. B. "Gesamt"
const rawValue = item.value.trim(); // z. B. "5000.0 kWh"
const numericValue = parseFloat(rawValue.replace(",", "."));
const id = `javascript.0.Wärmepumpe.Leistungsaufnahme.${name}`;
// Datenpunkt anlegen, falls nicht vorhanden
if (!existsObject(id)) {
await createStateAsync(id, 0, {
name: `Leistungsaufnahme ${name}`,
unit: 'kWh',
read: true,
write: true,
type: 'number'
});
}
// Schreiben in ioBroker nur bei Änderung oder null
const oldVal = getState(id)?.val;
if (oldVal !== numericValue) {
await setStateAsync(id, { val: numericValue, ack: true });
}
// UDP nur bei Änderung senden
if (lastValues[name] !== numericValue) {
sendeUDP(name, numericValue);
lastValues[name] = numericValue;
}
}
}
} catch (err) {
console.error("❌ Fehler beim Parsen:", err.message);
console.error(raw);
}
});
ws.on('close', () => {
console.warn('🔌 Verbindung zur Wärmepumpe getrennt!');
});
// === Funktion zur ID-Suche ===
function findIdByName(items, targetName) {
for (const item of items) {
if (item.name === targetName && item.id) {
return item.id;
}
if (item.items && item.items.length > 0) {
const result = findIdByName(item.items, targetName);
if (result) return result;
}
}
return null;
}