Store a state of a value "flow.set"

Hi

maybe I don't have understand right the function flow.set... ; and flow.get... ;

the whole project works as well, but in special situations I could detect irregularities

I find out, that I noticed that states of variables are not saved
when activating the first "Valve" the context data are listed correctly, when activating a second "Valve" the context data are also correct - but the first activated "Valve" is "forgotten"
(... is undefined after "flow.set..")

thank you in advance, kind regards
Max

  1. Follow.set stores the value in context storage.
  2. Flow.get retrieves data from context storage
  3. each time a function node runs it starts fresh and nows nothing about what happened the last time it ran.

Try adding a
node.warn("Valve30pen="+Valve30pen)
right after the
var Valve30pen - msg. Valve30pen;
statement and see what you get.

you mean: var Valve3Open = msg.Valve3Open;

in the function I get a warn-flag "Valve3Open is already defined"
otherwise I couldn't find any change

var Valve30pen - msg. Valve30pen;
was a typo (I cut the tip of my index finger last week and it is effecting my typing :roll_eyes:)
Did you add the node.warn("Valve30pen="+Valve30pen) as the second line of your 'function' node? The was all I wanted you to do.

thank you "zenofmud" for your patience

yes, I have now add this and in the Debug at first, I get the right result "Valve 1 is 1"

after when I start Valve 2 (is not in the same line), the I get from the same debug-node the answer " Valve 1 is undefined" >>> that makes me suspicious!!!

(Valve 3 or Valve 1 is equal)

Ask yourself "Which line in my function is setting the value of that variable?"

1 Like

hi Colin

the Value comes from the Flow Valve(n) after a https request "Valve(n) is open" - resp. "Valve(n) is close" (as return massage) - connected via Link to "this" function, where the condition should be stored by flow.set (1 is open, 0 is close), see screenshot

The line that sets the value of the variable is
var Valve1Open = msg.Valve1Open
Did the latest message in have a value in msg.Valve1Open?

The flow.set command in the first message sets the flow variable. If you want to fetch the previous value of the flow variable when the second message comes in then you have to use flow.get.

Can you please export your function node and paste it to a reply (not a screen shot)

the 5 function of this group, which determine whether the supply-pump must run

    {
        "id": "4d9ed806.e42d08",
        "type": "link in",
        "z": "efd2f080.df5cf8",
        "name": "Supply_ON",
        "links": [
            "449c44f9.470ec4",
            "72b230cd.0ce04",
            "7e061671.e95e5",
            "ca652598.ea8308",
            "d998e505.7c1e1",
            "e6d79acc.a1d8a8",
            "f781820.c59fc8",
            "2ce6a98.580b656"
        ],
        "x": 179.00003051757812,
        "y": 460.6666507720947,
        "wires": [
            [
                "fd7d64f9.3ce9f8",
                "b2367205.f8f6f",
                "53d5220c.eda60c",
                "b548c003.07b5c"
            ]
        ]
    },
    {
        "id": "fd7d64f9.3ce9f8",
        "type": "function",
        "z": "efd2f080.df5cf8",
        "name": "Valve 6 + 7",
        "func": "var Valve7Open = msg.Valve7Open;\nvar Valve6Open = msg.Valve6Open;\nvar Supply4 = msg.Supply4;\n\nflow.set('Valve7Open', Valve7Open);\n\nflow.set('Valve6Open', Valve6Open);\n\n//node.warn(\"Valve 7 is \" + Valve7Open);\n//node.warn(\"Valve 6 is \" + Valve6Open);\n\nif ( Valve7Open === 1 || Valve6Open === 1 ) {\n    Supply4 = true;\n    }\n    else {\n        if ( Valve7Open === 0 && Valve6Open === 0 ) {\n        Supply4 = false;\n    }\n    }\n \nflow.set('Supply4', Supply4);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 314.0000305175781,
        "y": 400.6666507720947,
        "wires": [
            [
                "e7c3c751.2a2878"
            ]
        ]
    },
    {
        "id": "b2367205.f8f6f",
        "type": "function",
        "z": "efd2f080.df5cf8",
        "name": "Valve 5 + 2",
        "func": "var Valve5Open = msg.Valve5Open;\nvar Valve2Open = msg.Valve2Open;\nvar Supply3 = msg.Supply3;\n\nflow.set('Valve5Open', Valve5Open);\n\nflow.set('Valve2Open', Valve2Open);\n\nnode.warn(\"Valve 5 is \" + Valve5Open);\nnode.warn(\"Valve 2 is \" + Valve2Open);\n\nif ( Valve5Open === 1 || Valve2Open === 1 ) {\n    Supply3 = true\n    }\n    else {\n        if ( Valve5Open === 0 && Valve2Open === 0 ) {\n        Supply3 = false\n    }\n    }\nflow.set('Supply3', Supply3);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 314.0000305175781,
        "y": 440.6666507720947,
        "wires": [
            [
                "e7c3c751.2a2878"
            ]
        ]
    },
    {
        "id": "e7c3c751.2a2878",
        "type": "function",
        "z": "efd2f080.df5cf8",
        "name": "Supply?",
        "func": "var Supply1 = flow.get('Supply1');\nvar Supply2 = flow.get('Supply2');\nvar Supply3 = flow.get('Supply3');\nvar Supply4 = flow.get('Supply4');\n\nif ( Supply1 === true || \n        Supply2 === true || \n            Supply3 === true ||\n                Supply4 === true ) {\n    SupplyON = true\n                }\n    else {\n        SupplyON = false\n    }\n\nflow.set('SupplyON', SupplyON);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 504.0000305175781,
        "y": 460.6666507720947,
        "wires": [
            [
                "b5b00f0b.bee56"
            ]
        ]
    },
    {
        "id": "53d5220c.eda60c",
        "type": "function",
        "z": "efd2f080.df5cf8",
        "name": "Valve 1 + 3",
        "func": "var Valve3Open = msg.Valve3Open;\nvar Valve1Open = msg.Valve1Open;\nnode.warn(\"Valve 1 is \" + Valve1Open);\n\nvar Supply1 = msg.Supply1;\n\nflow.set('Valve3Open', Valve3Open);\n\nflow.set('Valve1Open', Valve1Open);\n\n//node.warn(\"Valve 3 is \" + Valve3Open);\n//node.warn(\"Valve 1 is \" + Valve1Open);\n\n\nif ( Valve3Open === 1 || Valve1Open === 1 ) {\n    Supply1 = true\n    }\n    else {\n        if ( Valve3Open === 0 && Valve1Open === 0 ) {\n        Supply1 = false\n    }\n    }\n\nflow.set('Supply1', Supply1);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 314.0000305175781,
        "y": 520.6666507720947,
        "wires": [
            [
                "e7c3c751.2a2878"
            ]
        ]
    },
    {
        "id": "b548c003.07b5c",
        "type": "function",
        "z": "efd2f080.df5cf8",
        "name": "Valve 4",
        "func": "var Valve4Open = msg.Valve4Open;\nvar Supply2 = msg.supply2;\n\nflow.set('Valve4Open', Valve4Open);\n\nif ( Valve4Open === 1 ) {\n    Supply2 = true\n    }\n    else {\n        if ( Valve4Open === 0 ) {\n        Supply2 = false\n    }\n    }\n\nflow.set('Supply2', Supply2);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 304.0000305175781,
        "y": 480.6666507720947,
        "wires": [
            [
                "e7c3c751.2a2878"
            ]
        ]
    }
]```


there are 7 Valves how get a "open" or a "close"-command to a IOT (3); the IOT brings a return message "Valve(n) is open/close" (7 Link's "Supply_ON")
this 7 Valves is packed in 4 groups (Valve 1 + 3; Valve 4; Valve 5 + 2; Valve 6 + 7)
the only task is, if one of the valves give the message it "... is open", the Supply-Pump must be ON

normally only one valve is open, in this case there is no matter, everything is ok,
*but* if a second valve goes open, then the first Valve-condition is not more available (Valve(n) is undefined) as an effect when the second closes again, then the status "no supply pump" is set, although one valve is still open

so I can't understand the message "Valve(n) is undefined" - because in the Context-Data "Flow" there the first Valve when goes open is displayed; when a second Valve goes open, then this will be displayed, the previous no more

Ok the way you have your flow laid out, every message will be going to all four of the 'Valve' function nodes. So if data is for valve 6 + 7, it will also go to 'Valve 1 + 3' and if it doesn't contain msg.Valve30pen, you will get the error.

I'm surprised you haven hit more with all the function nodes.

What does the data look like coning out of the link node? You might be able to add a switch node to direct each msg to the right function node.

p.s. when posting flows/code on a message, in order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

So use back-tics. You used the single quote mark but I corrected it in your post.

hi Paul

thank you for your hints and support
I have understand now

I have divide the Link-connections of this 4 groups
now it work fine

PS
sorry about the wrong placement of those ``` it was a bit late last night :innocent:

1 Like

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