# Mute messages overnight

**URL:** <https://discourse.nodered.org/t/mute-messages-overnight/28085>\
**Category:** General\
**Created:** [9 June 2020 22:03 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085 "2020-06-09T22:03:14Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 June 2020 22:03 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/1 "2020-06-09T22:03:14Z")

</div>

My search fu is sadly lacking as I feel this must have been talked about many times before ☹

How do I stop messages passing thru a flow overnight (9pm to 9am in my case)?

I just want to make sure that no messages go to my Alexa speaking node overnight and wake anyone up!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 June 2020 22:04 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/2 "2020-06-09T22:04:48Z")

</div>

If you just wanna do it quick n dirty, add a function node that checks the hour. If \>= 21 or \< 9, return null. Otherwise, return msg.

Do you need the code?

This should get you started...

```auto
var d = new Date();
var hours = d.getHours();

if(hours < 9 || hours >= 21)
   return null;
return msg;

```

Future...  
If you want to make it adjustable, store the upper and lower values in context or file.

You could even make it settable via a telegram or email or mqtt

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 June 2020 22:20 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/3 "2020-06-09T22:20:45Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> var d = new Date();
> var hours = d.getHours();
> 
> if(hours < 9 || hours >= 21)
> return null;
> return msg;
> 
> ```

That'll do nicely for tonight ta 🙂

---

<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:** [10 June 2020 08:08 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/4 "2020-06-10T08:08:32Z")

</div>

Or if you want it configurable use node-red-contrib-queue-gate to hold/release the flow and cron-plus or similar for the scheduling.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [10 June 2020 09:39 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/5 "2020-06-10T09:39:32Z")

</div>

Ta for suggestion

I thought - can I avoid another contrib node so I'm just using the inject node to inject open/close at specific times

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/7077a58464d89e9fa1a77436a7c10555932d1418.png)

```auto
[{"id":"bae8f4c.74ae908","type":"q-gate","z":"c37f6ae0.e9c5a8","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","maxQueueLength":"0","keepNewest":false,"qToggle":false,"persist":true,"x":650,"y":780,"wires":[["5cfc4f69.d9109"]]},{"id":"5cfc4f69.d9109","type":"debug","z":"c37f6ae0.e9c5a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":780,"wires":[]},{"id":"df8ea047.bf2dd","type":"inject","z":"c37f6ae0.e9c5a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 21 * * *","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":400,"y":720,"wires":[["bae8f4c.74ae908"]]},{"id":"f9881920.7e04b8","type":"inject","z":"c37f6ae0.e9c5a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 09 * * *","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":400,"y":860,"wires":[["bae8f4c.74ae908"]]},{"id":"399f9aee.ecb3a6","type":"inject","z":"c37f6ae0.e9c5a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":780,"wires":[["bae8f4c.74ae908"]]}]

```

Also means its dead easy to override it 🙂

---

<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:** [10 June 2020 09:43 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/6 "2020-06-10T09:43:33Z")

</div>

Something like this:

```auto
[{"id":"101620da.dd9def","type":"inject","z":"b9924a74.4d98f8","name":"Your message","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2630,"wires":[["7905085e.ed27d8"]]},{"id":"77c3a40d.cf1914","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"Stop","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2450,"wires":[["88eda37c.fcc62"]]},{"id":"bfdfa379.3510d8","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"Go","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2500,"wires":[["88eda37c.fcc62"]]},{"id":"88eda37c.fcc62","type":"change","z":"b9924a74.4d98f8","name":"","rules":[{"t":"set","p":"control","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":2470,"wires":[[]]},{"id":"7905085e.ed27d8","type":"switch","z":"b9924a74.4d98f8","name":"","property":"control","propertyType":"flow","rules":[{"t":"eq","v":"Go","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":2630,"wires":[["5d54f15f.bd2fa8"]]},{"id":"5d54f15f.bd2fa8","type":"debug","z":"b9924a74.4d98f8","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":550,"y":2630,"wires":[]}]

```

![Screenshot from 2020-06-10 19-42-46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/15302e136680e6f4cebe9694e698d86c43d95ecf.png)

I know mine doesn't queue the messages. But so what? It is either `open` or `closed`.

---

<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:** [10 June 2020 09:53 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/7 "2020-06-10T09:53:30Z")

</div>

That has the disadvantage that it uses context, which is best avoided unless there is a positive advantage.

---

<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:** [10 June 2020 09:55 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/8 "2020-06-10T09:55:01Z")

</div>

Yeah, but I just bashed that out.

The node used in the post probably also uses context, but only in/on that node.

Kinda same horse - different jockey.

Hey, I was just spit balling.

---

<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:** [10 June 2020 09:57 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/9 "2020-06-10T09:57:45Z")

</div>

How about this:

```auto
[{"id":"101620da.dd9def","type":"inject","z":"b9924a74.4d98f8","name":"Your message","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2630,"wires":[["ab588538.5d8fc8"]]},{"id":"77c3a40d.cf1914","type":"inject","z":"b9924a74.4d98f8","name":"Stop","topic":"control","payload":"Stop","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2450,"wires":[["ab588538.5d8fc8"]]},{"id":"bfdfa379.3510d8","type":"inject","z":"b9924a74.4d98f8","name":"Go","topic":"control","payload":"Go","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2500,"wires":[["ab588538.5d8fc8"]]},{"id":"5d54f15f.bd2fa8","type":"debug","z":"b9924a74.4d98f8","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":550,"y":2630,"wires":[]},{"id":"ab588538.5d8fc8","type":"function","z":"b9924a74.4d98f8","name":"","func":"if (msg.topic == \"control\")\n{\n context.set(\"allow\",msg.payload);\n return;\n}\nvar x = context.get(\"allow\")\nif (x == \"Stop\")\n{\n return;\n}\nelse\nreturn msg;","outputs":1,"noerr":0,"x":400,"y":2630,"wires":[["5d54f15f.bd2fa8"]]}]

```

---

<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:** [10 June 2020 09:57 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/10 "2020-06-10T09:57:55Z")

</div>

You might want to do [reset, open] if you don't want queued messages to be actioned when you open it.

---

<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:** [10 June 2020 09:59 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/11 "2020-06-10T09:59:24Z")

</div>

or just use: [gate](https://flows.nodered.org/node/node-red-contrib-simple-gate) which doesn't have queueing.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [10 June 2020 10:02 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/12 "2020-06-10T10:02:39Z")

</div>

Wrote this timerange switch a while back which should pretty much fullfill your requirements:  
[https://flows.nodered.org/flow/fce3ff5ca0df010ab2d4e692f2e50b4e](https://flows.nodered.org/flow/fce3ff5ca0df010ab2d4e692f2e50b4e)  
its a subflow that only uses core nodes and is fully configurable either over the subflow ui or a message.  
Routes messages to output 1 if in the timerange and to 2 if outside.  
Can also be optionally configured for days of week too.  
Just a little bit more flexible than just a function node and also nicely shows the configured range in the status.

---

<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:** [10 June 2020 10:04 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/13 "2020-06-10T10:04:40Z")

</div>

> [@Trying\_to\_learn](#):
>
> The node used in the post probably also uses context, but only in/on that node.  
> Kinda same horse - different jockey.

There is a significant difference. Context in the node is not visible from the outside and does not cause actions in one node to affect another without an obvious flow of messages via the wires. For instance, if you wanted two instances of the flow with different queues and different timings then the first idea would be to copy/paste the set of nodes, wire up the inputs and outputs, and use it. That would fail when using flow context as both flows would be using the same context variable so you would have to go into the flow and find where to change it. That would not be a problem using the queue node.  
Personally I never use flow/global for situations like this.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [10 June 2020 12:22 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/14 "2020-06-10T12:22:15Z")

</div>

Ta - I'd completely forgotten about that method of gating messages - I used to use it before @drmibell made his gate nodes

Coupled with setting repeat times on the inject nodes and it'll do fine 🙂

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [10 June 2020 12:24 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/15 "2020-06-10T12:24:16Z")

</div>

Looks very good 🙂

But overkill for my simple use case

---

<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:** [10 June 2020 12:28 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/16 "2020-06-10T12:28:10Z")

</div>

Glad I can give back for all the questions I ask.

Good luck.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [9 August 2020 12:28 UTC](https://discourse.nodered.org/t/mute-messages-overnight/28085/17 "2020-08-09T12:28:14Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
