# How to implement a task scheduler?

**URL:** <https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273>\
**Category:** General\
**Created:** [17 April 2019 07:06 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273 "2019-04-17T07:06:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)\
**Post date:** [17 April 2019 07:06 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/1 "2019-04-17T07:06:35Z")

</div>

Hi,

To resume, I am working with a S7 PLC and I am aiming to retrieve periodic data and alarms from the S7, so I declared two S7 connectors :

-one for periodic reading  
-one for alarm reading

![Annotation%202019-04-17%20084958](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5ec972af28f569b4c89eccad76f15413fe318188.png)

I also add a status node to monitor the state of S7 connectors, javascript function are juste here to add an information to the

`msg.payload` as `msg.payload.nodeid = 'whatever'`

Each nodes `status`, `Alarm` and `Periodic` will execute a respective python script (status.py for Status, alarm.py for Alarm, periodic.py for Periodic), but those scripts can't run at the same time.

A scenario example that can't happen :

Periodic sends data and status sends data at same time

So in order to implement a kind of Scheduler, I tried to use the `switch node` to test the equality of `msg.payload.nodeid` so that if the msg.payload.nodeid is equal to the alarm id then it must delay the execution of the status script and periodic script (and if the msg.payload.nodeid is equal to periodic id, delay the two others and so on).

The problem is that using the switch node, I am able to distinguish msg.payload.nodeid but can't schedule tasks.

Do you have any idea?

Kind regards

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [17 April 2019 07:16 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/2 "2019-04-17T07:16:31Z")

</div>

I am a bit lost on the "scheduler" part. What is the use for the switch node ?

Fill in the blanks:  
`periodic --> ? --> ?`

---

<div class="post-metadata">

**Author:** ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)\
**Post date:** [17 April 2019 07:25 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/3 "2019-04-17T07:25:47Z")

</div>

`periodic -> scheduler -> exec script`

The scheduler part is not clear for me at all, that's why I used the switch node because I thought I can use it to schedule script execution.

An example scenario :

Periodic node sends msg, then if the nodeid is equal to a certain value (00 for the periodic) then we can exec the script in condition not another script is running (alarm or status)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [17 April 2019 07:30 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/4 "2019-04-17T07:30:20Z")

</div>

What about the periodic/alarm nodes, what output do these produce ?  
(eg periodic -\> debug node with full msg)

---

<div class="post-metadata">

**Author:** ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)\
**Post date:** [17 April 2019 07:41 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/5 "2019-04-17T07:41:55Z")

</div>

They produce this type of output (here is the periodic's output)

![Annotation%202019-04-17%20093507](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/45ffa2775a3c567514bc66b88668906b1afa1b84.png)

Then I convert the json object to string and my pass it as an argument to python script.

Here is my complete flow which works but only if periodic, alarm and status don't send at same time.

 ![Annotation%202019-04-17%20093937](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6a7fdcf64686b445f0fe08283a364eacc8b77bde.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [17 April 2019 08:01 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/6 "2019-04-17T08:01:09Z")

</div>

The periodic and alarm both produce the same output ? There is no information to distinguish the 2 ?

---

<div class="post-metadata">

**Author:** ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)\
**Post date:** [17 April 2019 08:11 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/7 "2019-04-17T08:11:16Z")

</div>

No doesn't send same output.

Alarm output is :

![Annotation%202019-04-17%20101058](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/811550d81a5fdbcefa9cd9aed219ff739b7a4099.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [17 April 2019 08:15 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/8 "2019-04-17T08:15:37Z")

</div>

Visual

 ![13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/50d6f31bfe207c6fa4313058f8a3a8a788df13e2.jpeg)

Function node:

```auto
t = msg.payload.nodeid

periodic = null //default
alarm = null //default

if(t=="00"){
   periodic = {payload:"do periodic task"}
}
if(t=="01"){
   alarm = {payload:"do alarm task"}
}

return [periodic,alarm]

```

Note that you have to add another output to the function node.  
First will be `periodic` second `alarm`

\*edit this is ofcourse also possible with a switch node.

---

<div class="post-metadata">

**Author:** ![elisascheer](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elisascheer](https://discourse.nodered.org/u/elisascheer)\
**Post date:** [17 April 2019 08:26 UTC](https://discourse.nodered.org/t/how-to-implement-a-task-scheduler/10273/9 "2019-04-17T08:26:46Z")

</div>

Ok thanks I'll try to do it with the switch node but I got it.

Best
