# Script status alle lights on/off and dim and create variables

**URL:** <https://community.homey.app/t/script-status-alle-lights-on-off-and-dim-and-create-variables/131330>\
**Category:** Questions & Help\
**Created:** [February 22, 2025, 5:07pm UTC](https://community.homey.app/t/script-status-alle-lights-on-off-and-dim-and-create-variables/131330 "2025-02-22T17:07:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Monninkhof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/brian_monninkhof/32/59307_2.png) [@Brian\_Monninkhof](https://community.homey.app/u/Brian_Monninkhof)\
**Post date:** [February 22, 2025, 5:07pm UTC](https://community.homey.app/t/script-status-alle-lights-on-off-and-dim-and-create-variables/131330/1 "2025-02-22T17:07:38Z")

</div>

Hi everyone,  
Can anyone help me with this script. ChatGPT wrote it, so blame it ;-). Somehow it doesn’t create a fully working script, but something lik 70%.

```auto
// Functie om een variabele te controleren en aan te maken als deze niet bestaat
async function checkAndCreateVariable(variableName, variableType, initialValue = 0) {
  try {
    console.log(`Controleren of variabele voor ${variableName} bestaat...`);

    // Haal de bestaande variabelen op
    const existingVariables = await Homey.logic.getVariables();

    // Controleer of de variabele al bestaat
    if (existingVariables[variableName]) {
      console.log(`Variabele ${variableName} bestaat al.`);
      return null; // Geen nieuwe variabele nodig
    }

    console.log(`Variabele ${variableName} bestaat niet, wordt aangemaakt...`);

    // Correcte manier om een variabele aan te maken
    await Homey.logic.createVariable({
      name: variableName,
      type: variableType,
      value: initialValue
    });

    console.log(`Variabele ${variableName} succesvol aangemaakt.`);
    return variableName;
    
  } catch (error) {
    console.error(`Fout bij het controleren of aanmaken van de variabele ${variableName}:`, error);
  }
}

// Functie om de status van lampen op te halen en nieuwe variabelen aan te maken
async function getLampStatuses() {
  try {
    const createdVariables = []; // Lijst van aangemaakte variabelen

    // Verkrijg de lijst van apparaten in Homey
    const devices = await Homey.devices.getDevices();

    // Filter alleen de apparaten die lampen zijn
    const lamps = Object.values(devices).filter(device => device.class === 'light');

    console.log('Beschikbare lampen:', lamps);

    for (let lamp of lamps) {
      const lampStatus = lamp.capabilitiesObj?.onoff?.value || false; // Aan/uit status
      const brightness = lamp.capabilitiesObj?.dim?.value || 0; // Helderheid

      console.log(`Status voor ${lamp.name}: ${lampStatus ? 'Aan' : 'Uit'}, Helderheid: ${brightness}`);

      // Variabelenamen op basis van de lampnaam
      const statusVariableName = `${lamp.name.replace(/\s+/g, '_')}_Aan`;
      const brightnessVariableName = `${lamp.name.replace(/\s+/g, '_')}_Helderheid`;

      // Status-variabele controleren en aanmaken
      const createdStatusVariable = await checkAndCreateVariable(statusVariableName, 'boolean', lampStatus);
      if (createdStatusVariable) createdVariables.push(createdStatusVariable);

      // Helderheid-variabele controleren en aanmaken
      const createdBrightnessVariable = await checkAndCreateVariable(brightnessVariableName, 'number', brightness);
      if (createdBrightnessVariable) createdVariables.push(createdBrightnessVariable);
    }

    if (createdVariables.length > 0) {
      console.log('De volgende variabelen zijn succesvol aangemaakt:');
      createdVariables.forEach(variableName => console.log(`- ${variableName}`));
    } else {
      console.log('Er zijn geen nieuwe variabelen aangemaakt.');
    }
    
  } catch (error) {
    console.error('Fout bij het ophalen van lampstatus:', error);
  }
}

// Voer de functie uit
getLampStatuses();

```

---

<div class="post-metadata">

**Author:** ![Brian\_Monninkhof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/brian_monninkhof/32/59307_2.png) [@Brian\_Monninkhof](https://community.homey.app/u/Brian_Monninkhof)\
**Post date:** [February 22, 2025, 5:08pm UTC](https://community.homey.app/t/script-status-alle-lights-on-off-and-dim-and-create-variables/131330/2 "2025-02-22T17:08:24Z")

</div>

This is the output:

> Variabele Toilet\_boven\_Helderheid bestaat niet, wordt aangemaakt…  
> Fout bij het controleren of aanmaken van de variabele Toilet\_boven\_Helderheid: Error: Missing Parameter: variable  
> at ManagerClass.value (/app/node\_modules/homey-api/lib/HomeyAPI/HomeyAPIV3/Manager.js:127:27)  
> at checkAndCreateVariable (opslaan van de lampstatus (handmatig variabele aanmaken).js:18:23)  
> at process.processTicksAndRejections (node:internal/process/task\_queues:95:5)  
> at async getLampStatuses (opslaan van de lampstatus (handmatig variabele aanmaken).js:60:41)  
> Er zijn geen nieuwe variabelen aangemaakt.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [February 23, 2025, 2:26am UTC](https://community.homey.app/t/script-status-alle-lights-on-off-and-dim-and-create-variables/131330/3 "2025-02-23T02:26:06Z")

</div>

Just use tags instead of variables, much easier.  
Inside Homeyscript you will find an example-tag example script!

N.b. Please use a translation tool for the error output, when you post in the English section.
