Homey script functionality taking into account changing IDs after Homey restart:
- if TV is off and Sonos on: show album art
- else show Ring doorbell image
Use return value as a parameter to send this value to Dashboard Studio, and use this in a Dashboard Studio image.
Google Gemini is very useful for programming Homey scripts.
// 1. Configuratie (id in Homey Developer Tools/Devices, vervang id door eigen waarden)
const SONOS_ID = 'b29a89d8-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const TV_ID = '3ee1a967-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const RING_ID = '9a6ccaf1-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
const BASE_URL = "https://[Your Homey id].homey.athom-prod-euwest1-001.homeypro.net/api/image/"; //pad in Homey Developer Tools/Images
// 2. Haal specifiek de benodigde apparaten op
const sonos = await Homey.devices.getDevice({ id: SONOS_ID });
const tv = await Homey.devices.getDevice({ id: TV_ID });
const ring = await Homey.devices.getDevice({ id: RING_ID });
// 3. Validatie
if (!sonos || !tv || !ring) {
console.error("Configuratie fout: Sonos, TV of Ring niet gevonden.");
return null;
}
// 4. Status bepalen Sonos en TV (true of false)
const isSonosPlaying = sonos.capabilitiesObj?.speaker_playing?.value ?? false;
const isTvOn = tv.capabilitiesObj?.onoff?.value ?? false;
// 5. Logica voor selectie (TV uit EN Sonos aan -> Album art, anders Ring)
const useSonos = !isTvOn && isSonosPlaying;
const imageId = useSonos
? (sonos.images[0]?.imageObj?.id)
: (ring.images[0]?.imageObj?.id);
if (!imageId) {
console.error("Geen afbeelding ID gevonden op het geselecteerde apparaat.");
return null;
}
// 6. Genereer URL (cache voorkomen)
const cacheBuster = Math.floor(Math.random() * 10000) + 1;
const finalUrl = BASE_URL + imageId + "?rand=" + cacheBuster;
console.log("Gegenereerde URL: " + finalUrl);
// 7. Return resultaat
return finalUrl;