# Sending payload if other payload is true

**URL:** <https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824>\
**Category:** General\
**Created:** [7 December 2021 11:50 UTC](https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824 "2021-12-07T11:50:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MR76](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MR76](https://discourse.nodered.org/u/MR76)\
**Post date:** [7 December 2021 11:50 UTC](https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824/1 "2021-12-07T11:50:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 December 2021 11:53 UTC](https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824/2 "2021-12-07T11:53:29Z")

</div>

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](https://cookbook.nodered.org/basic/route-on-context) - 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.

---

<div class="post-metadata">

**Author:** ![MR76](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MR76](https://discourse.nodered.org/u/MR76)\
**Post date:** [7 December 2021 14:06 UTC](https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824/3 "2021-12-07T14:06:49Z")

</div>

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

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 December 2021 14:29 UTC](https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824/4 "2021-12-07T14:29:05Z")

</div>

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

 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d92530e25a826a43409f52d7e282e4047e90c66c.jpeg)

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![MR76](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MR76](https://discourse.nodered.org/u/MR76)\
**Post date:** [8 December 2021 12:09 UTC](https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824/5 "2021-12-08T12:09:52Z")

</div>

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

---

<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:** [22 December 2021 12:10 UTC](https://discourse.nodered.org/t/sending-payload-if-other-payload-is-true/54824/6 "2021-12-22T12:10:49Z")

</div>

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