How to insert a delay between rules in a function node?

Hi all,

I'm using Node Red to control devices around my home based on

  • time of day
  • Solar production
  • Price of electricity
  • How much energy I'm exporting.

While the rules themselves are working as expected, the issue I'm facing is when solar production suddenly jumps.
The EmonPower factor below is based on a baseline value of 10000 (neither importing or exporting).
Any value below this is how many watts are being exported.
I've set three switches to different 'turn on' values, but if exporting suddenly jumps by say 1000watts, several rules take effect, EmonPower goes back into negative and so switches are then turning off (also at varying EmonPower values). It does manage to find itself an equilibrium, and although these are mostly dump devices (heating elements), I wonder if there's a way to add a delay.

Ideally, assess 'switchD', if a command to turn on is sent, wait 5 seconds before considering 'switchC'

//Daytime - SOLAR PRODUCTION - Rules - Fish Tank - ***TURN ON***
if (currentHour >= 07 && currentHour <= 19  && EmonPower < 9550 || currentWholesalePrice < 16) {
    updatedStates['switchD'] = 1;
//Daytime - SOLAR PRODUCTION - Rules - Fish tank LOW POWER  ***TURN ON*** C
if (currentHour >= 07 && currentHour <= 19  && EmonPower < 9400 || currentWholesalePrice < 16) {
    updatedStates['switchC'] = 1;
//Daytime - SOLAR PRODUCTION - Rules - Fish tank HIGH POWER  ***TURN ON***  Single 300w heater
if (currentHour >= 07 && currentHour <= 19  && EmonPower < 9600 || currentWholesalePrice < 16) {
    updatedStates['switchA'] = 1;

Thanks in advance

Couple of potential ways to do that.

You could split the function node and have each behind a delay node.

Or you could move the secondary code into a JavaScript timeout function (setTimeout). Just note that in this case, your function node will immediately execute and exit, the timeout code will run after the set time (in ms) so if you want a msg output from it, you will need to use node.send rather than return.