Trigger after multiple of message

I've been experimenting with detection, and am trying to weed out false negatives and positives. Essentially, I want a message saying a state has changed when it has been different for the past n messages.

Using counters and switches, I've done this:

[
    {
        "id": "a48fa9.dd5a7058",
        "type": "tab",
        "label": "Test-flow",
        "disabled": false,
        "info": ""
    },
    {
        "id": "e255e559.b5b308",
        "type": "switch",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "property": "count",
        "propertyType": "msg",
        "rules": [
            {
                "t": "gt",
                "v": "5",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 790,
        "y": 320,
        "wires": [
            [
                "ebba2a29.f9d2a8"
            ]
        ]
    },
    {
        "id": "d4daa5e7.259228",
        "type": "counter",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "init": "0",
        "step": "1",
        "lower": "",
        "upper": "5",
        "mode": "increment",
        "outputs": "1",
        "x": 660,
        "y": 320,
        "wires": [
            [
                "e255e559.b5b308"
            ]
        ]
    },
    {
        "id": "3ed91eba.0d9ad2",
        "type": "counter",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "init": "0",
        "step": "1",
        "lower": "",
        "upper": "5",
        "mode": "increment",
        "outputs": "1",
        "x": 660,
        "y": 240,
        "wires": [
            [
                "7cddf176.694cf"
            ]
        ]
    },
    {
        "id": "3e89afb5.27b6b",
        "type": "switch",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 450,
        "y": 280,
        "wires": [
            [
                "3ed91eba.0d9ad2",
                "5e184d7e.582074"
            ],
            [
                "d4daa5e7.259228",
                "c306f940.066d38"
            ]
        ]
    },
    {
        "id": "c306f940.066d38",
        "type": "change",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "reset",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 340,
        "wires": [
            [
                "3ed91eba.0d9ad2"
            ]
        ]
    },
    {
        "id": "5e184d7e.582074",
        "type": "change",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "reset",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 220,
        "wires": [
            [
                "d4daa5e7.259228"
            ]
        ]
    },
    {
        "id": "7cddf176.694cf",
        "type": "switch",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "property": "count",
        "propertyType": "msg",
        "rules": [
            {
                "t": "gte",
                "v": "5",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 790,
        "y": 240,
        "wires": [
            [
                "ebba2a29.f9d2a8"
            ]
        ]
    },
    {
        "id": "468821a5.6febc",
        "type": "inject",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "topic": "",
        "payload": "false",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 290,
        "y": 300,
        "wires": [
            [
                "3e89afb5.27b6b"
            ]
        ]
    },
    {
        "id": "ebba2a29.f9d2a8",
        "type": "debug",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 990,
        "y": 280,
        "wires": []
    },
    {
        "id": "91c8db87.d30ca8",
        "type": "inject",
        "z": "a48fa9.dd5a7058",
        "name": "",
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 290,
        "y": 260,
        "wires": [
            [
                "3e89afb5.27b6b"
            ]
        ]
    }
]

I'm still a bit new to Node Red, and I think I'm missing something very simple.

Please do a search through the core nodes and on the flows site. There are nodes that will do this for you. Hint: RBE.

Are you sure about that? It can check for changed by a defined amount, but I can't see how it does what is required here.

Is what you actually want that it passes on a message after it has been the same for the past n messages, after being something different.
node-red-contrib-debounce may do what you want if the messages are coming in regularly, so the value should be the same for a defined period rather than a number of messages.

:slight_smile: nope!

Is that nope you are not sure or nope my suggestion is false?

Haha, I would never suggest that you might be wrong Colin :wink:

But I would certainly suggest that I might be.

3 Likes

Instead of 'wrong', I prefer 'alternative' :wink:

My first attempt was with switches and counters, but the RBE node works just as well in place of the switches, and offers a whole different kind of flexibility.

Using debouncing could work, but it may make the flow a lot more complicated and less flexible. I've made a flow together with the join node that works quite well, though.

But in searching for debouncing I also found a batching node (node-red-contrib-batcher), which spits out an array of accumulated payloads. I can then test if there's a true in there, which solves my false negatives. Since the errant false messages tend to come sporadically, a batch of three solves it.

EDIT: it also works with the normal batcher node.

Thanks everyone!

2 Likes