Send Msg From Second Input If First Input Is True

Dear All,

I'm trying to send a msg which comes from the second input to a function node if the first input to the node is "true" from a Boolean gate ultimate node.

I also need to make the function node not send a message until both inputs have arrived and send only one message per 24h (though I could do this with a delay node after the function node I think).

Sorry for this pretty basic question but I've trawled the web and can't find a solution to this (seemingly!) Very simple operation.

My test flow is attached below:

[
    {
        "id": "a0da04ce2ca2ea94",
        "type": "tab",
        "label": "Test",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "681f07f50a06f37d",
        "type": "function",
        "z": "a0da04ce2ca2ea94",
        "name": "If This Is The Min Block Time",
        "func": "if (msg.payload[0] = msg.payload[1]) {\n    msg.payload = \"true\";\n}\nelse {\n    msg.payload =\"false\";\n}\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 140,
        "wires": [
            [
                "88fa2461164a749b",
                "343017c6e242ede5"
            ]
        ]
    },
    {
        "id": "343017c6e242ede5",
        "type": "debug",
        "z": "a0da04ce2ca2ea94",
        "name": "Is It The Time?",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 100,
        "wires": []
    },
    {
        "id": "88fa2461164a749b",
        "type": "FilterUltimate",
        "z": "a0da04ce2ca2ea94",
        "name": "If True",
        "payloadPropName": "payload",
        "translatorConfig": "",
        "x": 650,
        "y": 140,
        "wires": [
            [
                "3044b872b070af0a"
            ],
            []
        ]
    },
    {
        "id": "666c10679f5ef745",
        "type": "debug",
        "z": "a0da04ce2ca2ea94",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1120,
        "y": 160,
        "wires": []
    },
    {
        "id": "3044b872b070af0a",
        "type": "function",
        "z": "a0da04ce2ca2ea94",
        "name": "function 3",
        "func": "if (msg.payload[0] == \"true\") {\n   msg.payload = msg.payload[1]\n}\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 920,
        "y": 160,
        "wires": [
            [
                "666c10679f5ef745"
            ]
        ]
    },
    {
        "id": "ffd1fdbbf83b213b",
        "type": "function",
        "z": "a0da04ce2ca2ea94",
        "name": "Add Msg To Inverter Payload",
        "func": "msg.payload = {\n    'value': msg.payload,\n    'fc': 6,\n    'unitid': 1,\n    'address': 43141,\n    'quantity': 1\n    }\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 200,
        "wires": [
            [
                "3044b872b070af0a"
            ]
        ]
    },
    {
        "id": "a88a39fcbfd50be6",
        "type": "inject",
        "z": "a0da04ce2ca2ea94",
        "name": "Current Time",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "time",
        "payload": "03:00",
        "payloadType": "str",
        "x": 130,
        "y": 100,
        "wires": [
            [
                "681f07f50a06f37d"
            ]
        ]
    },
    {
        "id": "bdbaba788bdecab3",
        "type": "inject",
        "z": "a0da04ce2ca2ea94",
        "name": "Cheapest Block",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "03:00",
        "payloadType": "str",
        "x": 140,
        "y": 140,
        "wires": [
            [
                "681f07f50a06f37d"
            ]
        ]
    },
    {
        "id": "6d85670bf6c0fb0d",
        "type": "inject",
        "z": "a0da04ce2ca2ea94",
        "name": "Cheapest Time (Hours)",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "cheapest hours",
        "payload": "03",
        "payloadType": "str",
        "x": 160,
        "y": 200,
        "wires": [
            [
                "ffd1fdbbf83b213b"
            ]
        ]
    }
]

I can't get this to output just the payload from the 'Add Msg To Inverter Payload' node to debug 4 ONLY when the boolean gate sends 'true'. I can then limit it to one message per 24h with a delay node but for the time being I feel my skills are wholly lacking on javascript coding to make the 'function 3' node perform this task.

Very grateful for any help you can offer,
James

@Sonsofhigsolar Welcome to the forum,

When ever I ask for help on an issue, the first thing I do is create a small flow demonstrating the issue and attach it to my post (note 1 below). If any of the input comes from some device, I capture that data(note 2 below) and put it in a inject node to simulate the device and I test my flow to make sure it reproduces my issue. That way someone else can run the flow you provide to see what you are actually doing.

I also provide the version of NR and node.js I'm running (see the startup log), the device I'm running on. This should give people enough to help you with your problem.


Note 1

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json


Note 2

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Dear Zenofmud,

Thank you for that. I've edited the post with my test flow and an image of the flow itself. Please let me know if you need anything else!

Cheers,
James

First of all, msgs are single threaded so your function 3 will only get one msg at a time. If you need information from two msgs, you will need to join them. Take a look at the join node.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Do you mean you want to block the messages from Add Msg To Inverter Payload when the output from the boolean node is false?

If so then have a look at node-red-contrib-simple-gate which can be used to block messages based on the value of a gate signal.

Dear Colin,

Yes, that's pretty much what I want. I can't get the gate to open with two inputs, one being the function node sending a payload of "open" and then the function 'add msg to inverter payload" sending the message that I actually want to be passed by the gate node if the gate is opened. Am I using it correctly?

Zenofmud, I've also tried using a join node before the functions to no avail. Do I need to set the values to context? How do I then label the inputs in the function node as they're coming in as a joined message payload?

Sorry, I have watched the videos and I know a little more but I'm struggling to apply it! :upside_down_face:

i did not understand the requirement correctly, try the attached flow and see if it helps.
some tweaking may be required to adjust to your flow.

you need to get node-red-contrib-simple-gate contrib node

[{"id":"085ba3c4884da265","type":"change","z":"a0da04ce2ca2ea94","name":"Close Gate","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":446.0000114440918,"y":385.00000381469727,"wires":[["7388cfc7dc90fc6b"]]},{"id":"c2a60d44a6677ea9","type":"inject","z":"a0da04ce2ca2ea94","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":246.0000114440918,"y":385.00000381469727,"wires":[["085ba3c4884da265"]]},{"id":"88fa2461164a749b","type":"FilterUltimate","z":"a0da04ce2ca2ea94","name":"If True","payloadPropName":"payload","x":646.0000114440918,"y":135.00000381469727,"wires":[["18858467913fb120"],["085ba3c4884da265"]]},{"id":"7388cfc7dc90fc6b","type":"gate","z":"a0da04ce2ca2ea94","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memoryOnly","x":766.0000114440918,"y":295.00000381469727,"wires":[["3044b872b070af0a"]]},{"id":"681f07f50a06f37d","type":"function","z":"a0da04ce2ca2ea94","name":"If This Is The Min Block Time","func":"if (msg.payload[0] = msg.payload[1]) {\n    msg.payload = \"true\";\n}\nelse {\n    msg.payload =\"false\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":396.0000114440918,"y":135.00000381469727,"wires":[["88fa2461164a749b","343017c6e242ede5"]]},{"id":"18858467913fb120","type":"change","z":"a0da04ce2ca2ea94","name":"Open Gate","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":446.0000114440918,"y":325.00000381469727,"wires":[["7388cfc7dc90fc6b"]]},{"id":"ffd1fdbbf83b213b","type":"function","z":"a0da04ce2ca2ea94","name":"Add Msg To Inverter Payload","func":"msg.payload = {\n    'value': msg.payload,\n    'fc': 6,\n    'unitid': 1,\n    'address': 43141,\n    'quantity': 1\n    }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":476.0000114440918,"y":195.00000381469727,"wires":[["7388cfc7dc90fc6b"]]},{"id":"3044b872b070af0a","type":"function","z":"a0da04ce2ca2ea94","name":"function 3","func":"if (msg.payload[0] == \"true\") {\n   msg.payload = msg.payload[1]\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":956.0000114440918,"y":295.00000381469727,"wires":[["666c10679f5ef745"]]},{"id":"a88a39fcbfd50be6","type":"inject","z":"a0da04ce2ca2ea94","name":"Current Time","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"time","payload":"03:00","payloadType":"str","x":126.0000114440918,"y":95.00000381469727,"wires":[["681f07f50a06f37d"]]},{"id":"bdbaba788bdecab3","type":"inject","z":"a0da04ce2ca2ea94","name":"Cheapest Block","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"03:00","payloadType":"str","x":136.0000114440918,"y":135.00000381469727,"wires":[["681f07f50a06f37d"]]},{"id":"343017c6e242ede5","type":"debug","z":"a0da04ce2ca2ea94","name":"Is It The Time?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":676.0000114440918,"y":95.00000381469727,"wires":[]},{"id":"1cdd3af356494e5c","type":"inject","z":"a0da04ce2ca2ea94","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":246.0000114440918,"y":325.00000381469727,"wires":[["18858467913fb120"]]},{"id":"6d85670bf6c0fb0d","type":"inject","z":"a0da04ce2ca2ea94","name":"Cheapest Time (Hours)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cheapest hours","payload":"03","payloadType":"str","x":156.0000114440918,"y":195.00000381469727,"wires":[["ffd1fdbbf83b213b"]]},{"id":"666c10679f5ef745","type":"debug","z":"a0da04ce2ca2ea94","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1136.0000114440918,"y":295.00000381469727,"wires":[]}]

Thank you Sman,

Unfortunately that doesn't result in the payload I want progressing to the debug 4 node at the end.

In its most basic function the only thing I want to achieve is:

A msg payload from one node being allowed to be sent through the flow if another node sends a payload of 'true'.

I am just so shocked that this is (as far as I can see) a very simple goal that must be a common problem for coding in this type of flow based medium but I just cant find out how to do it. What I think I need is a switch that either allows that message to pass through based on the input of another node.....

Please help if you can :smiley:

Dear All,

Thank you very much, I've cracked it! As you suggested, the gate node was the trick, just needed a bit of tweaking!

Cheers! Finished flow below.
James

[
    {
        "id": "b00bd43cb27f4007",
        "type": "link in",
        "z": "d04245aa0e23ed8c",
        "name": "Receive Min Block Time",
        "links": [
            "d4ecdbe4137b638d"
        ],
        "x": 135,
        "y": 2220,
        "wires": [
            [
                "56c525c76fba3c6a"
            ]
        ]
    },
    {
        "id": "afb0a13ecd010fcf",
        "type": "link in",
        "z": "d04245aa0e23ed8c",
        "name": "Receive Current Time",
        "links": [
            "da10df31385af2cc"
        ],
        "x": 135,
        "y": 2180,
        "wires": [
            [
                "56c525c76fba3c6a"
            ]
        ]
    },
    {
        "id": "56c525c76fba3c6a",
        "type": "function",
        "z": "d04245aa0e23ed8c",
        "name": "If This Is The Min Block Time",
        "func": "if (msg.payload[0] == msg.payload[1]) {\n    msg.payload = \"true\";\n}\nelse {\n    msg.payload =\"false\";\n}\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 320,
        "y": 2200,
        "wires": [
            [
                "85dfc0891523608e",
                "05e570f4d85aec57"
            ]
        ]
    },
    {
        "id": "85dfc0891523608e",
        "type": "debug",
        "z": "d04245aa0e23ed8c",
        "name": "Is It The Time?",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 2120,
        "wires": []
    },
    {
        "id": "05e570f4d85aec57",
        "type": "FilterUltimate",
        "z": "d04245aa0e23ed8c",
        "name": "If True",
        "payloadPropName": "payload",
        "translatorConfig": "",
        "x": 550,
        "y": 2200,
        "wires": [
            [
                "ae34d1499fc04f4c"
            ],
            [
                "2c2c276b26685b5c"
            ]
        ]
    },
    {
        "id": "c80a4475f870045b",
        "type": "function",
        "z": "d04245aa0e23ed8c",
        "name": "Write Data on Inverter",
        "func": "msg.payload = {\n    'value': msg.payload,\n    'fc': 6,\n    'unitid': 1,\n    'address': 43141,\n    'quantity': 1\n    }\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 2260,
        "wires": [
            [
                "33eb15957fcb718a"
            ]
        ]
    },
    {
        "id": "33eb15957fcb718a",
        "type": "gate",
        "z": "d04245aa0e23ed8c",
        "name": "",
        "controlTopic": "control",
        "defaultState": "closed",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 1050,
        "y": 2240,
        "wires": [
            []
        ]
    },
    {
        "id": "2c2c276b26685b5c",
        "type": "change",
        "z": "d04245aa0e23ed8c",
        "name": "Open Gate",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "control",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "open",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 2180,
        "wires": [
            [
                "33eb15957fcb718a"
            ]
        ]
    },
    {
        "id": "ae34d1499fc04f4c",
        "type": "change",
        "z": "d04245aa0e23ed8c",
        "name": "Close Gate",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "control",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "close",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 2220,
        "wires": [
            [
                "33eb15957fcb718a"
            ]
        ]
    },
    {
        "id": "448e351284ba6e82",
        "type": "inject",
        "z": "d04245aa0e23ed8c",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "2",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 170,
        "y": 2260,
        "wires": [
            [
                "b88a5d19f3136b16"
            ]
        ]
    },
    {
        "id": "b88a5d19f3136b16",
        "type": "change",
        "z": "d04245aa0e23ed8c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload",
                "tot": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 340,
        "y": 2260,
        "wires": [
            [
                "c80a4475f870045b"
            ]
        ]
    },
    {
        "id": "2048e3e9c1df1a8d",
        "type": "delay",
        "z": "d04245aa0e23ed8c",
        "name": "",
        "pauseType": "rate",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "10",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 1240,
        "y": 2240,
        "wires": [
            [
                "7628f42b3b8c8df1"
            ]
        ]
    },
    {
        "id": "7628f42b3b8c8df1",
        "type": "debug",
        "z": "d04245aa0e23ed8c",
        "name": "debug 3",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1400,
        "y": 2240,
        "wires": []
    }
]
2 Likes

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