Reset a number to zero an starts counting

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

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?

Suggestion:

Given:

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

Put a function node in there.

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.

The device that's sending the data is a device FLUKSO (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.

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

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.

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?" :joy: 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.

My sincerest apologies.

I wrote that on the fly.

This is the correct code:

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.

getting closer..

TypeError: key.startsWith is not a function

Doing something wrong.

And again...

context.set('old',newammount);
1 Like

Very likely.

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

I am not having a good time with code.

Steve-Mcl is right.

Another goof on my part.

1 Like

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

No problems.

It was only an idea anyway.