Storing a variable

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

[
    {
        "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
    }
]

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

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

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

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

Thanks, will give it a try.

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

:+1: Will do.

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