Sending payload if other payload is true

Hi,
I'm new to Node-RED and don't a lot of experience with Java. I have a payload containing a number that I only want to send if another payload I have has the number 1. If it is 0 I want the flow to stop. What is the best way of doing that?

best regards

Hi @MR76 , welcome to the forum.

One small, but important clarification - we use JavaScript not Java. They sound similar but are very different.

This recipe in the cookbook might help - Route or block a message based on a context value : Node-RED - it shows how you can route a message based on the current value of something held in context. In your case, you'd store your 0 or 1 in a context variable and then use the Switch node to test the current value to see if the message to pass through or not.

1 Like

Hi thanks for your answer. I can't get it to work. Can you take a look?

[{"id":"62302add.4be644","type":"ui_text","z":"3e53ca35.c83ac6","group":"8e556f63.5ad14","order":1,"width":"0","height":"0","name":"","label":"T1:","format":"{{msg.payload}}","layout":"row-center","className":"","x":450,"y":120,"wires":[]},{"id":"bd9fb294.d2bb9","type":"timeswitch","z":"3e53ca35.c83ac6","name":"T1","mytopic":"","lat":"","lon":"","starttime":"360","endtime":"840","timezone":"Europe/Berlin","duskoff":"0","dawnoff":"0","sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"x":90,"y":60,"wires":[["60f14d17.b85aa4"]]},{"id":"b24710ae.12744","type":"s7 in","z":"3e53ca35.c83ac6","endpoint":"17b58fa0.732d4","mode":"single","variable":"dword","diff":false,"name":"","x":90,"y":120,"wires":[["6ac3db71.811b34"]]},{"id":"60f14d17.b85aa4","type":"change","z":"3e53ca35.c83ac6","name":"","rules":[{"t":"set","p":"state","pt":"flow","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":60,"wires":[[]]},{"id":"6ac3db71.811b34","type":"switch","z":"3e53ca35.c83ac6","name":"","property":"state","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":270,"y":120,"wires":[["62302add.4be644"]]},{"id":"8e556f63.5ad14","type":"ui_group","z":"","name":"Schichten","tab":"171d8214.95a08e","order":3,"disp":false,"width":"6","collapse":false,"className":""},{"id":"17b58fa0.732d4","type":"s7 endpoint","z":"","transport":"iso-on-tcp","address":"192.168.178.10","port":"102","rack":"0","slot":"2","localtsaphi":"01","localtsaplo":"00","remotetsaphi":"01","remotetsaplo":"00","connmode":"rack-slot","adapter":"","busaddr":"2","cycletime":"300","timeout":"2000","name":"","vartable":[{"addr":"DB2,X0.0","name":"test"},{"addr":"DB2,DI6","name":"dword"}]},{"id":"171d8214.95a08e","type":"ui_tab","z":"","name":"MSK Produktzähler","icon":"dashboard","disabled":false,"hidden":false}]

Your flow works for me, using inject nodes rather than the timeswitch node (which I don't have).

An alternative approach is to use a gate node - node-red-contrib-simple-gate

[
    {
        "id": "07a0720de1e26cc3",
        "type": "inject",
        "z": "ca2412f8e6714386",
        "name": "Message",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 420,
        "y": 260,
        "wires": [
            [
                "ab9bbd97f0c91355"
            ]
        ]
    },
    {
        "id": "ea6b90edc8309278",
        "type": "debug",
        "z": "ca2412f8e6714386",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 810,
        "y": 200,
        "wires": []
    },
    {
        "id": "ab9bbd97f0c91355",
        "type": "gate",
        "z": "ca2412f8e6714386",
        "name": "",
        "controlTopic": "control",
        "defaultState": "open",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 640,
        "y": 200,
        "wires": [
            [
                "ea6b90edc8309278"
            ]
        ]
    },
    {
        "id": "ca234be39844564c",
        "type": "inject",
        "z": "ca2412f8e6714386",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "control",
        "payload": "open",
        "payloadType": "str",
        "x": 430,
        "y": 140,
        "wires": [
            [
                "ab9bbd97f0c91355"
            ]
        ]
    },
    {
        "id": "504417a1b20e2039",
        "type": "inject",
        "z": "ca2412f8e6714386",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "control",
        "payload": "close",
        "payloadType": "str",
        "x": 430,
        "y": 180,
        "wires": [
            [
                "ab9bbd97f0c91355"
            ]
        ]
    }
]
1 Like

I solved my problem by using the gate function. Thanks for your help guys.

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