Node-red-contrib-counter

No, you don't.
Replace it with a change node, and save/retrieve your data values as described in the node-RED documentation.

Also, if you share any flows or code, please use this method - How to share code or flow json

i have playes around a little.
this methode from @JGKK is incrementing the number by 2 counts each time...

Instead of writing the count in a File can i not just store it in iobroker?
Please have a look at the flow, it is not working but that is how i think it should work in my logic.

Blockquote [
{
"id": "78b19c4c.4aea74",
"type": "rpi-gpio in",
"z": "dd8f053.697b4f8",
"d": true,
"name": "",
"pin": "11",
"intype": "tri",
"debounce": "10",
"read": false,
"x": 150,
"y": 200,
"wires": [
[
"4c20b33d.ac5f8c"
]
]
}
]
<

Should it not grab the "Old" count in the run?

Please recheck your last post, you haven't posted the full flow... also before going back & editing it, please read and use this post - How to share code or flow json otherwise nobody will be able to examine your problem.

Here is a simpler version of @JGKK flow using a flow variable. In the "Reset" path it will delete the flow variable and reset the counter. In the "flow.counter = count (rounded to 3 decimals)" change node it rounds the count to 3 decimals bcause there looks likethere is a bug in the counter node when incrimenting by .001

When it it is at 0.008 its fine but the next incrementation responds with 0.009000000000000001

[UPDATE] I'll open an issue about this in GitHub See my next post in this thread

[{"id":"33f1976d.907798","type":"inject","z":"674b54ba.c0ebf4","name":"increment","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":420,"wires":[["381f2c71.f7670c"]]},{"id":"381f2c71.f7670c","type":"counter","z":"674b54ba.c0ebf4","name":"","init":"0","step":".001","lower":"","upper":"","mode":"increment","outputs":"1","x":540,"y":480,"wires":[["295107a1.67cc58","b98146ef.0ab288"]]},{"id":"b98146ef.0ab288","type":"change","z":"674b54ba.c0ebf4","name":"flow.counter = count (rounded to 3 decimals)","rules":[{"t":"set","p":"counter","pt":"flow","to":"$round(count, 3)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":480,"wires":[[]]},{"id":"9b5e941a.b9cbf","type":"inject","z":"674b54ba.c0ebf4","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":480,"wires":[["6f0843b0.c5be9c"]]},{"id":"6f0843b0.c5be9c","type":"change","z":"674b54ba.c0ebf4","name":"delete flow.counter, reset count","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"},{"t":"delete","p":"counter","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":480,"wires":[["381f2c71.f7670c"]]},{"id":"295107a1.67cc58","type":"debug","z":"674b54ba.c0ebf4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"count","targetType":"msg","x":710,"y":420,"wires":[]}]
1 Like

You are seeing an inherent problem with floating point arithmetic. Since numbers generally cannot be represented exactly then if you keep adding the same number on to a value then the error slowly accumulates. Try this in a function node

msg.payload = 0
for (let i=0; i<9; i++) {
    msg.payload += .001
}
return msg;

and you will find that you end up with 0.009000000000000001
The only way to avoid that is to instead increment an integer (so 0, 1, 2, 3, ...) and multiply that by the base (0.001). That will avoid the error building up, until you get to very large numbers such that the integer itself cannot be stored accurately.

@kxn25
UPDATE: after more research, this is not a bug in node-red-contrib-counter, it is an issue with how javascript handles decimals. Ther is a great explaination at http://adripofjavascript.com/blog/drips/avoiding-problems-with-decimal-math-in-javascript.html

The solution for you is to use whole numbers and device by 1000 when you want to use it. Here is an updated flow as an example:

[{"id":"33f1976d.907798","type":"inject","z":"674b54ba.c0ebf4","name":"increment","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":420,"wires":[["381f2c71.f7670c"]]},{"id":"381f2c71.f7670c","type":"counter","z":"674b54ba.c0ebf4","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":"1","x":540,"y":480,"wires":[["295107a1.67cc58","b98146ef.0ab288"]]},{"id":"b98146ef.0ab288","type":"change","z":"674b54ba.c0ebf4","name":"flow.counter = count ","rules":[{"t":"set","p":"counter","pt":"flow","to":"count","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"count/1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":480,"wires":[["ea0c2a5f.fcd858"]]},{"id":"9b5e941a.b9cbf","type":"inject","z":"674b54ba.c0ebf4","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":480,"wires":[["6f0843b0.c5be9c"]]},{"id":"6f0843b0.c5be9c","type":"change","z":"674b54ba.c0ebf4","name":"delete flow.counter, reset count","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"},{"t":"delete","p":"counter","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":480,"wires":[["381f2c71.f7670c"]]},{"id":"295107a1.67cc58","type":"debug","z":"674b54ba.c0ebf4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"count","targetType":"msg","x":710,"y":420,"wires":[]},{"id":"ea0c2a5f.fcd858","type":"debug","z":"674b54ba.c0ebf4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"count","statusType":"msg","x":930,"y":480,"wires":[]}]
    {
        "id": "a502b94c.ad8818",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "bae7fc45.c415e",
        "type": "rpi-gpio in",
        "z": "a502b94c.ad8818",
        "name": "",
        "pin": "11",
        "intype": "tri",
        "debounce": "10",
        "read": false,
        "x": 90,
        "y": 180,
        "wires": [
            [
                "f1c00b23.b5b998"
            ]
        ]
    },
    {
        "id": "f1c00b23.b5b998",
        "type": "switch",
        "z": "a502b94c.ad8818",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 270,
        "y": 180,
        "wires": [
            [
                "ae5a4e97.10f6f"
            ]
        ]
    },
    {
        "id": "bdbaaf8a.facb2",
        "type": "ioBroker get object",
        "z": "a502b94c.ad8818",
        "name": "test",
        "topic": "Status.0.Strom.1OG_zähler.test",
        "attrname": "value",
        "x": 170,
        "y": 320,
        "wires": [
            [
                "ae5a4e97.10f6f"
            ]
        ]
    },
    {
        "id": "ae5a4e97.10f6f",
        "type": "counter",
        "z": "a502b94c.ad8818",
        "name": "test",
        "init": "0",
        "step": "1",
        "lower": "",
        "upper": "",
        "mode": "increment",
        "outputs": "1",
        "x": 650,
        "y": 300,
        "wires": [
            [
                "34932715.52f888"
            ]
        ]
    },
    {
        "id": "418d7aa5.999744",
        "type": "change",
        "z": "a502b94c.ad8818",
        "d": true,
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "increment",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 360,
        "y": 420,
        "wires": [
            []
        ]
    },
    {
        "id": "f5e4e434.716b08",
        "type": "ioBroker out",
        "z": "a502b94c.ad8818",
        "name": "test",
        "topic": "Status.0.Strom.1OG_zähler.test",
        "ack": "false",
        "autoCreate": "false",
        "stateName": "",
        "role": "",
        "payloadType": "",
        "readonly": "",
        "stateUnit": "",
        "stateMin": "",
        "stateMax": "",
        "x": 1070,
        "y": 300,
        "wires": []
    },
    {
        "id": "50200bc9.437e94",
        "type": "change",
        "z": "a502b94c.ad8818",
        "d": true,
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "count",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 680,
        "y": 420,
        "wires": [
            []
        ]
    },
    {
        "id": "9e6cd677.b26a18",
        "type": "debug",
        "z": "a502b94c.ad8818",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1030,
        "y": 480,
        "wires": []
    },
    {
        "id": "20bc6bde.c0e654",
        "type": "ioBroker get",
        "z": "a502b94c.ad8818",
        "d": true,
        "name": "test",
        "topic": "Status.0.Strom.1OG_zähler.test",
        "attrname": "payload",
        "payloadType": "object",
        "x": 270,
        "y": 260,
        "wires": [
            [
                "ae5a4e97.10f6f"
            ]
        ]
    },
    {
        "id": "34932715.52f888",
        "type": "function",
        "z": "a502b94c.ad8818",
        "name": "Wandler",
        "func": "msg.payload=msg.count;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 880,
        "y": 300,
        "wires": [
            [
                "f5e4e434.716b08",
                "9e6cd677.b26a18"
            ]
        ]
    }
] ]

this should be the hole flow.
I want to grab the existing count from iobroker and increase by 1 an write it in iobroker again.
The devide by 1000 is a good thin but how am i iplementing all this?
And i only what to count the GPIO 1 not the 0.