# Filter after Counter

**URL:** <https://discourse.nodered.org/t/filter-after-counter/70048>\
**Category:** General\
**Created:** [3 November 2022 17:12 UTC](https://discourse.nodered.org/t/filter-after-counter/70048 "2022-11-03T17:12:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ELParreira](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@ELParreira](https://discourse.nodered.org/u/ELParreira)\
**Post date:** [3 November 2022 17:12 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/1 "2022-11-03T17:12:44Z")

</div>

Hello to all,

I'm having a brain meltdown since I have a code to send error messages, I want to receive a message after 3 errors, so I introduced a counter and a filter.  
It warns when I have 3 error but not when I stop having error, but if I force it in the counter inject buttons it works fine. Can someone please explain me of how it work in one way and not in the other?

Cheers

```auto
[
    {
        "id": "2f1d5163f620e4e4",
        "type": "function",
        "z": "395651665a2c113d",
        "name": "clear counter",
        "func": "msg.reset = true;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 850,
        "y": 440,
        "wires": [
            [
                "f971d755bff2fc42"
            ]
        ]
    },
    {
        "id": "bb1c363d264036b0",
        "type": "inject",
        "z": "395651665a2c113d",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 650,
        "y": 440,
        "wires": [
            [
                "2f1d5163f620e4e4"
            ]
        ]
    },
    {
        "id": "baa08b80827e14d8",
        "type": "function",
        "z": "395651665a2c113d",
        "name": "inccounter",
        "func": "msg.increment = 1;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 850,
        "y": 400,
        "wires": [
            [
                "f971d755bff2fc42"
            ]
        ]
    },
    {
        "id": "1ad9cd8083c9deb9",
        "type": "inject",
        "z": "395651665a2c113d",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 650,
        "y": 400,
        "wires": [
            [
                "baa08b80827e14d8"
            ]
        ]
    },
    {
        "id": "d06e756b50182275",
        "type": "counter",
        "z": "395651665a2c113d",
        "name": "",
        "init": "0",
        "step": 1,
        "lower": "",
        "upper": "",
        "mode": "increment",
        "outputs": 1,
        "x": 1060,
        "y": 420,
        "wires": [
            [
                "adcd6e4aca0a6848",
                "97afb36c2599eece"
            ]
        ]
    },
    {
        "id": "428b0112708bac30",
        "type": "debug",
        "z": "395651665a2c113d",
        "name": "message",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "topic",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1900,
        "y": 420,
        "wires": []
    },
    {
        "id": "1d40fb8f00d8c675",
        "type": "function",
        "z": "395651665a2c113d",
        "name": "Config. Messages",
        "func": "if (msg.CommError == 0)\n{\n msg.topic = 'Commm Error Restored';\n msg.payload = 'The comunication is restored';\n}\nif (msg.CommError == 1 )\n{\n msg.topic = 'Commm Error DOWN';\n msg.payload = 'The comunication is DOWN since ';\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1690,
        "y": 420,
        "wires": [
            [
                "428b0112708bac30"
            ]
        ]
    },
    {
        "id": "97afb36c2599eece",
        "type": "function",
        "z": "395651665a2c113d",
        "name": "Errors > 2",
        "func": "//ALTERAR NOME CONTADOR(msg.error != 'Error: Timed out' || msg.error == 'Error: Client Not Ready To Read At State reconnecting' )\n\nif (msg.count > 1 )\n{\n msg.CommError = 1;\n}\nelse\n{\n msg.CommError = 0;\n} \n\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1240,
        "y": 420,
        "wires": [
            [
                "5c06ed3d6c4764f9",
                "c2088301799b0f41"
            ]
        ],
        "icon": "node-red/alert.svg"
    },
    {
        "id": "c2088301799b0f41",
        "type": "debug",
        "z": "395651665a2c113d",
        "name": "CommError",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1410,
        "y": 480,
        "wires": []
    },
    {
        "id": "adcd6e4aca0a6848",
        "type": "debug",
        "z": "395651665a2c113d",
        "name": "count",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1210,
        "y": 480,
        "wires": []
    },
    {
        "id": "5c06ed3d6c4764f9",
        "type": "rbe",
        "z": "395651665a2c113d",
        "name": "report-by-exception",
        "func": "rbei",
        "gap": "",
        "start": "",
        "inout": "out",
        "septopics": true,
        "property": "CommError",
        "topi": "topic",
        "x": 1450,
        "y": 420,
        "wires": [
            [
                "1d40fb8f00d8c675"
            ]
        ]
    },
    {
        "id": "67dc3acdbce1e9f7",
        "type": "inject",
        "z": "395651665a2c113d",
        "name": "count = 0",
        "props": [
            {
                "p": "count",
                "v": "0",
                "vt": "num"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 1040,
        "y": 340,
        "wires": [
            [
                "97afb36c2599eece"
            ]
        ]
    },
    {
        "id": "4707e2c1178e5160",
        "type": "inject",
        "z": "395651665a2c113d",
        "name": "count = 3",
        "props": [
            {
                "p": "count",
                "v": "3",
                "vt": "num"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 1040,
        "y": 300,
        "wires": [
            [
                "97afb36c2599eece"
            ]
        ]
    },
    {
        "id": "f971d755bff2fc42",
        "type": "junction",
        "z": "395651665a2c113d",
        "x": 980,
        "y": 420,
        "wires": [
            [
                "d06e756b50182275"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 November 2022 17:59 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/2 "2022-11-03T17:59:32Z")

</div>

Where do you set msg.topic?

---

<div class="post-metadata">

**Author:** ![ELParreira](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@ELParreira](https://discourse.nodered.org/u/ELParreira)\
**Post date:** [4 November 2022 09:07 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/3 "2022-11-04T09:07:46Z")

</div>

Hello Zenofmud,

The problem is not with the message config block, my problem is when i reset the counter and my alarm bit goes to 0 the filter doesn't recognize the state change. Like it does when I feed the error \>2 block with the two injection nodes.

Cheers

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 November 2022 10:51 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/4 "2022-11-04T10:51:40Z")

</div>

In the `function` node labeled 'Errors \> 2' you have

```auto
//ALTERAR NOME CONTADOR(msg.error != 'Error: Timed out' || msg.error == 'Error: Client Not Ready To Read At State reconnecting' )

if (msg.count > 1 )
{
    msg.CommError = 1;
}
else
{
    msg.CommError = 0;
} 

return msg;

```

Your 'if' statement and function name do not match.

When you use the `inject` node connected to the 'clear counter' `function` node, msg.count is set to 0 so the msg will never pass the 'Errors \> 2' function node.

---

<div class="post-metadata">

**Author:** ![ELParreira](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@ELParreira](https://discourse.nodered.org/u/ELParreira)\
**Post date:** [4 November 2022 11:34 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/5 "2022-11-04T11:34:33Z")

</div>

Hello once again Zenofmud,

Thank you for your help.

I'm not understanding. If I do inject in the clear counter node, I will have after the "Error \>2 " output count: 0 CommError: 0.

Cheers

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 November 2022 12:10 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/6 "2022-11-04T12:10:20Z")

</div>

try changing the filter and set 'Mode' to 'block unless value changes`

---

<div class="post-metadata">

**Author:** ![ELParreira](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@ELParreira](https://discourse.nodered.org/u/ELParreira)\
**Post date:** [16 November 2022 14:42 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/7 "2022-11-16T14:42:49Z")

</div>

Sorry for taking so much time to answer back.  
If I set it to block unless value changes, it always sends a message when the value of the msg.CommError is 0.  
That is why I've set it to block unless value changes (ignoring initial value).  
Maybe the problem is there, but still isn't the solution I need.

Cheers

---

<div class="post-metadata">

**Author:** ![knoepsche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knoepsche/32/69450_2.png) [@knoepsche](https://discourse.nodered.org/u/knoepsche)\
**Post date:** [16 November 2022 18:11 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/8 "2022-11-16T18:11:51Z")

</div>

I think the filter node reacts on the item msg.reset which you use for the counter node. I deleted the item "msg.reset" and I think this way it works:

(this post was edited....my guess before was wrong!!!)

```auto
[
    {
        "id": "67200c59f49ebe9f",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f971d755bff2fc42",
        "type": "junction",
        "z": "67200c59f49ebe9f",
        "x": 580,
        "y": 420,
        "wires": [
            [
                "d06e756b50182275"
            ]
        ]
    },
    {
        "id": "2f1d5163f620e4e4",
        "type": "function",
        "z": "67200c59f49ebe9f",
        "name": "clear counter",
        "func": "msg.reset = true;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 440,
        "wires": [
            [
                "f971d755bff2fc42"
            ]
        ]
    },
    {
        "id": "bb1c363d264036b0",
        "type": "inject",
        "z": "67200c59f49ebe9f",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 150,
        "y": 440,
        "wires": [
            [
                "2f1d5163f620e4e4"
            ]
        ]
    },
    {
        "id": "baa08b80827e14d8",
        "type": "function",
        "z": "67200c59f49ebe9f",
        "name": "inccounter",
        "func": "msg.increment = 1;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 400,
        "wires": [
            [
                "f971d755bff2fc42"
            ]
        ]
    },
    {
        "id": "1ad9cd8083c9deb9",
        "type": "inject",
        "z": "67200c59f49ebe9f",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 150,
        "y": 400,
        "wires": [
            [
                "baa08b80827e14d8"
            ]
        ]
    },
    {
        "id": "428b0112708bac30",
        "type": "debug",
        "z": "67200c59f49ebe9f",
        "name": "message",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "topic",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1700,
        "y": 420,
        "wires": []
    },
    {
        "id": "1d40fb8f00d8c675",
        "type": "function",
        "z": "67200c59f49ebe9f",
        "name": "Config. Messages",
        "func": "if (msg.CommError == 0)\n{\n msg.topic = 'Commm Error Restored';\n msg.payload = 'The comunication is restored';\n}\nif (msg.CommError == 1 )\n{\n msg.topic = 'Commm Error DOWN';\n msg.payload = 'The comunication is DOWN since ';\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1490,
        "y": 420,
        "wires": [
            [
                "428b0112708bac30"
            ]
        ]
    },
    {
        "id": "97afb36c2599eece",
        "type": "function",
        "z": "67200c59f49ebe9f",
        "name": "Errors > 2",
        "func": "//ALTERAR NOME CONTADOR(msg.error != 'Error: Timed out' || msg.error == 'Error: Client Not Ready To Read At State reconnecting' )\n\nif (msg.payload > 2 )\n{\n msg.CommError = 1;\n}\nelse\n{\n msg.CommError = 0;\n} \ndelete msg.reset;\ndelete msg.increment;\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 940,
        "y": 420,
        "wires": [
            [
                "5c06ed3d6c4764f9",
                "c2088301799b0f41"
            ]
        ],
        "icon": "node-red/alert.svg"
    },
    {
        "id": "c2088301799b0f41",
        "type": "debug",
        "z": "67200c59f49ebe9f",
        "name": "CommError",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1010,
        "y": 480,
        "wires": []
    },
    {
        "id": "adcd6e4aca0a6848",
        "type": "debug",
        "z": "67200c59f49ebe9f",
        "name": "count",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 810,
        "y": 480,
        "wires": []
    },
    {
        "id": "5c06ed3d6c4764f9",
        "type": "rbe",
        "z": "67200c59f49ebe9f",
        "name": "report-by-exception",
        "func": "rbei",
        "gap": "",
        "start": "",
        "inout": "out",
        "septopics": false,
        "property": "CommError",
        "topi": "topic",
        "x": 1210,
        "y": 420,
        "wires": [
            [
                "1d40fb8f00d8c675"
            ]
        ]
    },
    {
        "id": "67dc3acdbce1e9f7",
        "type": "inject",
        "z": "67200c59f49ebe9f",
        "name": "count = 0",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 640,
        "y": 340,
        "wires": [
            [
                "97afb36c2599eece"
            ]
        ]
    },
    {
        "id": "4707e2c1178e5160",
        "type": "inject",
        "z": "67200c59f49ebe9f",
        "name": "count = 3",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "3",
        "payloadType": "num",
        "x": 640,
        "y": 300,
        "wires": [
            [
                "97afb36c2599eece"
            ]
        ]
    },
    {
        "id": "d06e756b50182275",
        "type": "counter",
        "z": "67200c59f49ebe9f",
        "inc": "1",
        "name": "",
        "x": 660,
        "y": 420,
        "wires": [
            [
                "adcd6e4aca0a6848",
                "97afb36c2599eece"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![ELParreira](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@ELParreira](https://discourse.nodered.org/u/ELParreira)\
**Post date:** [17 November 2022 15:33 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/9 "2022-11-17T15:33:03Z")

</div>

Thank u @knoepsche, it worked perfectly.  
Cheers

---

<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:** [16 January 2023 15:33 UTC](https://discourse.nodered.org/t/filter-after-counter/70048/10 "2023-01-16T15:33:11Z")

</div>

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