# Storing a variable

**URL:** <https://discourse.nodered.org/t/storing-a-variable/77729>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 April 2023 07:55 UTC](https://discourse.nodered.org/t/storing-a-variable/77729 "2023-04-20T07:55:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [20 April 2023 07:55 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/1 "2023-04-20T07:55:52Z")

</div>

Hi,

In a project, I'm using a slider to set a variable which is then sent to mqtt. [this works]  
Said variable is then stored in a file. [this works]  
At boot/reboot the variable is red and sent to the slider. [this works]  
At boot/reboot the slider which received the stored variable is sent to mqtt. [this doesn't work]

What am I missing?

TIA

```auto
[
    {
        "id": "a8132547efec3fa4",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "cab2f8d0d564769e",
        "type": "ui_slider",
        "z": "a8132547efec3fa4",
        "name": "Soil 1 Ref",
        "label": "Soil1 Ref",
        "tooltip": "",
        "group": "87f00ff5ca79e605",
        "order": 1,
        "width": "3",
        "height": "1",
        "passthru": true,
        "outs": "end",
        "topic": "payload",
        "topicType": "msg",
        "min": 0,
        "max": "2",
        "step": ".10",
        "className": "",
        "x": 180,
        "y": 260,
        "wires": [
            [
                "b05b400fea5a4985",
                "4b4f8b7593b54fd2"
            ]
        ]
    },
    {
        "id": "b05b400fea5a4985",
        "type": "function",
        "z": "a8132547efec3fa4",
        "name": "Store var 1",
        "func": "if (msg.payload > - 0)\n{\n msg.topic = \"casa/rp2/soil/s1ref\";\n flow.set(\"cxTest1\", msg.payload, \"soil1ref\");\n return msg;\n}\nreturn null;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 190,
        "y": 320,
        "wires": [
            [
                "9d36c340b8ba1e88"
            ]
        ]
    },
    {
        "id": "e37af7ea8ac25386",
        "type": "inject",
        "z": "a8132547efec3fa4",
        "name": "Set at start",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 170,
        "y": 140,
        "wires": [
            [
                "e9e6c22f3ee2e0cf"
            ]
        ]
    },
    {
        "id": "e9e6c22f3ee2e0cf",
        "type": "function",
        "z": "a8132547efec3fa4",
        "name": "Get ref1",
        "func": "msg.payload = flow.get(\"cxTest1\",\"soil1ref\") || 1;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 180,
        "y": 200,
        "wires": [
            [
                "cab2f8d0d564769e",
                "e02e85b39e89c916"
            ]
        ]
    },
    {
        "id": "e02e85b39e89c916",
        "type": "debug",
        "z": "a8132547efec3fa4",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 420,
        "y": 200,
        "wires": []
    },
    {
        "id": "4b4f8b7593b54fd2",
        "type": "debug",
        "z": "a8132547efec3fa4",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 420,
        "y": 260,
        "wires": []
    },
    {
        "id": "9d36c340b8ba1e88",
        "type": "debug",
        "z": "a8132547efec3fa4",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 420,
        "y": 320,
        "wires": []
    },
    {
        "id": "87f00ff5ca79e605",
        "type": "ui_group",
        "name": "SOIL",
        "tab": "59f076b601e8190d",
        "order": 8,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "59f076b601e8190d",
        "type": "ui_tab",
        "name": "PICO",
        "icon": "network_check",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 April 2023 08:09 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/2 "2023-04-20T08:09:40Z")

</div>

Why don't you make it a Retained variable in MQTT and avoid the need to store it in a file?

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [20 April 2023 09:10 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/3 "2023-04-20T09:10:59Z")

</div>

Well, first I need to learn how to do that but that option wouldn't set the slider to last settings at reboot.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [20 April 2023 09:59 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/4 "2023-04-20T09:59:44Z")

</div>

It seems to work once I link the get function to the set function.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 April 2023 13:40 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/5 "2023-04-20T13:40:32Z")

</div>

> [@ebolisa](#):
>
> Well, first I need to learn how to do that but that option wouldn't set the slider to last settings at reboot.

Yes it would, you just feed the MQTT node into the slider.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [20 April 2023 14:32 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/6 "2023-04-20T14:32:09Z")

</div>

Thanks, will give it a try.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 April 2023 14:42 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/7 "2023-04-20T14:42:36Z")

</div>

Clear the Pass Through option on the slider so you don't get an MQTT loop.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [20 April 2023 15:12 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/8 "2023-04-20T15:12:59Z")

</div>

👍 Will do.

---

<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:** [19 June 2023 15:13 UTC](https://discourse.nodered.org/t/storing-a-variable/77729/9 "2023-06-19T15:13:36Z")

</div>

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