Mute messages overnight

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

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!

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...

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

2 Likes

That'll do nicely for tonight ta :slight_smile:

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.

3 Likes

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

[{"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 :slight_smile:

Something like this:

[{"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

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

1 Like

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

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.

How about this:

[{"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"]]}]

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

or just use: gate which doesn't have queueing.

Wrote this timerange switch a while back which should pretty much fullfill your requirements:
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.

1 Like

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.

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 :slight_smile:

Looks very good :slight_smile:

But overkill for my simple use case

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

Good luck.

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