# Flow with delay time condition

**URL:** <https://community.homey.app/t/flow-with-delay-time-condition/113494>\
**Category:** Questions & Help\
**Tags:** flow, homey-pro\
**Created:** [July 27, 2024, 6:42am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494 "2024-07-27T06:42:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![focus11](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/focus11/32/77613_2.png) [@focus11](https://community.homey.app/u/focus11)\
**Post date:** [July 27, 2024, 6:42am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/1 "2024-07-27T06:42:53Z")

</div>

Hi all!  
I want to create a flow in which Homey Pro turns off my AC when the solar production ends up being lower than the house’s consumption for a set period of time. I want this because it often happens that a cloud passes and for a few seconds the value of the production drops below the consumption of the house and turns off my AC unnecessarily, because in a short time the cloud disappears and the production returns again higher than the consumption. That’s why an option would be useful that would allow me to set the automatic shutdown of a device when the solar production is lower than the consumption for a period of 10 min, for example.  
I found a version with a delay, but it only executes a delay of the stop command, no matter how small the period of time in which the production is lower than the consumption.  
For a more precise analysis I have a Victron inverter and Gree Fairy AC.  
Thanks in advance for any useful solution!

---

<div class="post-metadata">

**Author:** ![DirkH](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dirkh/32/78952_2.png) [@DirkH](https://community.homey.app/u/DirkH)\
**Post date:** [July 27, 2024, 9:17am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/2 "2024-07-27T09:17:38Z")

</div>

Hi @focus11,  
there are many examples in the forum with power/temperature/etc. higher or lower than X for Y minutes, which you can use.  
Man of them are using timer apps like Chronograph or any of these: [Search for Timer](https://homey.app/en-de/apps/browse/#filter?query=timer)  
I would prefer to store the global energy consumption or production (probably per day) and check every few minutes, how much it increased and the react on the outcome.  
Unfortunately I do not have a good example ready, but it is quite easy, depending on the additional conditions.  
Tell me, if you need more help.

---

<div class="post-metadata">

**Author:** ![focus11](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/focus11/32/77613_2.png) [@focus11](https://community.homey.app/u/focus11)\
**Post date:** [July 27, 2024, 9:30am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/3 "2024-07-27T09:30:10Z")

</div>

Thanks for the reply!  
I searched on the forum for something similar to my case, but I didn’t find it. In the end, my son helped me, who programmed for me a script exactly as I need it.  
Thanks again!

---

<div class="post-metadata">

**Author:** ![DirkG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dirkg/32/118897_2.png) [@DirkG](https://community.homey.app/u/DirkG)\
**Post date:** [July 27, 2024, 12:10pm UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/5 "2024-07-27T12:10:35Z")

</div>

Hi Aurel and welcome!

Even if you already have a solution, I would still like to show you a way to solve such a use case with Advanced Flows.

For the same use case I created already some flows. Additionally I created a virtual device which will be switched on/off depending on if the consumption of the house is below 0 watt (more PV energy available than consumption in the house) or the consumption of the house is more than 0 watt (less PV energy available than consumption in the house).  
By using a virtual device, or you can also create and use a yes/no variable, it’s easy possible to implement it into other flows to turn devices on or off.

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/4/242a32c6727121174900777234069f6f89f91987.jpeg)

⚠  
Please note the connection points at the “And…” flow cards, it’s really important!  
Blue means: This condition is true  
Yellow/Orange means: This condition is **not** true

Some users invert the cards, which makes the flow easier to read. However, this is too much work for me, so I always leave the cards as they are inserted… 😉

For creating the virtual device I used the [Device Capabilities](https://homey.app/en-ro/app/nl.qluster-it.DeviceCapabilities/Device-Capabilities/) app. For the timer, I used the [Chronograph](https://homey.app/en-ro/app/nl.fellownet.chronograph/Chronograph/) app, as already suggested by @DirkH.

Maybe you also share the script so that other users can also benefit from it?

---

<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:** [July 27, 2024, 3:31pm UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/6 "2024-07-27T15:31:45Z")

</div>

> [@focus11](#):
>
> programmed for me a script

Share, share!  
Meaning, when you ask for a solution, it’ll be much appreciated when you share the solution you found yourself in the meantime 😉

---

<div class="post-metadata">

**Author:** ![focus11](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/focus11/32/77613_2.png) [@focus11](https://community.homey.app/u/focus11)\
**Post date:** [August 16, 2024, 9:28am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/7 "2024-08-16T09:28:28Z")

</div>

Okay !  
Below you have the code of script flow, that is made by my son. I don’t know programming codes…

```auto
// Script oprire AC etaj

const timeRequiredToStopInMinutes = 6;
const timeRequiredToStop = timeRequiredToStopInMinutes * 60 * 1000; // 6 min (in ms)
const msBetweenChecks = 2000;

const iterationsRequiredToStop = timeRequiredToStop / msBetweenChecks;
let iterationsSinceProductionIsLessThanConsumption = 0;

let minSeparator = '';
if (timeRequiredToStopInMinutes >= 20) {
  minSeparator = 'de ';
}

while (true) {
  const ac = await Homey.devices.getDevice({ id: '8955e5e5-83f9-4563-8a2d-1330c7a01eab' });
  const isAcOn = ac.capabilitiesObj['onoff'].value;

  console.log('[Script AC etaj] Running...');

  if (isAcOn) {
    const victron = await Homey.devices.getDevice({ id: 'ff6580a4-623e-4c5f-8e06-9ccf153d053c' });
    
    const production = victron.capabilitiesObj['measure_power.PV'].value;
    const consumption = victron.capabilitiesObj['measure_power'].value;

    if (production < consumption) {
      iterationsSinceProductionIsLessThanConsumption++;
    }
    else {
      iterationsSinceProductionIsLessThanConsumption = 0;
    }

    console.log('[Script AC etaj] Production:', production);
    console.log('[Script AC etaj] Consumption:', consumption);
    console.log('[Script AC etaj] Iterations:', iterationsSinceProductionIsLessThanConsumption);

    if (iterationsSinceProductionIsLessThanConsumption >= iterationsRequiredToStop) {
      await ac.setCapabilityValue('onoff', false);
      await Homey.flow.runFlowCardAction({
        id: 'homey:manager:notifications:create_notification',
        args: {
          title: `Notificare din script`,
          text: `[AC Etaj] Oprit pentru că producția a fost mai mică decât consumul timp de ${timeRequiredToStopInMinutes} ${minSeparator}minute.`
        },
      });
      
      iterationsSinceProductionIsLessThanConsumption = 0;

      await wait(10000);
    }
  }

  await wait(msBetweenChecks);
}

```

---

<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:** [August 16, 2024, 10:12am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/9 "2024-08-16T10:12:11Z")

</div>

I don’t see how this script can work, since HomeyScript doesn’t allow long-running scripts 🤔

---

<div class="post-metadata">

**Author:** ![focus11](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/focus11/32/77613_2.png) [@focus11](https://community.homey.app/u/focus11)\
**Post date:** [August 16, 2024, 10:29am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/10 "2024-08-16T10:29:31Z")

</div>

Yes, is working very well

 ![Script](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/c/ac031c9f413ccadea7644e02f28134e9384b118c.jpeg)

---

<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:** [August 16, 2024, 10:33am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/11 "2024-08-16T10:33:45Z")

</div>

It probably just gets killed after 30 seconds every time.

---

<div class="post-metadata">

**Author:** ![focus11](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/focus11/32/77613_2.png) [@focus11](https://community.homey.app/u/focus11)\
**Post date:** [August 16, 2024, 10:46am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/12 "2024-08-16T10:46:31Z")

</div>

Not. The script runs for 7 days, until homey restarts automatically (that’s how I put a flow). Then I start the script manually and it works again for another 7 days until the next restart.  
I am very satisfied with its efficiency.

---

<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:** [August 16, 2024, 10:54am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/13 "2024-08-16T10:54:15Z")

</div>

Ah I see, HomeyScript actually contains a bug that allows certain scripts to run indefinitely 😃

---

<div class="post-metadata">

**Author:** ![focus11](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/focus11/32/77613_2.png) [@focus11](https://community.homey.app/u/focus11)\
**Post date:** [August 16, 2024, 11:49am UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/14 "2024-08-16T11:49:54Z")

</div>

the important thing is that it works perfectly, exactly as I need it 😜😉

---

<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:** [August 16, 2024, 2:33pm UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/15 "2024-08-16T14:33:20Z")

</div>

I’m curious about HS memory use.  
Goto  
[https://my.homey.app/insights](https://my.homey.app/insights) and enter `homeyscript mem` in the filter bar on top

---

<div class="post-metadata">

**Author:** ![focus11](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/focus11/32/77613_2.png) [@focus11](https://community.homey.app/u/focus11)\
**Post date:** [August 16, 2024, 5:05pm UTC](https://community.homey.app/t/flow-with-delay-time-condition/113494/16 "2024-08-16T17:05:41Z")

</div>

![1000061834](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/c/8cd27a2bfe09b8556a19e2dfbc8b38516a15542f.jpeg)
