Created a script, tested and works. Can't make it work with a flow

I made a script to make my shutters go down when a certain °C is reached and also when a certain lux level is reached. When I test the script, it works but when I want to create an automated flow nothing happens.
How can I solve this?

thx

We can’t read your mind yet :grimacing::smiling_face:
Please share script, flow and error message(s), thanks.

Hint, in advanced flow you can run code right from a Homeyscript “Then” card: “Run code …”

The script:

// 🛠️ Tes appareils (noms réels Homey)
const idHueMotionSensor = '13d9a1d7-01a5-4ff2-82ba-6ff85fa586af';  // ID exact du capteur extérieur
const idVoletSalleDeBain = 'fc90b0ab-4674-4552-baa3-d403d2c330df';  // L'ID exact du volet salle de bain
const idVoletCuisineStudio = 'daae4439-8355-41ac-8caa-61afc367762b';  // ID exact du volet Cuisine Studio
const idVoletChambreStudio = 'c1eb7824-2b03-43e5-9e0f-c7dfdaba4688';  // L'ID exact du volet Chambre Studio
const idVoletRez = '89753380-58e2-420b-a173-97cac92b1765';  // ID exact du volet salon rez-de-chaussée

async function main() {
  // Récupération des appareils
  const devices = await Homey.devices.getDevices();
  
  const sensor = Object.values(devices).find(device => device.id === idHueMotionSensor);
  const voletSalleDeBain = Object.values(devices).find(device => device.id === idVoletSalleDeBain);
  const voletCuisineStudio = Object.values(devices).find(device => device.id === idVoletCuisineStudio);
  const voletChambreStudio = Object.values(devices).find(device => device.id === idVoletChambreStudio);
  const voletRez = Object.values(devices).find(device => device.id === idVoletRez);

  if (!sensor) {
    console.log('❌ Capteur extérieur non trouvé.');
    return;
  }

  // Lecture des valeurs
  const temperature = sensor.capabilitiesObj['measure_temperature'].value;
  const luminance = sensor.capabilitiesObj['measure_luminance'].value;

  // Lecture de la date actuelle
  const now = new Date();
  const mois = now.getMonth() + 1;
  const jour = now.getDate();

  console.log(`Température actuelle : ${temperature}°C`);
  console.log(`Luminosité actuelle : ${luminance} lux`);
  console.log(`Date actuelle : ${jour}/${mois}`);

  // Vérification de la période (entre 1er avril et 20 septembre)
  const estDansPeriode = 
    (mois > 4 && mois < 9) || 
    (mois === 4 && jour >= 1) || 
    (mois === 9 && jour <= 20);

  // Conditions (temp > 25°C et lux > 30000 et période correcte)
  if (temperature > 25 && luminance > 30000 && estDansPeriode) {
    console.log('Conditions OK, fermeture des volets...');

    // Fermer les volets existants uniquement
    if (voletSalleDeBain) {
      await voletSalleDeBain.setCapabilityValue('windowcoverings_state', 'down');
    } else {
      console.log('⚠️ Volet salle de bain non trouvé.');
    }

    if (voletCuisineStudio) {
      await voletCuisineStudio.setCapabilityValue('windowcoverings_state', 'down');
    } else {
      console.log('⚠️ Volet Cuisine Studio non trouvé.');
    }

    if (voletChambreStudio) {
      await voletChambreStudio.setCapabilityValue('windowcoverings_state', 'down');
    } else {
      console.log('⚠️ Volet Chambre Studio non trouvé.');
    }

    if (voletRez) {
      await voletRez.setCapabilityValue('windowcoverings_state', 'down');
    } else {
      console.log('⚠️ Volet salon rez-de-chaussée non trouvé.');
    }

  } else {
    console.log('Conditions non remplies, aucune action.');
  }
}

// Exécution
main().catch(console.error);

Flow:

For the flow I don’t have an error it just won’t automatically start

Thx

The trigger card only triggers once, when the temp was below 25° and then became greater than 25.
Just like how a thermostat functions.

When it’s already over 25°, and the temperature rises, it will never trigger.

Oh ok and is there a way to trigger it again at a higher temperature if the lux hasn’t been reached but reached once it’s 26°C? Or do I create a new script and flow? Thx

Yes, add another flow with
When..
Lux became greater than X
And..
logic card [temperature] is greater than 25
Then..

A bit like this logic card, you can just enter 25 into the second field:

Do you know how to find and insert a tag from a sensor?

No didn’t look it up yet. Thx for your help!

1 Like

Does this looks like it?

Yes.
You can add notification cards at Then and Else, to be able to see what happens. You can add the lux and temperature tags f.i., and the flow name.

1 Like

Thank you!

@Har_Dou for your future projects you may consider using triggers like this:
image
with this trigger flow is started every time your sensors updates value (of luminance in my case), but keep in mind it might be too often to run the whole flow (depends on frequency of your sensor synchronization), so you can add simple logic to continue flow only in valid scenarios, like this:


so you will not run your code every time your sensor updates but only if relevant… hope it helps for your future automations :wink:

1 Like

thx will try it