# \[ANNOUNCE\] @waldbaer/node-red-persistent-values

**URL:** <https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677>\
**Category:** Share Your Nodes\
**Created:** [3 February 2023 21:04 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677 "2023-02-03T21:04:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![waldbaer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/waldbaer/32/74212_2.png) [@waldbaer](https://discourse.nodered.org/u/waldbaer)\
**Post date:** [3 February 2023 21:04 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/1 "2023-02-03T21:04:29Z")

</div>

Hi,  
I would like to announce my new released node for user-friendly usage of the Node-RED context stores.

### Motivation

I am using Node-RED to control my SmartHome.  
For many smart actions several states / config options are typically necessary (Anyone present, Everyone sleeping, Heating Period, Open Windows, Open Doors, ...) fed by sensor data or manual user interaction.

To store all these states I used in the past two years a custom subflow/function with subflow configuration options where I could define all my used states/config options.  
Under the hood the excellent core [context stores](https://nodered.org/docs/user-guide/context) is used to store all the states either on disk or memory to let them survive system reboots etc.

One major issue with the context store APIs are that a user must always remember the correct name of the variables. Futhermode if different scopes (node, flow, global) or stores (file, memory, default) were used an unintended mix up of these parameters can happen very fast.

### Solution

To simplify the usage of all these states / config options etc the idea came up to implement a dedicated node where all necessary states can be defined via an shared configuration node.  
In the configuration node the different states as well as their datatype, default value, scope and context store can be configured at a single location.  
In the concrete nodes used to read or write to the state/config option only the shared configuration node as well as the state/config option to be accessed must be selected.

This is the new born node:

> **[@waldbaer/node-red-persistent-values](https://flows.nodered.org/node/@waldbaer/node-red-persistent-values)**
>
> A user-friendly abstraction of the Node-RED context stores.

### Future

There are several ideas for future improvements:

- Get previous state value in the output message
- More config validation: Unique config and state names
- More datatypes (Map, Object, Array, BigInt, ? ...)
- Output current state after NodeRED startup

I hope that the node is helpful for many more NodeRED enthusiasts.  
Looking forward to constructive feedback!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 February 2023 22:04 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/2 "2023-02-03T22:04:38Z")

</div>

Hi @waldbaer

Nice work!

Can you see the variables created, also being accessed via the `global` context, given that custom nodes can write to that context?

---

<div class="post-metadata">

**Author:** ![waldbaer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/waldbaer/32/74212_2.png) [@waldbaer](https://discourse.nodered.org/u/waldbaer)\
**Post date:** [3 February 2023 22:14 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/3 "2023-02-03T22:14:52Z")

</div>

@marcus-j-davies  
The node is "just" an abstraction of the NodeRED core [context API](https://nodered.org/docs/user-guide/context).  
So you can see all the states as before in the UI as well access them in any custom function.

As I have also the usecase that I need several states at once to make a decision (e.g. Windows open && Lights on) I added the "Collect Values" option. Multiple nodes can then be "chained" and at the end in a custom function all the states are evaluated. The collected states are added to an configurable msg property as Object. E.g. `msg.states = { RoomStates_OpenWindows: 5, RoomStates_LightsOn: true}`.  
Please also have a look at the example flow "Collect Multiple Values".

---

<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:** [3 February 2023 22:36 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/4 "2023-02-03T22:36:08Z")

</div>

What is the advantage of this over using filesystem context?  
(Rather than the default where context is stored in memory)

---

<div class="post-metadata">

**Author:** ![waldbaer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/waldbaer/32/74212_2.png) [@waldbaer](https://discourse.nodered.org/u/waldbaer)\
**Post date:** [3 February 2023 22:47 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/5 "2023-02-03T22:47:44Z")

</div>

@Trying_to_learn The user-friendly usage in multiple flows etc without needing to remember the exact variable name, context type, store type.

Any mixup of the string parameters results in an invalid behavior.

Flow1:  
`msg.payload = global.get('mystate1', 'memory');`  
Flow2:  
`msg.payload = global.get('mystate1', 'file');. // Inconsistent states returned!`

Also every custom node must take care of an not yet set context variable.

The same applies if context variables are accessed via the change node.

There are definitely usecases where the direct usage of the context APIs is the best and easiest way (e.g. variables on scope 'node'). But for huge NodeRED setups where dozens of variables in the context need to be accessed from multiple flow locations it might end up fast in bugs due to copy-pasting code.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 February 2023 22:48 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/6 "2023-02-03T22:48:04Z")

</div>

@waldbaer can correct me...

But I see it as a UI / definable way to manage context based variables

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 February 2023 22:53 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/7 "2023-02-03T22:53:00Z")

</div>

@waldbaer

Can this enumerate variables not defined Within your node?

---

<div class="post-metadata">

**Author:** ![waldbaer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/waldbaer/32/74212_2.png) [@waldbaer](https://discourse.nodered.org/u/waldbaer)\
**Post date:** [3 February 2023 22:56 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/8 "2023-02-03T22:56:36Z")

</div>

@marcus-j-davies Can you please give me some more detail on your question? I don't get it.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 February 2023 22:58 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/9 "2023-02-03T22:58:08Z")

</div>

If a user were to do (in a function node)

`global.set('SomeKey','SomeValue')`

Would it be listed in the node?

---

<div class="post-metadata">

**Author:** ![waldbaer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/waldbaer/32/74212_2.png) [@waldbaer](https://discourse.nodered.org/u/waldbaer)\
**Post date:** [3 February 2023 23:03 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/10 "2023-02-03T23:03:31Z")

</div>

@marcus-j-davies Ah, nice idea! 🤩  
But this is not (yet) supported.

Might be an option to add this an feature. But this might then require an configurable naming scheme.

Today the values are stored with the scheme `<Config Name>_<Value Name>`.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 February 2023 23:05 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/11 "2023-02-03T23:05:09Z")

</div>

Understood 👍

Again Nice work!

---

<div class="post-metadata">

**Author:** ![waldbaer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/waldbaer/32/74212_2.png) [@waldbaer](https://discourse.nodered.org/u/waldbaer)\
**Post date:** [3 February 2023 23:12 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/12 "2023-02-03T23:12:08Z")

</div>

@marcus-j-davies

In a first shot I didn't want to implement a full abstraction of all variables in the context stores to avoid too invasive effects on existing setups.

At the moment any deletion or renaming of configured values will not lead to a renaming of the context variables.

But yes, would be a future extension to let the node actually do a 'full management' of all context variables.

Thanks for the input!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 February 2023 23:18 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/13 "2023-02-03T23:18:37Z")

</div>

There are members of the community on higher 'payrolls' that can provide more robust feedback 😅

And if desirable, would provide some solid input. 👍

---

<div class="post-metadata">

**Author:** ![waldbaer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/waldbaer/32/74212_2.png) [@waldbaer](https://discourse.nodered.org/u/waldbaer)\
**Post date:** [6 September 2023 15:30 UTC](https://discourse.nodered.org/t/announce-waldbaer-node-red-persistent-values/74677/14 "2023-09-06T15:30:01Z")

</div>

I just want to give a short update here. Since the first released version several small features have been added in the meantime.

With the latest version **1.6.0** the following new features are now included:

- full dynamic control of command and selected value with configurable `msg` properties
- Output of previous persisted value
- Datatype JSON
- 'Reset' command to restore configured default value
- Description field and 'meta data' output of persisted value
- Several small fixes and improvements

Have fun with it!
