# Trouble trying to create a day counter

**URL:** https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120
**Category:** General
**Created:** [5 May 2020 07:52 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120 "2020-05-05T07:52:09Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Jez](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Jez](https://discourse.nodered.org/u/Jez)
#### Post date: [5 May 2020 07:52 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/1 "2020-05-05T07:52:10Z")

</div>

Hi All,

I'm trying to create a counter that counts the days from one position of the switch and a seperate count when the switch is in the other position, when i move the switch i would like the other counter to pause but i'm having no luck. At the moment I'm using a delay and a count node but when i switch the switch it keeps counting when i don't want it to. Is there a way to reset the delay node. I know it would be possible to do this in a function node but i don't know the correct syntax

```auto
[{"id":"5d94a949.60db88","type":"ui_switch","z":"2b6f7cd2.b0d1a4","name":"","label":"Light Cycle","tooltip":"","group":"51e31138.899ea","order":3,"width":4,"height":1,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"0","onvalueType":"num","onicon":"","oncolor":"","offvalue":"1","offvalueType":"num","officon":"","offcolor":"","x":1510,"y":1440,"wires":[["975f1b44.102758","e3e5addd.68dbe","987b42c2.886b7","2df10e3e.285a12","896e1781.a2504","1280becf.6f8129","d96f0ad6.d7c4e8"]]},{"id":"88aaf065.6cf8b","type":"delay","z":"2b6f7cd2.b0d1a4","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"days","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1980,"y":1220,"wires":[["ee2f325a.a76858"]]},{"id":"175bde4e.4fc502","type":"ui_text","z":"2b6f7cd2.b0d1a4","group":"1e7f45a3.99f78a","order":4,"width":3,"height":1,"name":"","label":"Days in Flower","format":"{{msg.payload}}","layout":"row-spread","x":2160,"y":1240,"wires":[]},{"id":"ee2f325a.a76858","type":"counter","z":"2b6f7cd2.b0d1a4","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":2,"x":1980,"y":1260,"wires":[["88aaf065.6cf8b","175bde4e.4fc502","9d149af6.b4e36"],[]]},{"id":"896e1781.a2504","type":"switch","z":"2b6f7cd2.b0d1a4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1830,"y":1260,"wires":[["ee2f325a.a76858"]]},{"id":"4d2a7d60.34366c","type":"counter","z":"2b6f7cd2.b0d1a4","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":2,"x":1980,"y":1560,"wires":[["bc8ce597.52e66","75545630.76c9a8","c6617e9.de4c2"],[]]},{"id":"bc8ce597.52e66","type":"delay","z":"2b6f7cd2.b0d1a4","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"days","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1980,"y":1600,"wires":[["4d2a7d60.34366c"]]},{"id":"1280becf.6f8129","type":"switch","z":"2b6f7cd2.b0d1a4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1630,"y":1560,"wires":[["76846b39.5c8944"]]},{"id":"c6617e9.de4c2","type":"ui_text","z":"2b6f7cd2.b0d1a4","group":"1e7f45a3.99f78a","order":3,"width":3,"height":1,"name":"","label":"Days in Veg","format":"{{msg.payload}}","layout":"row-spread","x":2150,"y":1540,"wires":[]},{"id":"76846b39.5c8944","type":"change","z":"2b6f7cd2.b0d1a4","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1800,"y":1560,"wires":[["4d2a7d60.34366c"]]},{"id":"d96f0ad6.d7c4e8","type":"function","z":"2b6f7cd2.b0d1a4","name":"maybe somthing like this but with a timer in the while loop","func":"msg1 = {};\nmsg2 = {};\n\nwhile (msg.payload === 0){\n msg1.payload = msg.payoad +1\n}\nwhile (msg.payload == 1){\n msg2.payload = msg.payload + 1\n}\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"x":1850,"y":1100,"wires":[[],[]]},{"id":"51e31138.899ea","type":"ui_group","z":"","name":"Left","tab":"9caadd41.281e9","order":1,"disp":false,"width":8,"collapse":false},{"id":"1e7f45a3.99f78a","type":"ui_group","z":"","name":"Right","tab":"7dcf1cb5.14a274","order":4,"disp":false,"width":"6","collapse":false},{"id":"9caadd41.281e9","type":"ui_tab","z":"","name":"Ipad","icon":"dashboard","disabled":false,"hidden":false},{"id":"7dcf1cb5.14a274","type":"ui_tab","z":"","name":"Greenhouse","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

```

---

<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: [5 May 2020 08:03 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/2 "2020-05-05T08:03:40Z")

</div>

Hi Jez.

I loaded your flow, but am not getting what you want to do, sorry.

Just one thing, you could combine both those two `switch` nodes.

`if msg.payload == 0` --\> 0  
`if msg.payload == 1` --\> 1

But that's academic at this stage.

The `count` node: Won't it (they) need to be reset at some stage?

I also think there is a _missing_ signal to indicate passage of time.

> [@Jez](#):
>
> that counts the days

In the flow you posted, I can't see where a _new day signal_ is to indicate a new day.

### Edit

Before using the flow, press the `inject` node for either **switch position 1** or **switch position 2**  
It won't work otherwise.

You have a `change` node on the second (lower) branch. I don't think that is needed.

> [@Jez](#):
>
> At the moment I'm using a delay and a count node but when i switch the switch it keeps counting when i don't want it to.

That is because you aren't resetting the node.  
The way you originally do this is problematic - as I think I said.  
You will need to keep stopping the counting on the node which is disabled.  
My (newer) way that is not part is completely redone another way which I think is better.

> reset  
> If the received message has this property set to any value, all outstanding messages held by the node are cleared without being sent.

Ok, sorry. The `delay` nodes. Yeah. Ok, but problematic I think.

From what I see, that flow won't really work, though you may already know that: as that's why you are asking.

Have a look at this for a starting point.  
I don't have any `dashboard` stuff. The `switch` is the two lower `inject` nodes.  
The two `text` nodes are replaced with the two `debug` nodes.

When the counter is _paused_, is that with the _switch_ in position 1 or 2?

I don't want to go too far on this because I have (nearly) rebuilt your flow and you may not like it. (Been there done that)  
But it shows you another way of doing it.

To **test** the flow, just press the `inject` button that is the `Daily pulse` node.  
That will _simulate_ a new day.  
(You will have to change the time if/when you use it, as I just left it at mid day rather than mid-night)

```auto
[{"id":"267edf11.a1736","type":"inject","z":"d4f6e01b.2b8f68","name":"Daily pulse","topic":"","payload":"dailyPulse","payloadType":"str","repeat":"","crontab":"00 12 * * *","once":false,"onceDelay":0.1,"x":220,"y":670,"wires":[["75887ecd.294b48"]]},{"id":"d1348ca9.d8e85","type":"inject","z":"d4f6e01b.2b8f68","name":"Switch - position 1","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":770,"wires":[["92739bf3.43591"]]},{"id":"f2626b30.abd1c","type":"inject","z":"d4f6e01b.2b8f68","name":"Switch - position 2","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":810,"wires":[["92739bf3.43591"]]},{"id":"92739bf3.43591","type":"change","z":"d4f6e01b.2b8f68","name":"","rules":[{"t":"set","p":"SWITCH","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":790,"wires":[[]]},{"id":"75887ecd.294b48","type":"switch","z":"d4f6e01b.2b8f68","name":"Switch","property":"SWITCH","propertyType":"flow","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":670,"wires":[["d93bc6d3.372568"],["5ed22dfc.8adbb4"]]},{"id":"d93bc6d3.372568","type":"counter","z":"d4f6e01b.2b8f68","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":2,"x":560,"y":630,"wires":[["349f73bc.c76f3c"],[]]},{"id":"349f73bc.c76f3c","type":"debug","z":"d4f6e01b.2b8f68","name":"Days in flower","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":760,"y":630,"wires":[]},{"id":"d5223eab.900518","type":"debug","z":"d4f6e01b.2b8f68","name":"Days in veg","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":750,"y":700,"wires":[]},{"id":"5ed22dfc.8adbb4","type":"counter","z":"d4f6e01b.2b8f68","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":2,"x":560,"y":700,"wires":[["d5223eab.900518"],[]]}]

```

---

<div class="post-metadata">

### Author: ![Jez](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Jez](https://discourse.nodered.org/u/Jez)
#### Post date: [5 May 2020 08:40 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/3 "2020-05-05T08:40:35Z")

</div>

I would like to count how meany days the switch is in position 1, then when i switch to position 2, I would like the counter on position 1 to pause as well as count how meany days the switch has been in position 2 and vice versa. If that helps with what i am trying to achieve.

---

<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: [5 May 2020 08:45 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/4 "2020-05-05T08:45:55Z")

</div>

Ok. So to recap:

The _switch_ is in _position1_.  
It counts the days.

You move the _switch_ to _position2_.  
It then counts how many days it is in position 2.

When you move it back to _position1_, it **resumes** counting.

Question/s:  
When you switch to 2, is the previous count kept or wiped?  
(Similar to 1.)

Because if that is correct, the flow I posed does that now.

You can test this easily.  
Select **switch position 1** , click the **daily pulse** node a few times.  
Select **switch position 2** , click the **daily pulse** node a few times and see that count go up.

Go back to **position 1** and see what happens.

---

<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: [5 May 2020 08:56 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/5 "2020-05-05T08:56:00Z")

</div>

If you prefer doing it more like how you first did it, this may be the way you want to do it:

```auto
[{"id":"e04979b5.c7923","type":"inject","z":"d4f6e01b.2b8f68","name":"Daily pulse","topic":"","payload":"dailyPulse","payloadType":"str","repeat":"","crontab":"00 12 * * *","once":false,"onceDelay":0.1,"x":230,"y":940,"wires":[["75ab0cc.7741a74"]]},{"id":"75ab0cc.7741a74","type":"function","z":"d4f6e01b.2b8f68","name":"SWITCH","func":"if (msg.topic == \"SET\")\n{\n context.set(\"SWITCH\",msg.payload);\n return;\n}\n\nmsg.payload = \"1\";\n\nlet x = context.get(\"SWITCH\") || 0;\nif (x === 0)\n{\n return [msg,null];\n} else\n{\n return [null,msg];\n}\n","outputs":2,"noerr":0,"x":380,"y":940,"wires":[["c3e0921.0fee4f"],["3ce11a10.519cc6"]]},{"id":"c3e0921.0fee4f","type":"counter","z":"d4f6e01b.2b8f68","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":2,"x":530,"y":900,"wires":[["20043bc9.3affd4"],[]]},{"id":"3ce11a10.519cc6","type":"counter","z":"d4f6e01b.2b8f68","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":2,"x":530,"y":970,"wires":[["9a7c64bd.dad538"],[]]},{"id":"61f4ec4f.b85fa4","type":"inject","z":"d4f6e01b.2b8f68","name":"Switch - position 1","topic":"SET","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":990,"wires":[["75ab0cc.7741a74"]]},{"id":"a96ce7bc.b89398","type":"inject","z":"d4f6e01b.2b8f68","name":"Switch - position 2","topic":"SET","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":1030,"wires":[["75ab0cc.7741a74"]]},{"id":"20043bc9.3affd4","type":"debug","z":"d4f6e01b.2b8f68","name":"Days in flower","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":710,"y":900,"wires":[]},{"id":"9a7c64bd.dad538","type":"debug","z":"d4f6e01b.2b8f68","name":"Days in veg","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":700,"y":970,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Jez](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@Jez](https://discourse.nodered.org/u/Jez)
#### Post date: [5 May 2020 10:17 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/6 "2020-05-05T10:17:32Z")

</div>

Thanks a lot mate both ways work exactly how i need it to. I have another question but i think it is for another topic, every time i deploy the flow all data to my dash board is lost and reset eg, the count from the switch is reset whenever i deploy the flow, would you know how to get around this or should i start another topic on the forum.

Cheers,

---

<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: [5 May 2020 11:52 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/7 "2020-05-05T11:52:52Z")

</div>

Yes, that is a bit off topic.

But you need to study `context` and how it works.

That should help you for that problem.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [4 July 2020 11:52 UTC](https://discourse.nodered.org/t/trouble-trying-to-create-a-day-counter/26120/8 "2020-07-04T11:52:53Z")

</div>

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