# Reset counter issue

**URL:** <https://discourse.nodered.org/t/reset-counter-issue/37040>\
**Category:** General\
**Created:** [5 December 2020 09:44 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040 "2020-12-05T09:44:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown00088](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unknown00088/32/33132_2.png) [@unknown00088](https://discourse.nodered.org/u/unknown00088)\
**Post date:** [5 December 2020 09:44 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/1 "2020-12-05T09:44:53Z")

</div>

Hello, I'm new with node red and programing so please do not destroy me for stupid questions.

In sweat, tears and blo0d I created this:

```auto
var count=context.get('count') || 0;
count +=1;
msg.payload=count;
context.set('count',count);
if (count == 3 ) {
count = 0;
}
return msg;

```

My objective is to recive in the end counter that counts 1,2,3,1,2,3 etc...  
Right now I have counter that count from 1 to infinity .... My issue is that im not able to reset this counter after it reaches to some value.

Sorry for beginners question but i'm not so good with programing.

Regards, Kamil.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 December 2020 09:52 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/2 "2020-12-05T09:52:31Z")

</div>

Try `context.set` after the if statement.

---

<div class="post-metadata">

**Author:** ![unknown00088](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unknown00088/32/33132_2.png) [@unknown00088](https://discourse.nodered.org/u/unknown00088)\
**Post date:** [5 December 2020 10:01 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/3 "2020-12-05T10:01:45Z")

</div>

Thank you very much for this 🙂

This is result which one is working.

```auto
var count=context.get('count') || 0;
count +=1;
msg.payload=count;
context.set('count',count);
if (count == 3 ) {
context.set("count", 0);
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 December 2020 10:21 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/4 "2020-12-05T10:21:59Z")

</div>

Or

```auto
var count=context.get('count') || 0;
count +=1;
msg.payload=count;
if (count == 3 ) {
count = 0;
}
context.set('count',count);
return msg;

```

Or

```auto
var count=context.get('count') || 0;
count +=1;
msg.payload=count;
context.set('count',(count > 2 ? 0 : count));
return msg;

```

---

<div class="post-metadata">

**Author:** ![unknown00088](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unknown00088/32/33132_2.png) [@unknown00088](https://discourse.nodered.org/u/unknown00088)\
**Post date:** [5 December 2020 16:40 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/5 "2020-12-05T16:40:08Z")

</div>

Can I add different cases for each value ?  
Each number will generate different payload and I will inject it to call servcie block type.

This is possible ?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 December 2020 16:43 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/6 "2020-12-05T16:43:34Z")

</div>

You need to explain more.

---

<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:** [5 December 2020 16:46 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/7 "2020-12-05T16:46:24Z")

</div>

> [@unknown00088](#):
>
> Can I add different cases for each value ?  
> Each number will generate different payload and I will inject it to call servcie block type.
> 
> This is possible

Sure.

you can use a switch node

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ced78d55b7ed153ea49efd0e8912ec5fc5de6e6c.png)

* * *

or in JS function...

```auto
var count=context.get('count') || 0;
count +=1;
msg.payload=count;
context.set('count',(count > 2 ? 0 : count));

switch(count) {
  case 1:
  msg.payload = "value is 1";
  break; 
  case 2: 
  msg.payload = "value is 2 - who knew?";
  break;
  default: 
  msg.payload = "value is " + count;
} 

return msg;

```

---

<div class="post-metadata">

**Author:** ![unknown00088](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unknown00088/32/33132_2.png) [@unknown00088](https://discourse.nodered.org/u/unknown00088)\
**Post date:** [5 December 2020 17:03 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/8 "2020-12-05T17:03:10Z")

</div>

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

Yeah I have it like this right now, insted of msg I have call services for single entity to change it states.

I was wondering if one function with longer code is better then solution above ....

If its stuid and it works, it means it not so stupid 🙂

> [@Steve-Mcl](#):
>
> ```auto
> var count=context.get('count') || 0;
> count +=1;
> msg.payload=count;
> context.set('count',(count > 2 ? 0 : count));
> 
> switch(count) {
> case 1:
> msg.payload = "value is 1";
> break; 
> case 2: 
> msg.payload = "value is 2 - who knew?";
> break;
> default: 
> msg.payload = "value is " + count;
> } 
> 
> return msg;
> 
> ```

What I have to type in properties of call service block to corectly read this ?

For example I have Aquara micro switch and I would like to do:  
Signle click - On/Off - I have it  
Double click - select settings from 4 or 5 presets - I have it right now but Im wondering is there any more efficient wat to do it ....  
Hold / release - Dimmer - no code yet....

K.

---

<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:** [3 February 2021 17:03 UTC](https://discourse.nodered.org/t/reset-counter-issue/37040/9 "2021-02-03T17:03:15Z")

</div>

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