Flow with delay time condition

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!

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
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.

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!

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.

:warning:
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… :wink:

For creating the virtual device I used the Device Capabilities app. For the timer, I used the Chronograph app, as already suggested by @DirkH.

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

2 Likes

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 :wink:

1 Like

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

// 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);
}
3 Likes

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

Yes, is working very well

It probably just gets killed after 30 seconds every time.

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.

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

1 Like

the important thing is that it works perfectly, exactly as I need it :stuck_out_tongue_winking_eye::wink:

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