Timer function

467 views
Skip to first unread message

Kasper Jepsen

unread,
Jan 24, 2018, 1:41:19 PM1/24/18
to Node-RED
Hello,

Sorry for my very dump question :)

How do i make a time there sends Start/stop signals to 2 fans.
They must not run at same time.
but lets say they have to run 1min each then shift.

So:
Start fan 1
1min
Stop fan 1
Start fan 2
1min
stop fan 2
start fan1
etc

I hope it could be done with a smart timer function?

/Kasper

Mark Setrem

unread,
Jan 24, 2018, 3:46:33 PM1/24/18
to Node-RED
Check out the "Delay Node" which will delay sending a message to the following nodes.

[Timer Start]-------[Turn On Fan 1]
|
|
|
|--[DELAY NODE - 1 Min]----[Turn OFF Fan 1]

etc etc...

Kasper Jepsen

unread,
Jan 24, 2018, 4:19:55 PM1/24/18
to Node-RED
Thank you for the answer.

How to sync the 2 Timer start to make sure they will not shift in time so the 2 fans will be on at same time?

/Kasper

Colin Law

unread,
Jan 24, 2018, 4:33:24 PM1/24/18
to node...@googlegroups.com
Handle both fans down the same path of nodes.

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/cb742f64-efc7-43f6-af56-1667141fe241%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kasper Jepsen

unread,
Jan 24, 2018, 4:53:02 PM1/24/18
to Node-RED
Hi,

I need to send 4 different Mqtt messages? - so i need to split them up?
also i need to control the on/off time for both fans individual.

/Kasper

Den onsdag den 24. januar 2018 kl. 22.33.24 UTC+1 skrev Colin Law:
Handle both fans down the same path of nodes.

Colin
On 24 January 2018 at 21:19, Kasper Jepsen <rap...@gmail.com> wrote:
Thank you for the answer.

How to sync the 2 Timer start to make sure they will not shift in time so the 2 fans will be on at same time?

/Kasper

Den onsdag den 24. januar 2018 kl. 21.46.33 UTC+1 skrev Mark Setrem:
Check out the "Delay Node" which will delay sending a message to the following nodes.

[Timer Start]-------[Turn On Fan 1]                        
                              |                                          
                              |                                          
                              |                                          
                              |--[DELAY NODE - 1 Min]----[Turn OFF Fan 1]

etc etc...

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Colin Law

unread,
Jan 24, 2018, 4:59:33 PM1/24/18
to node...@googlegroups.com
Is it a fixed pattern you want to go through or something else?

Colin

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Colin Law

unread,
Jan 24, 2018, 5:00:56 PM1/24/18
to node...@googlegroups.com
On 24 January 2018 at 21:58, Colin Law <cla...@gmail.com> wrote:
Is it a fixed pattern you want to go through or something else?

Try and give us enough information so we can fully understand what you need to do.

Colin

Kasper Jepsen

unread,
Jan 24, 2018, 5:14:40 PM1/24/18
to Node-RED
I will give a shoot more:

I have 2 fans, i want to control the time they are on and off.
They must not run at same time.

example could be fan1 is running 20min each hour - fan2 is running 30min each hour (or 2min each 10min and 3min each 10min)
I need to send mqtt messages for each command - fan 1 on/off and fan 2 on/off.

so input: 
2x on time for each fan
1x timebase

output 4 different mqqt strings/tx

/Kasper

Colin

Zenofmud

unread,
Jan 24, 2018, 7:20:37 PM1/24/18
to node...@googlegroups.com
Will fan 2 start up as soon as fan 1 shuts off?
Will fan 1 wait for n minutes after fan 2 stops so not to be running twice in the hour?
Will the time between fan 1 running and fan 2 running be constant or variable?
Where will that value come from or will it be calculated 60-fan1 run time - fan 2 run time?
Will there be a delay from when fan 2 ends till fan 1 starts?
If there are two delay times, how will they be calculated?

Paul

Sent from my iPad

Kasper Jepsen

unread,
Jan 25, 2018, 2:44:34 AM1/25/18
to Node-RED
Se answers below

Will fan 2 start up as soon as fan 1 shuts off? Yes (maybe short delay to be sure fans is stopped)
Will fan 1 wait for n minutes after fan 2 stops so not to be running twice in the hour? Yes ( wait =  time_base - (run_fan1+Run_fan2))
Will the time between fan 1 running and fan 2 running be constant or variable? Constant
Where will that value come from or will it be calculated 60-fan1 run time - fan 2 run time? Constant or later from some control app - same for all values
Will there be a delay from when fan 2 ends till fan 1 starts? Same as nr2?
If there are two delay times, how will they be calculated? I think 1 "variable" delay is ok.

It dosent have to be perfect first time - just the basics. 

Colin Law

unread,
Jan 25, 2018, 3:41:39 AM1/25/18
to node...@googlegroups.com
On 25 January 2018 at 07:44, Kasper Jepsen <rap...@gmail.com> wrote:
Se answers below

Will fan 2 start up as soon as fan 1 shuts off? Yes (maybe short delay to be sure fans is stopped)
Will fan 1 wait for n minutes after fan 2 stops so not to be running twice in the hour? Yes ( wait =  time_base - (run_fan1+Run_fan2))
Will the time between fan 1 running and fan 2 running be constant or variable? Constant
Where will that value come from or will it be calculated 60-fan1 run time - fan 2 run time? Constant or later from some control app - same for all values
Will there be a delay from when fan 2 ends till fan 1 starts? Same as nr2?
If there are two delay times, how will they be calculated? I think 1 "variable" delay is ok.

It dosent have to be perfect first time - just the basics. 

Right, now we have some information to go on.  You have values coming in from somewhere else (eventually) to specify the timing and need to control the fans based on this information.  Because it is so variable I think you will probably have to write a function node to handle it.  Within a function node you can use setTimeout() for the timing and node.send(msg) to send the on/off messages out as required.

Unfortunately this is not the simplest of code to write, a certain amount of javascript experience will be necessary to get it right.

Perhaps there is an existing node or sample flow that will help you, I don't know.

Colin

AIOT MAKER

unread,
Jan 25, 2018, 9:49:55 AM1/25/18
to Node-RED
I may propose the following flow as a starting point. It needs some polishing but it is a working flow that can give you a head start.

You should setup the timing parameters in the first function node. The second function node is the main engine that will cycle the fans. It is coded to allow a dead-time (no fans rotating) between fan1 and fan2 but not between fan2 and fan1.  If you want this second dead-time then you have to patch the code inside this function node.

I tested with basetime of 60 seconds, fan1 rotating for 30 seconds, fan2 rotating for 20 seconds and dead-time calculated as 60 seconds - 50 seconds = 10 seconds.

The fan status will be transmitted to an MQTT broker.












Flow:

[{"id":"7f2e3791.ab9be8","type":"function","z":"f6b8ce66.d6b76","name":"Cycle next fan","func":"switch (msg.next) {\n    \n    case 1:\n        msg.delay = msg.runtime1;\n        msg.fan1=\"ON\"; \n        msg.fan2=\"OFF\"; \n        msg.next = 2;\n        break;\n    case 2:\n        msg.delay = msg.deadtime;\n        msg.fan1=\"OFF\"; \n        msg.fan2=\"OFF\";\n        msg.next = 3;\n        break;  \n    case 3:\n        msg.delay = msg.runtime2;\n        msg.fan1=\"OFF\"; \n        msg.fan2=\"ON\";\n        msg.next = 1;\n        break; \n        \n    case 4:\n        msg.fan1=\"OFF\"; \n        msg.fan2=\"OFF\";\n        msg.reset = true;\n        break; \n}\n\n\n    msg1={topic:\"cmnd/fan1\", payload:msg.fan1};\n    msg2={topic:\"cmnd/fan2\", payload:msg.fan2};\n\n    // msg1 and msg2 are sent sequencially in outupt 1\n    // msg is sent on output 2 \n    return [[msg1, msg2],msg];","outputs":"2","noerr":0,"x":260,"y":220,"wires":[["92ce9449.6d90c8"],["e8228ca9.a66ce"]]},{"id":"fc7be0f.25b612","type":"inject","z":"f6b8ce66.d6b76","name":"Setup FANs","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":110,"y":80,"wires":[["54fbfa4f.909784"]]},{"id":"92ce9449.6d90c8","type":"mqtt out","z":"f6b8ce66.d6b76","name":"","topic":"","qos":"2","retain":"false","broker":"c7f4b651.bda738","x":530,"y":100,"wires":[]},{"id":"92b1e0bb.85b37","type":"debug","z":"f6b8ce66.d6b76","name":"","active":true,"console":"false","complete":"true","x":330,"y":380,"wires":[]},{"id":"81c129af.1fc868","type":"inject","z":"f6b8ce66.d6b76","name":"Stop FAN","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":440,"wires":[["c56a07d9.6b00c8"]]},{"id":"c56a07d9.6b00c8","type":"function","z":"f6b8ce66.d6b76","name":"Command Stop","func":"global.set(\"StopFan\",true);\nreturn msg;","outputs":1,"noerr":0,"x":385.00000381469727,"y":441,"wires":[[]]},{"id":"e8228ca9.a66ce","type":"function","z":"f6b8ce66.d6b76","name":"Test Stop","func":"//  user commanded stopping fans\nif (global.get(\"StopFan\")) msg.next = 4;\nreturn msg;","outputs":1,"noerr":0,"x":160,"y":340,"wires":[["9f0d2faf.3d214","92b1e0bb.85b37"]]},{"id":"54fbfa4f.909784","type":"function","z":"f6b8ce66.d6b76","name":"Initialize","func":"// Define basetime as 60 seconds (one minute)\nmsg.basetime = 60*1000;\n\n// Define runtime1 (30 seconds)\nmsg.runtime1 = 30*1000;\n\n// Define runtime2 (20 seconds)\nmsg.runtime2 = 20*1000;\n\n// Define deadtime (basetime-runtimes\nmsg.deadtime = msg.basetime-msg.runtime1-msg.runtime2;\n\n// Next fan to be activated is number 1\nmsg.next = 1;\n\n//Stop criteria is false\nglobal.set(\"StopFan\",false);\n\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":80,"wires":[["7f2e3791.ab9be8"]]},{"id":"9f0d2faf.3d214","type":"delay","z":"f6b8ce66.d6b76","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":340,"wires":[["7f2e3791.ab9be8"]]},{"id":"c7f4b651.bda738","type":"mqtt-broker","z":"","broker":"broker.mqttdashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]


Kasper Jepsen

unread,
Jan 25, 2018, 1:25:31 PM1/25/18
to Node-RED
Thank you - i will look into that :)

/Kasper

Kasper Jepsen

unread,
Jan 26, 2018, 10:02:49 AM1/26/18
to Node-RED
This seems to work...

And i can build-rebuild it af needed.

Thank you all for your time

Kasper

rsrob...@gmail.com

unread,
Jun 6, 2018, 5:24:53 AM6/6/18
to Node-RED
Pls advise how to make a flow for cyclic on/off variable timer switch activated by an input on nodered , 20 min on,30 min off for raspberry pi3 GPIO

Mark Setrem

unread,
Jun 6, 2018, 5:27:40 AM6/6/18
to Node-RED

Please post to the new forum: https://discourse.nodered.org you are far more likely to get a response.




Reply all
Reply to author
Forward
0 new messages