# Reset a number to zero an starts counting

**URL:** <https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928>\
**Category:** General\
**Created:** [20 October 2019 17:20 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928 "2019-10-20T17:20:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![debouttek](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@debouttek](https://discourse.nodered.org/u/debouttek)\
**Post date:** [20 October 2019 17:20 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/1 "2019-10-20T17:20:29Z")

</div>

I got a msg.payload that gives me a number for example 62123 and it is still counting liters of water that are flowing in my home. (I receive it from a Mqtt)  
Its not possible to reset it in the Mqtt device because I can not access it by software.

Is there a function that sets this number to zero and then starts counting from 1 upwards.

I would like to know my daily water usage and reset the counter every day, week, month, ...

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 October 2019 19:11 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/2 "2019-10-20T19:11:51Z")

</div>

So do you want to check if the value coming in is 62123 and set a counter to zero and then for every value after that subtract 62123 from it and add it into the total? What if the input value reaches 124226 (double 62123)?

What if the device resets and starts at 0 again?

What type of device is sending the data?

---

<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:** [20 October 2019 19:47 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/3 "2019-10-20T19:47:13Z")

</div>

## Suggestion:

Given:

- The device sends a value. eg: 62123.
- It can't be reset.

Put a `function` node in there.

```auto
var used = 0;
let old = context.get('old') || 0;
let new = parseINT(msg.payload);
if (new != old)
{
  used = new - old;
  context.set(old,new);
}

msg.payload = used;
return msg;

```

That is a quick solution.

You **will** need to add code for when the counter does _cycle_ back to 0.  
(It will eventually)

But I think that should do what you want.

---

<div class="post-metadata">

**Author:** ![debouttek](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@debouttek](https://discourse.nodered.org/u/debouttek)\
**Post date:** [20 October 2019 20:31 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/4 "2019-10-20T20:31:57Z")

</div>

The device that's sending the data is a device FLUKSO ([Flukso.net](http://Flukso.net)) that has 6 input ports for Solar power, home consumption and a port for gas meter and water meter. The device (FLUKSO) converts its data into MQTT data. (Its and older device with low support from them at this moment)  
For solar power and consumption I have a working flow and export to Emoncms. Works great.

Yesterday I connected the water sensor to the Flukso device and it sends per 0,5L Mqtt data.  
The data that I receive is at this moment a counter thats given a total of 62500 liter and goes higher the more water I use.  
I cannot reset it to 0.

I can display in dashboard the amount of used water. So at this moment 62500 liter but at midnight I would like to reset is to 0 (input node) and let it count the next 24hours and display only the amount that I use in my household.

I hope I'm clear.

---

<div class="post-metadata">

**Author:** ![debouttek](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@debouttek](https://discourse.nodered.org/u/debouttek)\
**Post date:** [20 October 2019 20:34 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/5 "2019-10-20T20:34:29Z")

</div>

thanks for you're coding but there is something wrong I think.

 ![50](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5da96224d91b7c2338fc072ac0b1b97750d85098.png)

---

<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:** [20 October 2019 20:38 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/6 "2019-10-20T20:38:34Z")

</div>

presumably you are saving the current reading somewhere (either in context or a database). Have an inject at midnight (or once a day) that then reads that variable and stores it in another one ... then wherever you need to do your display ready both and subtract one from the other before sending to the display.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 October 2019 20:50 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/7 "2019-10-20T20:50:28Z")

</div>

Ah, so the probe is reading the counter on the water meter and you have no ability to reset it.

What I would do is store the value (a reading) every 5 minutes in an influx database planning on reducing the data as time passes to every hour then maybe one per day. That way you could graph the usage over a period of time (use Granafa) and in a year look at it by week or month to compare and answer questions like "What happens the the water usage when the kinds come home from college in the summer?" 😂 Ok, that's an easy one and doesn't need to be tracked but you get the idea.

You can subtract the prior reading from the current one to get the amount use since the last reading.

---

<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:** [21 October 2019 09:54 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/8 "2019-10-21T09:54:13Z")

</div>

My sincerest apologies.

I wrote that on the fly.

This is the correct code:

```auto
var used = 0;
let old = context.get('old') || 0;
let newammount = parseInt(msg.payload);
if (newammount != old)
{
  used = newammount - old;
  context.set(old,newammount);
}

msg.payload = used;
return msg;

```

Stupid me forgot that the word `new` is reserved and I used it in the code so it gave you the errors.

That does work.  
(I hope)  
Sorry, I seem to have very big feet and they find my mouth a lot of the time.

But I hope you can see what I am doing.

---

<div class="post-metadata">

**Author:** ![debouttek](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@debouttek](https://discourse.nodered.org/u/debouttek)\
**Post date:** [21 October 2019 18:35 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/9 "2019-10-21T18:35:01Z")

</div>

getting closer..

TypeError: key.startsWith is not a function

Doing something wrong.

---

<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:** [21 October 2019 18:54 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/10 "2019-10-21T18:54:23Z")

</div>

> [@Trying\_to\_learn](#):
>
> context.set(old,newammount);

And again...

```auto
context.set('old',newammount);

```

---

<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:** [21 October 2019 18:57 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/11 "2019-10-21T18:57:00Z")

</div>

> [@debouttek](#):
>
> TypeError: key.startsWith is not a function
> 
> Doing something wrong.

Very likely.

Fix the line I posted above and see if that helps.

---

<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:** [21 October 2019 19:32 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/12 "2019-10-21T19:32:15Z")

</div>

I am not having a good time with code.

Steve-Mcl is right.

Another goof on my part.

---

<div class="post-metadata">

**Author:** ![debouttek](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@debouttek](https://discourse.nodered.org/u/debouttek)\
**Post date:** [22 October 2019 17:26 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/13 "2019-10-22T17:26:47Z")

</div>

The code does not have errors anymore.  
On the other hand the code is behaving strange. It starts from zero but the Mqtt device sends its data only after receiving 0,5l from the water tap and cumulates this after receiving another 0,5L and sends again its data and stops... so it gets a reset every time.  
I have to make it with a database as @dceejay has written or go for the influxdb from @zenofmud  
Thanks for the help already. I'l be back...

---

<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:** [22 October 2019 19:30 UTC](https://discourse.nodered.org/t/reset-a-number-to-zero-an-starts-counting/16928/14 "2019-10-22T19:30:07Z")

</div>

No problems.

It was only an idea anyway.
