HomeyScript : getting error : Invalid Token: string

I have a script that checks if a devic is not connected anymore, and it works perfectly in HomeyScript.

However, when I try to integrate it into flows to capture the result, I encounter the following error: *Invalid Token: string: Invalid Token: string

I try to replace log with return I have the same issue.

Thanks for your help.

This is the homey script :

// Obtenir tous les appareils
let devices = await Homey.devices.getDevices();

// Initialiser un tableau pour les appareils qui ne répondent pas
let nonResponsiveDevices = [];

// Parcourir chaque appareil
for (let deviceId in devices) {
  let device = devices[deviceId];

  // Vérifier si l'appareil est indisponible
  if (!device.available) {
    nonResponsiveDevices.push(device);
  }
}

// Afficher les appareils qui ne répondent pas
if (nonResponsiveDevices.length > 0) {
  console.log('Appareils qui ne répondent pas :');
  nonResponsiveDevices.forEach(device => {
    console.log(`- ${device.name} (Zone : ${device.zoneName})`);
  });
} else {
  console.log('Tous les appareils répondent correctement.');
}

The script doesn’t have a result, it doesn’t return anything nor does it set any tags.

Thanks Robert, I might be wrong, I’m running the script within a flow, It should return a text? I try ti replace console.log with return but I get th same error.

You have multiple calls to console.log(), which one did you replace with return?

Thanks Robert for your help, I re-think with ChatGPT the script, this one works fine :+1:

const devices = await Homey.devices.getDevices();
const zones = await Homey.zones.getZones(); // Récupérer la liste des zones
let nonResponsiveDevices = [];

// Parcourir chaque appareil
for (let deviceId in devices) {
  let device = devices[deviceId];

  // Vérifier si l'appareil est injoignable
  if (!device.available) {
    // Récupérer le nom de la zone en utilisant l'ID de la zone de l'appareil
    let zoneName = zones[device.zone]?.name || 'Zone inconnue';
    nonResponsiveDevices.push({ name: device.name, zoneName: zoneName });
  }
}

// Afficher les appareils qui ne répondent pas ou un message de confirmation
if (nonResponsiveDevices.length > 0) {
  let deviceList = 'Appareils qui ne répondent pas :\n';
  nonResponsiveDevices.forEach(device => {
    deviceList += `- ${device.name} (Zone : ${device.zoneName})\n`;
  });
  return deviceList;
} else {
  return 'Tous les appareils répondent correctement.';
}
1 Like