Topic filter and block

Hi all
wondering if someone could help me, i have 2 input topics t3 and t1 and would like to pass t3 through unless t1 is present then pass that payload value through instead, like a logic gate, but instead of true false payload, works on topic.
t3 always has an output t1 only has output when other conditions are met
In the example below null equals no t1 exists just t3 so pass that through and where t3 and t1 exist ignore t3 and pass only t1
table

image below is just a mock up, function node has no code (that's where the help is needed)
topicFilter

I presume that these are two seperate messages with msg.topic equal either to "t1" or "t3"?
If I'm wrong, you need to show us some sample data.
As a flow based language, Node-red generally works on one message at a time.

So what do you mean by "Unless t1 is present"?
A message with msg.topic == "t1" has been seen in the last hour? The last millisecond?

Something like this might work

thank you for your time and reply, i have attached a test flow , when topic "t3 only" is injected and no t1 it passes through ,
t3

when topic "both t1 & t3" are injected they both pass through
t1

what would be great, when the both t1 & t3 are injected instead of above result, only t1 comes through
image

[
    {
        "id": "58ff0f0c7ec6488e",
        "type": "inject",
        "z": "d5122f1bc5394052",
        "name": "both t1 & t3",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 190,
        "y": 1040,
        "wires": [
            [
                "85140873867b8b36",
                "ed375b562d11dc7e"
            ]
        ]
    },
    {
        "id": "85140873867b8b36",
        "type": "change",
        "z": "d5122f1bc5394052",
        "name": "t1",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "1200",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "t1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 330,
        "y": 1020,
        "wires": [
            [
                "1e9b188320b2d93e"
            ]
        ]
    },
    {
        "id": "ed375b562d11dc7e",
        "type": "change",
        "z": "d5122f1bc5394052",
        "name": "t3",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "1400",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "t3",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 330,
        "y": 1060,
        "wires": [
            [
                "1e9b188320b2d93e"
            ]
        ]
    },
    {
        "id": "b9563c8afbc545e7",
        "type": "inject",
        "z": "d5122f1bc5394052",
        "name": "t3 only",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "t3",
        "payload": "1350",
        "payloadType": "num",
        "x": 330,
        "y": 1120,
        "wires": [
            [
                "1e9b188320b2d93e"
            ]
        ]
    },
    {
        "id": "7188bbfe6bf03793",
        "type": "debug",
        "z": "d5122f1bc5394052",
        "name": "debug 17",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 660,
        "y": 1040,
        "wires": []
    },
    {
        "id": "1e9b188320b2d93e",
        "type": "function",
        "z": "d5122f1bc5394052",
        "name": "does magic",
        "func": "\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 510,
        "y": 1040,
        "wires": [
            [
                "7188bbfe6bf03793"
            ]
        ]
    }
]

Although you only have one inject point, the change nodes create two discrete messages:
{"payload":1200,"topic":"t1"} and {"payload":1400,"topic":"t3"}.
These two messages are passed to the function node sequentially, not concurrently
They may pass milliseconds apart but the function can not know about the t1 and t3 values at the same time. This is a fundamental feature of flow based programming.

That is why my illustration shows briefly storing the t1 message in a "context variable". You can compare a message property with a context variable.

1 Like

As @jbudd says these messages are not in same message, which complicates things.
This example may do what you want and could be simplified if you do not require the topics after the join.

[{"id":"58ff0f0c7ec6488e","type":"inject","z":"d1395164b4eec73e","name":"both t1 & t3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":780,"wires":[["85140873867b8b36","ed375b562d11dc7e"]]},{"id":"85140873867b8b36","type":"change","z":"d1395164b4eec73e","name":"t1","rules":[{"t":"set","p":"payload","pt":"msg","to":"1200","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"t1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":760,"wires":[["428cc49853bd48c6"]]},{"id":"ed375b562d11dc7e","type":"change","z":"d1395164b4eec73e","name":"t3","rules":[{"t":"set","p":"payload","pt":"msg","to":"1400","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"t3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":800,"wires":[["428cc49853bd48c6"]]},{"id":"b9563c8afbc545e7","type":"inject","z":"d1395164b4eec73e","name":"t3 only","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"t3","payload":"1350","payloadType":"num","x":130,"y":880,"wires":[["428cc49853bd48c6"]]},{"id":"7188bbfe6bf03793","type":"debug","z":"d1395164b4eec73e","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":460,"y":1000,"wires":[]},{"id":"98fcd18020b27c55","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"0.25","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":610,"y":780,"wires":[["7a2f3e4563934d51"]]},{"id":"7a2f3e4563934d51","type":"switch","z":"d1395164b4eec73e","name":"","property":"$count($$.payload.*)","propertyType":"jsonata","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":860,"wires":[["9ef50986dd4171be"],["c88cbf20372c9e3c"]]},{"id":"9ef50986dd4171be","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.t3.topic","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.t3.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":840,"wires":[["7188bbfe6bf03793"]]},{"id":"428cc49853bd48c6","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"temp.value","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"temp.topic","pt":"msg","to":"topic","tot":"msg"},{"t":"move","p":"temp","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":780,"wires":[["98fcd18020b27c55"]]},{"id":"c88cbf20372c9e3c","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.t1.topic","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.t1.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":880,"wires":[["7188bbfe6bf03793"]]}]

Or similar with a delay and merging the payloads after they are converted to objects, and back after.

[{"id":"d1395164b4eec73e","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"6dc2dbe3657109d1","type":"ui_button","z":"d1395164b4eec73e","name":"old data","group":"39236f77681f6d72","order":2,"width":0,"height":0,"passthru":false,"label":"add old data","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"stop","payloadType":"str","topic":"topic","topicType":"msg","x":80,"y":240,"wires":[["eecb9db36db02643"]]},{"id":"eecb9db36db02643","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"},{"t":"set","p":"offset_period","pt":"msg","to":"120000","tot":"num"},{"t":"set","p":"payload","pt":"msg","to":"\t\t   [\t       {\t           \"series\": [\"Live PV\", \"Comparison PV\"],\t           \"data\": [\t               [\t                   {\t                       \"x\": $$.timestamp,\t                       \"y\": 0\t                   }\t               ],\t               [\t                   {\t                       \"x\": $$.timestamp - $$.offest_period,\t                       \"y\": 0\t                   }\t               ]\t           ]\t       }\t    ]\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":240,"wires":[["b97e707e8ba23f75","d088c81af7e7496e"]]},{"id":"b97e707e8ba23f75","type":"ui_chart","z":"d1395164b4eec73e","name":"PV-Verlauf","group":"39236f77681f6d72","order":0,"width":0,"height":0,"label":"PV-Verlauf","chartType":"line","legend":"true","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"2","removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#7f7f7f","#bcbd22"],"outputs":1,"useDifferentColor":false,"className":"","x":570,"y":240,"wires":[["710461b234ada76d","7582b114ef1c428c"]]},{"id":"d088c81af7e7496e","type":"trigger","z":"d1395164b4eec73e","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-1","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":140,"y":340,"wires":[["e80a53c4828c0a28"]]},{"id":"8470ab8f7f1c1dc0","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"temp","tot":"msg"},{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"},{"t":"set","p":"offset_period","pt":"msg","to":"120000","tot":"num"},{"t":"set","p":"payload","pt":"msg","to":"chartdata","tot":"flow"},{"t":"set","p":"payload[0].data[1]","pt":"msg","to":"$append(\t    $$.payload[0].data[1], \t    [\t        {\t            \"x\": $$.timestamp - $$.offset_period,\t            \"y\": $$.temp\t        }\t    ]\t)[$.x > $$.timestamp - $$.offset_period * 2]","tot":"jsonata"},{"t":"set","p":"payload[0].data[0]","pt":"msg","to":"$append(\t   $$.payload[0].data[0],\t   [\t       { \t           \"x\": $$.timestamp,\t           \"y\": $$.temp\t       }\t   ]\t)[$.x > $$.timestamp - $$.offset_period * 2]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":340,"wires":[["b97e707e8ba23f75","21e6fb5d80bd1e9c"]]},{"id":"710461b234ada76d","type":"debug","z":"d1395164b4eec73e","name":"chart","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":280,"wires":[]},{"id":"7582b114ef1c428c","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"chartdata","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":180,"wires":[[]]},{"id":"e80a53c4828c0a28","type":"change","z":"d1395164b4eec73e","name":"incoming mqtt payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round($random(), 3)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":340,"wires":[["8470ab8f7f1c1dc0"]]},{"id":"21e6fb5d80bd1e9c","type":"debug","z":"d1395164b4eec73e","name":"debug 343","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":400,"wires":[]},{"id":"1882200e2f591cda","type":"inject","z":"d1395164b4eec73e","name":"reset","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":50,"y":60,"wires":[["5c2308587af05ba5"]]},{"id":"5c2308587af05ba5","type":"ui_button","z":"d1395164b4eec73e","name":"","group":"39236f77681f6d72","order":2,"width":0,"height":0,"passthru":true,"label":"Reset","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":190,"y":60,"wires":[["4a60ce664d26b40b"]]},{"id":"4a60ce664d26b40b","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[]","tot":"json"},{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":60,"wires":[["d088c81af7e7496e","b97e707e8ba23f75"]]},{"id":"58ff0f0c7ec6488e","type":"inject","z":"d1395164b4eec73e","name":"both t1 & t3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":780,"wires":[["85140873867b8b36","ed375b562d11dc7e"]]},{"id":"85140873867b8b36","type":"change","z":"d1395164b4eec73e","name":"t1","rules":[{"t":"set","p":"payload","pt":"msg","to":"1200","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"t1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":760,"wires":[["7a2f3e4563934d51"]]},{"id":"ed375b562d11dc7e","type":"change","z":"d1395164b4eec73e","name":"t3","rules":[{"t":"set","p":"payload","pt":"msg","to":"1400","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"t3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":800,"wires":[["7a2f3e4563934d51"]]},{"id":"b9563c8afbc545e7","type":"inject","z":"d1395164b4eec73e","name":"t3 only","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"t3","payload":"1350","payloadType":"num","x":130,"y":880,"wires":[["7a2f3e4563934d51"]]},{"id":"7188bbfe6bf03793","type":"debug","z":"d1395164b4eec73e","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":920,"wires":[]},{"id":"98fcd18020b27c55","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"0.25","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":410,"y":840,"wires":[["c88cbf20372c9e3c"]]},{"id":"6d214301bfd85a72","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":780,"wires":[["98fcd18020b27c55"]]},{"id":"c88cbf20372c9e3c","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":840,"wires":[["7188bbfe6bf03793"]]},{"id":"0ef206c441955abb","type":"delay","z":"d1395164b4eec73e","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":470,"y":720,"wires":[["6d214301bfd85a72"]]},{"id":"7a2f3e4563934d51","type":"switch","z":"d1395164b4eec73e","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"t1","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":390,"y":780,"wires":[["0ef206c441955abb"],["6d214301bfd85a72"]]},{"id":"39236f77681f6d72","type":"ui_group","name":"PV-Anlage","tab":"abcd1234","order":1,"disp":true,"width":"12","collapse":false,"className":""},{"id":"abcd1234","type":"ui_tab","name":"Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
1 Like

thank you, the first example will do the trick and simplified is good, i don't need the topics for the end result they were just a way to identify the 2 streams, the second example is a bit complex and not sure how it fits this example with the plotting graphs

Both my examples do exactly the same thing.

Here is another which is cleaner that does the same. It uses a JSONata expression to determine the topic.

[{"id":"58ff0f0c7ec6488e","type":"inject","z":"d1395164b4eec73e","name":"both t1 & t3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":500,"wires":[["85140873867b8b36","ed375b562d11dc7e"]]},{"id":"85140873867b8b36","type":"change","z":"d1395164b4eec73e","name":"t1","rules":[{"t":"set","p":"payload","pt":"msg","to":"1200","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"t1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":480,"wires":[["98fcd18020b27c55"]]},{"id":"ed375b562d11dc7e","type":"change","z":"d1395164b4eec73e","name":"t3","rules":[{"t":"set","p":"payload","pt":"msg","to":"1400","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"t3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":520,"wires":[["98fcd18020b27c55"]]},{"id":"98fcd18020b27c55","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"0.25","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":500,"wires":[["9ef50986dd4171be"]]},{"id":"b9563c8afbc545e7","type":"inject","z":"d1395164b4eec73e","name":"t3 only","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"t3","payload":"1350","payloadType":"num","x":170,"y":600,"wires":[["98fcd18020b27c55"]]},{"id":"9ef50986dd4171be","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"\"t1\" in $keys($$.payload) ? \"t1\" : \"t3\"","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"payload[msg.topic]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":500,"wires":[["7188bbfe6bf03793"]]},{"id":"7188bbfe6bf03793","type":"debug","z":"d1395164b4eec73e","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":560,"wires":[]}]

It should handle t1 only messages also, not that you set that as a requirement. Just thought it my help future readers.

thank you very much for your time and effort, it is appreciated

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