# HomeyScript : getting error : Invalid Token: string

**URL:** <https://community.homey.app/t/homeyscript-getting-error-invalid-token-string/118465>\
**Category:** Questions & Help\
**Tags:** homeyscript\
**Created:** [October 18, 2024, 8:07am UTC](https://community.homey.app/t/homeyscript-getting-error-invalid-token-string/118465 "2024-10-18T08:07:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDO](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henrido/32/84845_2.png) [@HenriDO](https://community.homey.app/u/HenriDO)\
**Post date:** [October 18, 2024, 8:07am UTC](https://community.homey.app/t/homeyscript-getting-error-invalid-token-string/118465/1 "2024-10-18T08:07:13Z")

</div>

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 :

```auto
// 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.');
}

```

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [October 18, 2024, 8:11am UTC](https://community.homey.app/t/homeyscript-getting-error-invalid-token-string/118465/2 "2024-10-18T08:11:34Z")

</div>

> [@HenriDO](#):
>
> to capture the result

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

---

<div class="post-metadata">

**Author:** ![HenriDO](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henrido/32/84845_2.png) [@HenriDO](https://community.homey.app/u/HenriDO)\
**Post date:** [October 18, 2024, 8:16am UTC](https://community.homey.app/t/homeyscript-getting-error-invalid-token-string/118465/3 "2024-10-18T08:16:12Z")

</div>

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.

 ![Capture d’écran 2024-10-18 à 10.12.24](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/e/0e154cb6a3f783e802edaa242bbcaf9413953969.png)

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [October 18, 2024, 8:18am UTC](https://community.homey.app/t/homeyscript-getting-error-invalid-token-string/118465/4 "2024-10-18T08:18:38Z")

</div>

> [@HenriDO](#):
>
> 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`?

---

<div class="post-metadata">

**Author:** ![HenriDO](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henrido/32/84845_2.png) [@HenriDO](https://community.homey.app/u/HenriDO)\
**Post date:** [October 18, 2024, 8:34am UTC](https://community.homey.app/t/homeyscript-getting-error-invalid-token-string/118465/5 "2024-10-18T08:34:36Z")

</div>

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

```auto
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.';
}
```
