Decrease Dashboard Value

Hello,

I am new to node-red and I am trying to find a way to decrease a gauge value by 1 every 4 seconds. The problem is that have set timestamp as number 10 and when I deploy it becomes 9 and then it keeps generating 9 instead of 9 (wait 4 seconds) 8 (wait.....) until 0. Is there any way to implement this?

Thanks for your time, appreciate your help!


image
image
image
image

you need to store the previous value in the function then recall it when it runs again.

e.g...

var counter = context.get("counter");
if(!counter) {
  counter = msg.payload;
} else {
  counter--;
}
msg.payload = counter;
context.set("counter",counter);
return msg;

Read the node-red docs on functions and context.

Thanks a lot for your help !

Is there anyway to reset it when pressing the timestamp? Thanks for your time !

Connect another inject directly to the function & set the topic to something like reset. Then in your function code, test the msg.topic for equalling "reset" and reset the counter value.

However, be aware, due to the way you set this up (using the trigger node) messages will continue to be sent into the function. So you might also want to send msg.reset = true to the trigger node to stop it.

Tbh there are many ways to achieve this using many different techniques, have a play, give it a go.

I am trying to reset using the following line inside the function:
if (msg.topic=="reset") counter = 0;
but I am not sure if I am using this right because it doesn't reset.
Instead of "counter=0" I tried "context.set("counter",0)" but still nothing. (I connected an inject directly to the function and the topic is reset)
Thanks again for your help ! Appreciate it a lot !

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