@Codierknecht
Hab es jetzt fast.
Am JS muss mir jmd helfen.
Da komme ich nicht weiter.
Die Ordner bekommen null Werte und beim Namen sämtlicher Ordner und DOs hätte ich gerne das selbe wie beim Pfad vom DP die kurze Variante ohne den Pfad vorher.
[image: 1767048997329-1000058373.jpg]
const SOURCE_DP = '0_userdata.0.zabbix.json';
const TARGET_ROOT = '0_userdata.0.zabbix.hosts';
// Hilfsfunktion: sichere ID
function normalize(name) {
return name
.toString()
.replace(/[^\w]/g, '_')
.replace(/_+/g, '_')
.replace(/^_|_$/g, '');
}
// JSON einlesen
const raw = getState(SOURCE_DP)?.val;
if (!raw) {
log('Zabbix JSON ist leer oder nicht vorhanden', 'warn');
return;
}
let data;
try {
data = JSON.parse(raw);
} catch (e) {
log('JSON Parsing fehlgeschlagen: ' + e, 'error');
return;
}
// Hosts durchlaufen
Object.keys(data).forEach(host => {
const hostId = `${TARGET_ROOT}.${normalize(host)}`;
// Host-Ordner anlegen (falls nicht vorhanden)
if (!existsObject(hostId)) {
createState(hostId, null, {
type: 'folder',
common: { name: host },
native: {}
});
}
const metrics = data[host];
Object.keys(metrics).forEach(metric => {
const m = metrics[metric];
const dpId = `${hostId}.${normalize(metric)}`;
const valueType = m.value_type === 'number' ? 'number' : 'string';
const value = valueType === 'number' ? Number(m.value) : String(m.value);
// Datenpunkt neu anlegen
if (!existsObject(dpId)) {
createState(dpId, value, {
type: valueType,
common: {
name: metric,
role: 'value',
unit: m.unit || '',
read: true,
write: false
},
native: {}
});
log(`DP angelegt: ${dpId}`);
}
// oder nur aktualisieren
else {
setState(dpId, value, true);
}
});
});