Hej @VietNgoc ;
works fine and it’s helpful… Thanks for that…
I need some more informations in the CSV-Table… I tried to add my points, but it doesn’t work… I ask the KI as i am not famliar in coding JS.
Maybe you are so kind to check my Code made by KI? It based on your code here.
I “only” want to add and want it in this order “Entität”, “Entität-ID”, “Device”, “Area”, “Integration”, “Status”…
And this is the code i tried with:
async function downloadEntitiesCSV() {
// Home Assistant-Objekt mit allen Entitäten holen
const hass = document.querySelector('home-assistant').hass;
const entities = hass.entities;
// Asynchrone Abfrage der Device Registry und Area Registry von Home Assistant
const devices = await hass.connection.sendMessagePromise({
type: "config/device_registry/list"
});
const areas = await hass.connection.sendMessagePromise({
type: "config/area_registry/list"
});
// Hilfsobjekte zur schnellen Zuordnung von device_id bzw. area_id zu Namen
const deviceNameById = {};
devices.forEach(device => {
deviceNameById[device.id] = device.name || device.name_by_user || '';
});
const areaNameById = {};
areas.forEach(area => {
areaNameById[area.area_id] = area.name || area.name_by_user || '';
});
// Sorted Array aller Entities nach entity_id alphabetisch sortiert
const sorted = Object.values(entities).sort((a, b) => {
const idA = a.entity_id?.toLowerCase() || '';
const idB = b.entity_id?.toLowerCase() || '';
return idA.localeCompare(idB);
});
// CSV-Header mit gewünschten Spalten
let csvContent = "Entität,Entity ID,Gerät,Bereich,Integration,Status,Platform\n";
// Jede Entität zur CSV hinzufügen
sorted.forEach(entity => {
// Friendly Name (menschlich lesbarer Name)
const friendlyName = entity.attributes?.friendly_name || '';
// Entity ID (eindeutige ID in HA)
const entityId = entity.entity_id || '';
// Geräte-Name aus Device Registry, basierend auf context.device_id
const deviceId = entity.context?.device_id || '';
const device = deviceNameById[deviceId] || '';
// Bereichs-Name aus Area Registry, basierend auf area_id des Geräts
const deviceAreaId = devices.find(d => d.id === deviceId)?.area_id || '';
const area = areaNameById[deviceAreaId] || '';
// Integration – benutzt platform-Zuweisung als Proxy
const integration = entity.integration || entity.platform || '';
// Status = aktueller Zustand der Entität (z.B. on, off)
const status = entity.state || '';
// Platform (mqtt, zwave, template...)
const platform = entity.platform || '';
// Zeile für CSV
csvContent += `"${friendlyName}","${entityId}","${device}","${area}","${integration}","${status}","${platform}"\n`;
});
// CSV-Datei als Blob erzeugen
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
// Download-URL erzeugen und Klick auf unsichtbaren Link simulieren
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", "hass_entities.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
And this is the result:
