How can I store a current value (2)

Hi,

Additional on my previous topic ( How can I store a current value ) I see that the the context.set("store",msg.payload) is stored at the second pulse from the kWh meter.
Is it possible that the value stores at the same time when the HOLD button is served?

This is important that someone is clicking on the "HOLD" button with no direct response.on the dashboard.

I'm confused. :slight_smile:

Your other thread is solved so I didn't read it too much.

Could you explain where HOLD comes into things?

You are receiving messages with kWh values and some of the values are stored.
I forget the specs for how they are stored.

There is a button connected to the node - function I'm guessing - that causes things to store the value (only because you mentioned it's name: HOLD)

But what do you want to happen again?

Ah, it was me who even helped/solved it for you.

I have CRAFT disease. (Don't ask)

Ok, we were keeping it off a dashboard in our posts.
(Only to keep it simple)

I got the code I posted that you said was the solution.

With that code, you press the hold and the next message received from the kWh is stored.
(In flow context)

Well, it can't as the hold message doesn't have any kWh data in it.

Well, you can give it some indication if you want.
This wasn't mentioned in the original post.

But let's - for now - keep to the question you asked and sorry, I still don't fully understand.

Thinking.....

I don't know how you get th kWh values.
Yes, they're from a node that is sending them out, or an MQTT node receiving them.

IF (big if) it is a NODE:
Can you send a message into that node?

Although I'm not 100% sure of what you are trying to do, this might be a viable approach.

  1. Store every incoming value in a flow context variable flow.latestValue.
    This just needs a change node in your normal message processing flow

  2. Connect the output of the button to another change node to copy flow.latestValue to flow.heldValue.

Yes the kWh pulses coming via MQTT from Domoticz. But I will receive also pulses from the watermeter and the time between pulses is to long so that's the reason for my question.
I will try the option @jbudd, I let you know.

I did not completely understand this option. I will test it for the watermeter first, below my flow.

[
    {
        "id": "02c8da9c9f004ce0",
        "type": "rpi-gpio in",
        "z": "daac5e65e6e72986",
        "name": "Watermeter puls (L1)",
        "pin": "23",
        "intype": "tri",
        "debounce": "25",
        "read": true,
        "bcm": true,
        "x": 200,
        "y": 1660,
        "wires": [
            [
                "ce8d952f.18666"
            ]
        ]
    },
    {
        "id": "fda0a334.bc9588",
        "type": "inject",
        "z": "daac5e65e6e72986",
        "name": "reset",
        "repeat": "",
        "crontab": "",
        "once": false,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 190,
        "y": 1800,
        "wires": [
            [
                "17cce981.975686"
            ]
        ]
    },
    {
        "id": "ce8d952f.18666",
        "type": "function",
        "z": "daac5e65e6e72986",
        "name": "msg object",
        "func": "msg.increment = 1;\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 390,
        "y": 1720,
        "wires": [
            [
                "ac65bf94.6e609"
            ]
        ]
    },
    {
        "id": "17cce981.975686",
        "type": "function",
        "z": "daac5e65e6e72986",
        "name": "msg object",
        "func": "msg.reset = true;\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 390,
        "y": 1800,
        "wires": [
            [
                "ac65bf94.6e609"
            ]
        ]
    },
    {
        "id": "ac65bf94.6e609",
        "type": "counter",
        "z": "daac5e65e6e72986",
        "name": "",
        "init": "100",
        "step": 1,
        "lower": null,
        "upper": null,
        "mode": "increment",
        "outputs": 1,
        "x": 560,
        "y": 1740,
        "wires": [
            [
                "c2893b966e892354",
                "fba855e07eca3774",
                "c9adc4cdcdda4625",
                "96beafdd58160cd1"
            ]
        ]
    },
    {
        "id": "fba855e07eca3774",
        "type": "function",
        "z": "daac5e65e6e72986",
        "name": "Watermeter",
        "func": "flow.set(\"water\",msg.count)\nlet waterT = flow.get(\"water\")\nmsg.payload = waterT;\nmsg.topic = \"Totaal water\"\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 1740,
        "wires": [
            [
                "04564826297c0bc0"
            ]
        ]
    },
    {
        "id": "285e0e19.b9680a",
        "type": "inject",
        "z": "daac5e65e6e72986",
        "name": "increment",
        "repeat": "",
        "crontab": "",
        "once": false,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 1720,
        "wires": [
            [
                "ce8d952f.18666"
            ]
        ]
    },
    {
        "id": "a8ad38766f6fa0c2",
        "type": "change",
        "z": "daac5e65e6e72986",
        "name": "get value water",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "#:(file)::wstore",
                "tot": "flow"
            },
            {
                "t": "set",
                "p": "whold",
                "pt": "flow",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1060,
        "y": 1860,
        "wires": [
            [
                "0ca4f578aa918e28"
            ]
        ]
    },
    {
        "id": "0ca4f578aa918e28",
        "type": "function",
        "z": "daac5e65e6e72986",
        "name": "Water totaal",
        "func": "var waterT = msg.payload;\nmsg.payload = waterT / 1000 + (\" m3\");\nmsg.topic = \"Totaal water\"\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1250,
        "y": 1860,
        "wires": [
            [
                "6ec8afb177c37c43"
            ]
        ]
    },
    {
        "id": "f8d5be30f9e348a5",
        "type": "inject",
        "z": "daac5e65e6e72986",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "HOLD",
        "payloadType": "str",
        "x": 170,
        "y": 1860,
        "wires": [
            [
                "c9adc4cdcdda4625"
            ]
        ]
    },
    {
        "id": "6ec8afb177c37c43",
        "type": "ui-text",
        "z": "daac5e65e6e72986",
        "group": "927da3575e7dfcde",
        "order": 28,
        "width": 2,
        "height": 0,
        "name": "Vastleggen water",
        "label": "Water totaal:",
        "format": "{{msg.payload}}",
        "layout": "row-left",
        "style": true,
        "font": "Arial Black,Arial Black,Gadget,sans-serif",
        "fontSize": "14",
        "color": "#000000",
        "wrapText": false,
        "className": "",
        "value": "payload",
        "valueType": "msg",
        "x": 1450,
        "y": 1860,
        "wires": []
    },
    {
        "id": "0ad55939fdce4995",
        "type": "ui-text",
        "z": "daac5e65e6e72986",
        "group": "927da3575e7dfcde",
        "order": 12,
        "width": 2,
        "height": 0,
        "name": "Water totaal",
        "label": "Water totaal:",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "style": true,
        "font": "Arial Black,Arial Black,Gadget,sans-serif",
        "fontSize": "16",
        "color": "#000000",
        "wrapText": false,
        "className": "",
        "value": "payload",
        "valueType": "msg",
        "x": 1430,
        "y": 1740,
        "wires": []
    },
    {
        "id": "992b6aa48bb386f6",
        "type": "trigger",
        "z": "daac5e65e6e72986",
        "name": "",
        "op1": "false",
        "op2": "true",
        "op1type": "bool",
        "op2type": "bool",
        "duration": "250",
        "extend": false,
        "overrideDelay": false,
        "units": "ms",
        "reset": "true",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 1040,
        "y": 1680,
        "wires": [
            [
                "99f2b642925a51dc"
            ]
        ]
    },
    {
        "id": "c2893b966e892354",
        "type": "function",
        "z": "daac5e65e6e72986",
        "name": "Water tarief pulsen",
        "func": "const thisvalue = msg.count  // Extract the latest value from payload\nconst lastvalue = context.get(\"lastvalueW\") || true  // and previous value from context\n\nif (thisvalue > lastvalue) {   // Do some sort of comparison\n    msg.payload = false\n}\nelse {\n    msg.payload = true\n}\n\ncontext.set(\"lastvalueW\", thisvalue)\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 810,
        "y": 1680,
        "wires": [
            [
                "992b6aa48bb386f6"
            ]
        ]
    },
    {
        "id": "99f2b642925a51dc",
        "type": "ui-led",
        "z": "daac5e65e6e72986",
        "name": "Water tatief led",
        "group": "927da3575e7dfcde",
        "order": 13,
        "width": 1,
        "height": 0,
        "label": "",
        "labelPlacement": "left",
        "labelAlignment": "center",
        "states": [
            {
                "value": "false",
                "valueType": "bool",
                "color": "#0000ff"
            },
            {
                "value": "true",
                "valueType": "bool",
                "color": "#ffffff"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "circle",
        "showBorder": true,
        "showGlow": true,
        "x": 1440,
        "y": 1680,
        "wires": []
    },
    {
        "id": "04564826297c0bc0",
        "type": "function",
        "z": "daac5e65e6e72986",
        "name": "Water totaal",
        "func": "let waterT = msg.count;\nmsg.payload = waterT / 1000 + (\" m3\");\n\nmsg.topic = \"Totaal water\"\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1030,
        "y": 1740,
        "wires": [
            [
                "0ad55939fdce4995"
            ]
        ]
    },
    {
        "id": "c9adc4cdcdda4625",
        "type": "function",
        "z": "daac5e65e6e72986",
        "name": "Water hold",
        "func": "const message = msg.payload\nlet whold = flow.get(\"holdw\") || 0\n\n//  If `HOLD`\nif (message == \"HOLD\")\n{\n    flow.set(\"holdw\",1)\n    return  //  Nothing more to do be hone here/now\n}\n\nif (whold == 1)\n{\n    //  `hold` is set.\n    flow.set(\"wstore\",msg.count, \"file\")\n    flow.set(\"holdw\",0)   //  stop next message being storred too.\n}\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 770,
        "y": 1860,
        "wires": [
            [
                "a8ad38766f6fa0c2"
            ]
        ]
    },
    {
        "id": "96beafdd58160cd1",
        "type": "change",
        "z": "daac5e65e6e72986",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "latestValue",
                "pt": "flow",
                "to": "count",
                "tot": "msg",
                "dc": true
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 810,
        "y": 1800,
        "wires": [
            [
                "05c575a78364c32a"
            ]
        ]
    },
    {
        "id": "05c575a78364c32a",
        "type": "change",
        "z": "daac5e65e6e72986",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "heldvalue",
                "pt": "flow",
                "to": "LatestValue",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1050,
        "y": 1800,
        "wires": [
            []
        ]
    },
    {
        "id": "927da3575e7dfcde",
        "type": "ui-group",
        "name": "Nr.11 Verbruik en tellerstanden",
        "page": "a5219b6b7f535c4d",
        "width": 3,
        "height": 1,
        "order": 3,
        "showTitle": true,
        "className": "",
        "visible": "true",
        "disabled": "false",
        "groupType": "default"
    },
    {
        "id": "a5219b6b7f535c4d",
        "type": "ui-page",
        "name": "Nr.11 Groepen / verbruik / WiFi",
        "ui": "9db73792cdc62e1e",
        "path": "/page8",
        "icon": "home",
        "layout": "grid",
        "theme": "2afc50d1d1cfac95",
        "breakpoints": [
            {
                "name": "Default",
                "px": "0",
                "cols": "3"
            },
            {
                "name": "Tablet",
                "px": "576",
                "cols": "6"
            },
            {
                "name": "Small Desktop",
                "px": "768",
                "cols": "9"
            },
            {
                "name": "Desktop",
                "px": "1024",
                "cols": "12"
            }
        ],
        "order": 9,
        "className": "",
        "visible": "true",
        "disabled": "false"
    },
    {
        "id": "9db73792cdc62e1e",
        "type": "ui-base",
        "name": "Muntsetille",
        "path": "/dashboard",
        "appIcon": "",
        "includeClientData": true,
        "acceptsClientConfig": [
            "ui-notification",
            "ui-control"
        ],
        "showPathInSidebar": false,
        "headerContent": "dashpage",
        "navigationStyle": "default",
        "titleBarStyle": "default",
        "showReconnectNotification": true,
        "notificationDisplayTime": 1,
        "showDisconnectNotification": true,
        "allowInstall": false
    },
    {
        "id": "2afc50d1d1cfac95",
        "type": "ui-theme",
        "name": "Default Theme",
        "colors": {
            "surface": "#924f96",
            "primary": "#ccc500",
            "bgPage": "#c19a9a",
            "groupBg": "#84b4e1",
            "groupOutline": "#cccccc"
        },
        "sizes": {
            "density": "default",
            "pagePadding": "12px",
            "groupGap": "4px",
            "groupBorderRadius": "4px",
            "widgetGap": "12px"
        }
    },
    {
        "id": "b23bc31c4967b732",
        "type": "global-config",
        "env": [],
        "modules": {
            "node-red-node-pi-gpio": "2.0.7",
            "node-red-contrib-counter": "0.1.6",
            "@flowfuse/node-red-dashboard": "1.30.2",
            "@flowfuse/node-red-dashboard-2-ui-led": "1.1.0"
        }
    }
]

(sorry for the dutch text)

@jbudd has a good idea in some ways, but reading what you just said, I am not sure it will really work.

(No offence @jbudd)

Let's keep it to the kWh thing, and forgive me for now being vague in how I'm describing things.

So your kWh sends you message every 10 minutes.

You want the value stored 5 minutes after receipt of the last one.
There is no magic way that you can get THE reading now.
(referring back to this.)

Is it possible that the value stores at the same time when the HOLD button is served?

Because you only get the value every 10 minutes.
Pressing the hold button can't make a value happen.

Well, maybe you can force the value to be sent if you send the thing a message.
I don't know. You need to expand on what the device is and what commands it accepts.
(How do you configure it to send the kWh values at the given interval, etc?)

When we know more about that we may be able to make something more to what you are asking.

But I'm not understanding WHY you want this any way.
You are getting the value every 10 minutes.
It is stored, and the most recent one is kept.
A slight tweak along the line of what @jbudd was saying would go a long way to resolving how my original flow worked.

Though with little effort you could modify mine to do what was said anyway.

Did you ever use the Read stored value inject node?

This is a quick effort at what you may want.

[{"id":"d5990187685fe0bf","type":"inject","z":"26262ba1.62dcbc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":6480,"y":6650,"wires":[["0b397ef8641455c3"]]},{"id":"0b397ef8641455c3","type":"function","z":"26262ba1.62dcbc","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":6670,"y":6650,"wires":[["15bb1ef2aa6a1e40"]]},{"id":"3c215e18f98675e6","type":"inject","z":"26262ba1.62dcbc","name":"Read stored value  AKA HOLD?","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":6550,"y":6700,"wires":[["636bfb6f41d5f45e"]]},{"id":"636bfb6f41d5f45e","type":"change","z":"26262ba1.62dcbc","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":6930,"y":6700,"wires":[["4ed820dd2290e21c"]]},{"id":"4ed820dd2290e21c","type":"debug","z":"26262ba1.62dcbc","name":"Look","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":7220,"y":6700,"wires":[]},{"id":"1bea07235e196dda","type":"debug","z":"26262ba1.62dcbc","name":"to Where ever","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":7250,"y":6650,"wires":[]},{"id":"15bb1ef2aa6a1e40","type":"change","z":"26262ba1.62dcbc","name":"Store the value for later access","rules":[{"t":"set","p":"store","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":7000,"y":6650,"wires":[["1bea07235e196dda"]]}]

to where ever and look are shown as TWO nodes. But you could connect them together and maybe get what you want.

Thanks, I will try to implement this, I will think about it.
The time between kWh pulses is approximately @200W three seconds.

The problem with the watermeter is, when there is less waterflow through the meter, the time is too long between two pulses. When I push the hold button, the value change to the hold value at the next flowmeter pulse.

I tet it know when I have a solution.