# Switch Node with time

**URL:** <https://discourse.nodered.org/t/switch-node-with-time/4185>\
**Category:** General\
**Created:** [22 October 2018 11:27 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185 "2018-10-22T11:27:48Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [23 October 2018 14:48 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/22 "2018-10-23T14:48:39Z")

</div>

> [@Gnindit](#):
>
> wednesday (odd week for example) and time between 10am and 1pm.

Another flow but this time using the context to store a "semaphore"

![r-02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/953ae34fbde33ca94533a6df594f021f732f534b.png)

```auto
[{"id":"2da73fb3.09bad","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"88636e86.ec34a","type":"function","z":"2da73fb3.09bad","name":"gate","func":"var moment = global.get('moment'); \nlet myWeek = moment().week();\nlet myDay = moment().day();\nlet myHour = moment().hour();\n\n// Check requirements\n// myWeek%2 - check even or odd week\n// myDay - Tuesday equals 2 (Sunday is 0)\n\nlet valid = (myWeek%2 == 1 && myDay == 2 && myHour >=10 && myHour <=19);\nif (valid) {\n node.status({fill:\"green\",shape:\"ring\",text:\"opened\"})} else {\n node.status({fill:\"red\", shape:\"ring\",text:\"closed\"})\n}\nflow.set(\"gateA\", valid);\nreturn null;","outputs":1,"noerr":0,"x":290,"y":180,"wires":[[]]},{"id":"97da6559.995e08","type":"comment","z":"2da73fb3.09bad","name":"moment.js in a function","info":"","x":200,"y":100,"wires":[]},{"id":"4fc38fc5.f059f","type":"inject","z":"2da73fb3.09bad","name":"","topic":"","payload":"A","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":260,"wires":[["186c4438.ee69dc"]]},{"id":"186c4438.ee69dc","type":"switch","z":"2da73fb3.09bad","name":"","property":"gateA","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":280,"wires":[["aa6f8e2e.0b122"]]},{"id":"ac7b2c4e.0e0ef","type":"inject","z":"2da73fb3.09bad","name":"","topic":"","payload":"B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":300,"wires":[["186c4438.ee69dc"]]},{"id":"aa6f8e2e.0b122","type":"debug","z":"2da73fb3.09bad","name":"","active":true,"console":false,"complete":"true","x":510,"y":280,"wires":[]},{"id":"ef0902f7.74b66","type":"inject","z":"2da73fb3.09bad","name":"Set gateA","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":140,"y":180,"wires":[["88636e86.ec34a"]]}]

```

The switch rule is very simple:

![r-03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e058e2b7a0ee897376ef5807882aadadf903647f.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 October 2018 04:25 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/23 "2018-10-24T04:25:54Z")

</div>

Andrei,

I didn't mean the post in a bad way.

But really, you could just put in the FUNCTION node and do what the SUB-FLOW did anyway.

As I said: I am not sure exactly why there is/was a sub-flow used anyway, other than to give the node a better name. But really that is not true either, as all you would do is just rename the node anyway.

Icon? Shrug. Not sure. But I was just wanting to show there are a few different ways of doing it.

As I am sure you already know. Again: Just saying. Not blaming.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 October 2018 03:28 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/24 "2018-10-28T03:28:21Z")

</div>

> [@Andrei](#):
>
> var moment = global.get('moment');  
> let myWeek = moment().week();  
> let myDay = moment().day();  
> let myHour = moment().hour();

LET..... That takes me back a long time to I think a very early version of BASIC, where you had to prefix variables with the LET command.

Interesting how I have never seen it mentioned here before.

I'm going to go off now and look at what the LET command does because it has sort of spiked my interest.

Hmmmmmm.... Interesting.

To me that is a bit like the Arduino code and how variables are handled there.  
But that may just be me.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 October 2018 06:58 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/25 "2018-10-28T06:58:26Z")

</div>

`let` (not `LET`) is a fairly recent addition to javascript, and earlier versions of node.js did not support it which is why it has only recently started to be used with node red. Basically it is what `var` should have been in the first place but initially javascript was a very simple language and I imagine the developers did not realise the extent of how it would eventually be used so thought that `var` would be sufficient.  
I recommend using `let` unless you have a good reason not to.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [28 October 2018 09:16 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/26 "2018-10-28T09:16:21Z")

</div>

> [@Trying\_to\_learn](#):
>
> var moment = global.get('moment');

Andrei/Colin, why is moment declared as a variable, why not use 'let', the same as myWeek, myDay, etc.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 October 2018 09:54 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/27 "2018-10-28T09:54:26Z")

</div>

> [@Paul-Reed](#):
>
> why is moment declared as a variable, why not use 'let',

No reason that I know of, in that bit of code.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 October 2018 16:59 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/28 "2018-10-28T16:59:32Z")

</div>

> [@Paul-Reed](#):
>
> > var moment = global.get('moment');
> 
> Andrei/Colin, why is moment declared as a variable, why not use 'let', the same as myWeek, myDay, etc.

If you are using newer versions of NodeJS, you should use `const` rather than `let` or `var` wherever you have something that shouldn't change. In fact, you can use `const` with objects and arrays and still change them, you just can't _replace_ a const.

```javascript
const x = {};

x = "Hello"; // will give an error, trying to replace the object.

x.payload = "Hello"; // will be fine, we are only adding a property to the existing object

```

The reason for using `let` is that it is locally scoped.

```javascript
let x = "Hello";

if ( x === "Hello" ) {
    let x = "Boo";
    console.log( x ); // outputs "Boo"
}

console.log( x ); // outputs "Hello"

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [28 October 2018 18:38 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/29 "2018-10-28T18:38:47Z")

</div>

> [@TotallyInformation](#):
>
> If you are using newer versions of NodeJS, you should use `const` rather than `let` or `var` wherever you have something that **shouldn't change**.

We're drifting OT here, but...  
So although `global.get('moment')` will return changing values, `const moment` is always equal to `global.get('moment')` so it is a constant reference to a value.  
So for example, it could be used when parsing an api;

```auto
const summary = (msg.data.minutely.summary);
node.send({payload:summary});

```

Is that correct?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 October 2018 19:27 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/30 "2018-10-28T19:27:36Z")

</div>

> [@Paul-Reed](#):
>
> Is that correct?

Yes, because moment is actually a reference to a NodeJS module (e.g. code), it cannot change and therefore it is a constant. This is best practice because, as you can imagine, if you do accidentally change it, bad things would happen!

In your second example, the `summary` variable does not change within the function node. It only changes outside the node. So, as far as the function node is concerned, it is a constant.

One other small point, I much prefer to always put quotes around my object property names when creating an object so that there can be never any possiblility that it could be misinterpreted as a variable.

```javascript
const summary = 25;
const urm = "payload";
node.send({urm:summary});
// Is the sent object {"urm":25} or is it {"payload":25} ?

```

In case, you can't work it out, the answer is:

```json
{"urm":25}

```

But it is confusing.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [30 October 2018 16:52 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/31 "2018-10-30T16:52:28Z")

</div>

> [@Paul-Reed](#):
>
> Andrei/Colin, why is moment declared as a variable, why not use 'let', the same as myWeek, myDay, etc

Hi Paul,

My sloppiness, 😇

I try to educate myself by using let and const but many times end up using var instead.

I endorse / agree with Colin and Julian´s remarks.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [30 October 2018 16:56 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/32 "2018-10-30T16:56:31Z")

</div>

> [@TotallyInformation](#):
>
> // Is the sent object {"urm":25} or is it {"payload":25} ?

and in case you want {"payload":25} you could use instead:

```auto
const summary = 25;
const urm = "payload";
// Is the sent object {"urm":25} or is it {"payload":25} ?
return {[urm]:summary};

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 November 2018 21:42 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/33 "2018-11-01T21:42:04Z")

</div>

Haha, even more confusing! But thanks, I didn't know that.

I've always done something like:

```auto
const summary = 25;
const urm = "payload";
const myMsg = {};
myMsg[urm] = summary;
return myMsg;

```

---

<div class="post-metadata">

**Author:** ![Gnindit](https://avatars.discourse-cdn.com/v4/letter/g/74df32/32.png) [@Gnindit](https://discourse.nodered.org/u/Gnindit)\
**Post date:** [17 November 2018 07:37 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/34 "2018-11-17T07:37:10Z")

</div>

Hi  
Thanks for all the input.  
I also found this. It depends on an url. Of course it is always nice to have it local like in other examples, but it depends on how important it is for it to work. For more important stuff I would use moment.js.  
Answer will be in Sweidhs since it is an Swedish URL. It returns Date, Weekday, if it is a Swedish holiday, week number, day of week.

```auto
[{"id":"c9e1d06c.dd771","type":"http request","z":"1c6379e4.a98eb6","name":"","method":"use","ret":"txt","url":"","tls":"","x":490,"y":3740,"wires":[["927231d9.a56e3"]]},{"id":"d21cdda2.78356","type":"inject","z":"1c6379e4.a98eb6","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":200,"y":3740,"wires":[["d305bf25.5b131"]]},{"id":"56fb0e1b.7b8e8","type":"debug","z":"1c6379e4.a98eb6","name":"","active":false,"console":"false","complete":"false","x":790,"y":3740,"wires":[]},{"id":"927231d9.a56e3","type":"json","z":"1c6379e4.a98eb6","name":"","pretty":false,"x":630,"y":3740,"wires":[["56fb0e1b.7b8e8","630c8635.bfefe8"]]},{"id":"d305bf25.5b131","type":"function","z":"1c6379e4.a98eb6","name":"","func":"var today = new Date();\nvar dd = today.getDate();\nvar mm = today.getMonth();\nvar yyyy = today.getFullYear();\nmsg.url = \"http://api.dryg.net/dagar/v2.1/\"+yyyy+\"/\"+mm+\"/\"+dd;\nmsg.method = \"GET\";\nreturn msg;\n\n//http://api.dryg.net/dagar/v2.1/2015/01/06","outputs":1,"noerr":0,"x":350,"y":3740,"wires":[["c9e1d06c.dd771"]]}]

```

Just one thing. It needs to be adjusted since this gives the month one month off. Since month are counted from 0 to 11. So basically mm = mm + 1;

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [17 November 2018 08:40 UTC](https://discourse.nodered.org/t/switch-node-with-time/4185/35 "2018-11-17T08:40:18Z")

</div>

please see [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

[Previous page](https://discourse.nodered.org/t/switch-node-with-time/4185.md?page=1)
