# How can I store a current value

**URL:** https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061
**Category:** General
**Created:** [16 May 2026 10:12 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061 "2026-05-16T10:12:25Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Johannesje](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@Johannesje](https://discourse.nodered.org/u/Johannesje)
#### Post date: [16 May 2026 10:12 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/1 "2026-05-16T10:12:26Z")

</div>

I am reading the output of my kWh meter (via P1).

This concerns the current kWh value, but it is increasing slowly. How can I freeze the current value when I press a button? I want to place this in the context data, as I want to know my monthly energy consumption.

---

<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: [16 May 2026 10:35 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/2 "2026-05-16T10:35:56Z")

</div>

Hi.

I'm not quite understanding the question.

How good are you with programming/using `Node-red`?

Alas the only way I can see you doing it is with a `function` node and a bit of code.

Idea:

You have the _normal_ input to the node and have a second input to cause the value to be captured.

I'm not sure if you hare using a `dashboard` or not. But I'm guessing you are.

But for the sake of keeping it _simple_ I will only talk at the editor level.

The `inject` node would send a `payload` of `HOLD`. Given the other payloads are numbers.

Doing this here/now.

```auto
con

```

No, hang on.  
I'll make the code in the editor and post.

```auto
const message = msg.payload
let hold = context.get("hold") || 0

// If `HOLD`
if (message == "HOLD")
{
    context.set("hold",1)
    return // Nothing more to do be hone here/now
}

if (hold == 1)
{
    // `hold` is set.
    context.set("store",msg.payload)
    context.set("hold",0) // stop next message being storred too.
}

return msg

```

Thoughts.  
Rather than `context` (nodal) make it `flow context`.  
That way you can see the stored value from elsewhere.

Replace: `context.set("store"` with `flow.set("store"` for the value.  
and `context.set("hold"` with `flow.set("hold"`  
and `context.get("hold"` with `flow.get("hold"`

Then in another part of the flow, you can _see_ the value and wipe the `hold` condition.

Example flow:

```auto
[{"id":"509552482ce62bd8","type":"function","z":"6cf51e98651df17b","name":"The magic happens here.","func":"const message = msg.payload\nlet hold = flow.get(\"hold\") || 0\n\n// If `HOLD`\nif (message == \"HOLD\")\n{\n flow.set(\"hold\",1)\n return // Nothing more to do be hone here/now\n}\n\nif (hold == 1)\n{\n // `hold` is set.\n flow.set(\"store\",msg.payload)\n flow.set(\"hold\",0) // stop next message being storred too.\n}\n\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2600,"y":4210,"wires":[["8c11f2103322c9cd"]]},{"id":"4f1ae4a73d6db203","type":"inject","z":"6cf51e98651df17b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2100,"y":4210,"wires":[["4f6b16abf4ef6dc4"]]},{"id":"4f6b16abf4ef6dc4","type":"function","z":"6cf51e98651df17b","name":"random vlue for testing","func":"const randomNumber = Math.floor(Math.random() * 10) + 1\nmsg.payload = randomNumber\n\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2290,"y":4210,"wires":[["509552482ce62bd8"]]},{"id":"c80e384d4cde4c9e","type":"inject","z":"6cf51e98651df17b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"HOLD","payloadType":"str","x":2340,"y":4150,"wires":[["509552482ce62bd8"]]},{"id":"f08a667949b8a286","type":"inject","z":"6cf51e98651df17b","name":"Read stored value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2130,"y":4330,"wires":[["5433167c824dd54f"]]},{"id":"5433167c824dd54f","type":"change","z":"6cf51e98651df17b","name":"get value","rules":[{"t":"set","p":"payload","pt":"msg","to":"store","tot":"flow"},{"t":"set","p":"hold","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":2330,"y":4330,"wires":[["eb35e83ef43ee6e6"]]},{"id":"eb35e83ef43ee6e6","type":"debug","z":"6cf51e98651df17b","name":"Look","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2540,"y":4330,"wires":[]},{"id":"8c11f2103322c9cd","type":"debug","z":"6cf51e98651df17b","name":"to Where ever","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2870,"y":4210,"wires":[]}]

```

---

<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: [16 May 2026 10:47 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/3 "2026-05-16T10:47:47Z")

</div>

Here is a _SIMILAR_ node I made that does something like what you described.

```auto
[{"id":"acbd4dca.cd318","type":"function","z":"78d17e5b.9e6ff","g":"7e49ed3d.35f9cc","name":"Avg/Max/Min readings","func":"let max = context.get(\"max\") || 0\nlet counter = context.get(\"counter\") || 0\nlet min = context.get(\"min\") || null\nlet avg = context.get(\"avg\") || null\nlet total = context.get(\"total\") || 0\n\n\n//node.warn(\"Initial total reading is \" + total);\n//total = total;\n\nlet new_value = parseInt(msg.payload)\n\nlet msg1 = {}\nlet msg2 = {}\n\nif (msg.reset == true)\n{\n // Reset all things\n node.warn(\"RESET\")\n context.set(\"max\",0)\n context.set(\"counter\",0)\n context.set(\"min\",0)\n context.set(\"avg\",0)\n context.set(\"total\",0)\n return\n}\n\nif (counter === 0)\n{\n // First pass\n context.set(\"max\",new_value)\n context.set(\"min\",new_value)\n context.set(\"avg\",new_value)\n min = new_value\n max = new_value\n avg = new_value\n total = new_value\n}\n\n//new_value = msg.payload;\n\nif (new_value > max)\n{\n max = new_value\n context.set(\"max\",new_value)\n}\n\nif (new_value < min)\n{\n\tmin = new_value\n\tcontext.set(\"min\",new_value)\n}\n\ncounter = counter + 1\ncontext.set(\"counter\",counter)\n\ntotal = context.get(\"total\") || 0\n//node.warn(\"context TOTAL is \" + total)\ntotal = total + new_value\n//node.warn(\"TOTAL IS NOW \" + total)\ncontext.set(\"total\",total)\navg = Math.round((total / counter))\n\nmsg.payload = Math.round(avg)\nmsg1.payload = Math.round(max)\nmsg2.payload = Math.round(min)\nreturn [msg,msg1,msg2]","outputs":3,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":3930,"y":1790,"wires":[["b6b37cab.88821","267b42e0.270b16"],["e631bde7.36527","1b588bd.547a2f4"],["e8ec38d8.a3c568","89fd7801.01c23"]],"outputLabels":["AVG","MAX","MIN"]}]

```

It is a `function` node that looks at changes and gives the average, max and min values received.

You will have to play with the message to reset/give the values.

But just offering another option.

---

<div class="post-metadata">

### Author: ![Johannesje](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@Johannesje](https://discourse.nodered.org/u/Johannesje)
#### Post date: [16 May 2026 11:03 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/4 "2026-05-16T11:03:20Z")

</div>

Thanks,

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

I have implemented your solution in my situation and it works fine, thanks a lot.

---

<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: [16 May 2026 11:07 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/5 "2026-05-16T11:07:29Z")

</div>

Mark the one you used as the solution.

Helps others.

---

<div class="post-metadata">

### Author: ![Johannesje](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@Johannesje](https://discourse.nodered.org/u/Johannesje)
#### Post date: [16 May 2026 11:15 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/6 "2026-05-16T11:15:04Z")

</div>

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

Indicated with yellow.  
I have tested with "Vermogen" (power) what I easely can change.  
The solution is simpler than I thought, now I can place that nicely on my dashboard.

Gr. Johannes

---

<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: [16 May 2026 11:17 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/7 "2026-05-16T11:17:04Z")

</div>

_Just saying_

a _problem_ with this is - as you are wanting to see if there are any changes during the day - if you _catch_ a value of..... (say) 5.5 and it changes during the day and goes back to 5.5 and you look at the _captured value_: you won't see a change.

---

<div class="post-metadata">

### Author: ![Johannesje](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@Johannesje](https://discourse.nodered.org/u/Johannesje)
#### Post date: [16 May 2026 11:22 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/8 "2026-05-16T11:22:17Z")

</div>

For me no problem because I use it for the kWh value and I deliver no energy back to the grid so the value only increase

---

<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: [16 May 2026 11:23 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/9 "2026-05-16T11:23:17Z")

</div>

Ok.

Sorry. I wasn't sure and saw that as a possible problem.

All good.

---

<div class="post-metadata">

### Author: ![Johannesje](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@Johannesje](https://discourse.nodered.org/u/Johannesje)
#### Post date: [16 May 2026 11:23 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/10 "2026-05-16T11:23:49Z")

</div>

It was a good point! thanks

---

<div class="post-metadata">

### Author: ![Johannesje](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@Johannesje](https://discourse.nodered.org/u/Johannesje)
#### Post date: [16 May 2026 13:51 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/11 "2026-05-16T13:51:50Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4e671dfd08d134e5582c5b631566e65866531d5.png)  
My result 🙂

---

<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: [30 May 2026 13:52 UTC](https://discourse.nodered.org/t/how-can-i-store-a-current-value/101061/12 "2026-05-30T13:52:28Z")

</div>

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