# On off timer switch mqtt node

**URL:** https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616
**Category:** General
**Created:** [12 October 2019 01:27 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616 "2019-10-12T01:27:19Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![mwawrzyniec](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mwawrzyniec](https://discourse.nodered.org/u/mwawrzyniec)
#### Post date: [12 October 2019 01:27 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/1 "2019-10-12T01:27:19Z")

</div>

I'm looking for a way to switch an mqtt node on for 5 minutes and off for 25. Anyone have any ideas?

---

<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: [12 October 2019 04:36 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/2 "2019-10-12T04:36:44Z")

</div>

You may need to explain what you mean.

Do you mean the **node** or the **server/broker**?

Two very different beasts.

---

<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: [12 October 2019 06:22 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/3 "2019-10-12T06:22:04Z")

</div>

MQTT nodes cannot be switched on and off. If you mean you want to send one value to the node then later send another you can use the Trigger node to send the On message then 5 minutes later send the Off message. If you trigger that from an inject node set to fire every 25 minutes then I think that should do the job.

---

<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: [12 October 2019 08:08 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/4 "2019-10-12T08:08:04Z")

</div>

Or if you want to stop sending / receiving in your flow for that time. You can use a flow as Colin suggests to save a context value,

Then in your MQTT flow have a switch that is open or closed depending on the state of the context value.

---

<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: [12 October 2019 08:25 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/5 "2019-10-12T08:25:35Z")

</div>

> [@ukmoose](#):
>
> Or if you want to stop sending / receiving in your flow for that time.

I had not thought of that interpretation of the question, but if that is indeed the requirement then a trigger node as I suggested but use it to drive a node-red-contrib-simple-gate to let mqtt messages through or not.

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [12 October 2019 21:51 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/6 "2019-10-12T21:51:55Z")

</div>

Might I recommend my node:

> **[node-red-contrib-mytimeout](https://flows.nodered.org/node/node-red-contrib-mytimeout)**
>
> A timer that supports setting the run time by passing JSON to the input

You use it with mqtt topics.

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [13 October 2019 00:10 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/7 "2019-10-13T00:10:56Z")

</div>

You might also look at Pete Scargill's BigTimer:

> **[node-red-contrib-bigtimer](https://flows.nodered.org/node/node-red-contrib-bigtimer)**
>
> The ultimate Node-Red Timer with dusk, dawn (and variations inc. sunrise, sunset, moonrise and moonset), months, days, manual override, schedule pause, random or fixed offsets, special days and much more. Using STOP now sets the output off

---

<div class="post-metadata">

### Author: ![mwawrzyniec](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mwawrzyniec](https://discourse.nodered.org/u/mwawrzyniec)
#### Post date: [14 October 2019 04:42 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/8 "2019-10-14T04:42:39Z")

</div>

I need a continuous loop that sends a payload of 1, waits 5 minutes, sends a payload of 0, waits 25 minutes and starts the loop again.

---

<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: [14 October 2019 05:29 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/9 "2019-10-14T05:29:48Z")

</div>

I think you need to re-think what you are doing and why/how you want to _switch the mqtt node on/off_

---

<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: [14 October 2019 06:22 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/10 "2019-10-14T06:22:12Z")

</div>

I think if you look at my first post it will do exactly what is needed.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [14 October 2019 07:54 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/11 "2019-10-14T07:54:07Z")

</div>

@Colin - being picky - the inject should fire every 30 minutes... and the trigger set for 5 mins.

---

<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: [14 October 2019 08:09 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/12 "2019-10-14T08:09:19Z")

</div>

> [@mwawrzyniec](#):
>
> I need a continuous loop that sends a payload of 1, waits 5 minutes, sends a payload of 0, waits 25 minutes and starts the loop again

So you need...

| time | signal |
| --- | --- |
| 00:00 | 1 |
| 00:05 | 0 |
| 00:30 | 1 |
| 00:35 | 0 |
| 01:00 | 1 |
| 01:05 | 0 |
| and so on? | |

Breaking that down, you need 2 events...

| Start time | Repeat | signal |
| --- | --- | --- |
| 00:00 | 30 | 1 |
| 00:05 | 30 | 0 |

How about this...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/354487f57308d20ebc906263ca4a9c9f825f68cd.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/be059bf626cbf7ba13c3974adbc68033091053d0.png)

**The node...**  
[node-red-contrib-cron-plus - A flexible scheduler node for Node-RED](https://flows.nodered.org/node/node-red-contrib-cron-plus)

**The flow...**

```auto
[{"id":"bd87bd54.f4266","type":"cronplus","z":"5eb10c8e.48d604","name":"","outputField":"payload","timeZone":"","options":[{"topic":"5","payload":"1","type":"num","expression":"0/30 * * * * "},{"topic":"30","payload":"0","type":"num","expression":"5/30 * * * * "}],"x":560,"y":280,"wires":[["c9c8cb3.a050d38"]]},{"id":"c9c8cb3.a050d38","type":"debug","z":"5eb10c8e.48d604","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":280,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![mwawrzyniec](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mwawrzyniec](https://discourse.nodered.org/u/mwawrzyniec)
#### Post date: [14 October 2019 14:37 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/13 "2019-10-14T14:37:49Z")

</div>

Thank you

---

<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: [14 October 2019 19:57 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/14 "2019-10-14T19:57:43Z")

</div>

> [@dceejay](#):
>
> being picky - the inject should fire every 30 minutes... and the trigger set for 5 mins.

Absolutely right of course.  
Also I like the cron-plus solution more than my suggestion.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [14 October 2019 20:03 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/15 "2019-10-14T20:03:14Z")

</div>

The inject plus trigger is just two of the core nodes.

---

<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: [14 October 2019 20:38 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/16 "2019-10-14T20:38:57Z")

</div>

@dceejay

I often advocate using core nodes and even in this instance I'm torn between my own offering and core nodes.

However in this instance, I feel the use of a contrib node fit the problem very nicely and that it permitted me to reply in a manner that helps people understand how we break a problem down to smaller, more manageable parts. more so than the contrib node itself.

👼

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [14 October 2019 21:25 UTC](https://discourse.nodered.org/t/on-off-timer-switch-mqtt-node/16616/17 "2019-10-14T21:25:37Z")

</div>

Indeed it's a great fit for this solution, and indeed does help to explain the complexities of cron, which are not for the faint hearted.
