2 inputs merge into 1 output

I have 2 inputs (Int32), one displays "left time in min" and one displays "left time in hours". "General left time" is both, X hours and Y min.
So far i show both separately in DB2.0, but i would like to have them as one output ("HH:XX h")
I already managed to combine both into one output and also build a function that it only works when both inputs send a signal each.
I added a change node to replace the "," between both inputs with ":" but it doesn't work.
Please feel free to also look over the rest of what i built, maybe there is an easier way?

I don't know how to add a flow so i just copy the json.

[
    {
        "id": "5cc0ae9525d73429",
        "type": "inject",
        "z": "5407f6cf6313c6cb",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "15",
        "payloadType": "num",
        "x": 370,
        "y": 1160,
        "wires": [
            [
                "ae4350a4deb5081a"
            ]
        ]
    },
    {
        "id": "3ddb7d3c8b7b7f28",
        "type": "inject",
        "z": "5407f6cf6313c6cb",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "25",
        "payloadType": "num",
        "x": 370,
        "y": 1200,
        "wires": [
            [
                "13ab6ef759f644e2"
            ]
        ]
    },
    {
        "id": "47a99f14ac4c4260",
        "type": "debug",
        "z": "5407f6cf6313c6cb",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1300,
        "y": 1140,
        "wires": []
    },
    {
        "id": "f83bcce0ab8cdc65",
        "type": "template",
        "z": "5407f6cf6313c6cb",
        "name": "Add Suffix",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{ payload }} h",
        "output": "str",
        "x": 1150,
        "y": 1140,
        "wires": [
            [
                "47a99f14ac4c4260"
            ]
        ]
    },
    {
        "id": "7284d2c28c8c819c",
        "type": "function",
        "z": "5407f6cf6313c6cb",
        "name": "msg.part 1 & msg.part2 vorhanden?",
        "func": "if (!context.parts) {\n    context.parts = {};\n}\n\n// Speichere die Teile in Kontextvariablen\ncontext.parts[msg.part] = msg.payload;\n\n// Wenn beide Teile vorhanden sind, sende die kombinierte Nachricht\nif (context.parts[1] !== undefined && context.parts[2] !== undefined) {\n    msg.payload = [context.parts[1], context.parts[2]];\n    \n    // Lösche die gespeicherten Teile, um doppelte Nachrichten zu vermeiden\n    context.parts = {};\n    \n    return msg;\n}",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 1140,
        "wires": [
            [
                "582b2ba42b7e1d89"
            ]
        ]
    },
    {
        "id": "582b2ba42b7e1d89",
        "type": "change",
        "z": "5407f6cf6313c6cb",
        "name": ", zu :",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": ",",
                "fromt": "str",
                "to": ":",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 990,
        "y": 1200,
        "wires": [
            [
                "f83bcce0ab8cdc65"
            ]
        ]
    },
    {
        "id": "ae4350a4deb5081a",
        "type": "function",
        "z": "5407f6cf6313c6cb",
        "name": "function 1",
        "func": "msg.part = 1;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 1160,
        "wires": [
            [
                "7284d2c28c8c819c"
            ]
        ]
    },
    {
        "id": "13ab6ef759f644e2",
        "type": "function",
        "z": "5407f6cf6313c6cb",
        "name": "function 2",
        "func": "msg.part = 2;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 1200,
        "wires": [
            [
                "7284d2c28c8c819c"
            ]
        ]
    }
]

First off, your flow.json is corrupt, due to not posting it correctly.

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

Second, you make the mistake of relying on function nodes to much, they should be a last call.

Third, here is how I may do it

[{"id":"5cc0ae9525d73429","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"15","payloadType":"num","x":170,"y":80,"wires":[["173adf1e6f564969"]]},{"id":"173adf1e6f564969","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"hour","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":80,"wires":[["2a91914ab5072c68"]]},{"id":"2a91914ab5072c68","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":510,"y":100,"wires":[["f83bcce0ab8cdc65"]]},{"id":"24ea235007e5388f","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"min","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":120,"wires":[["2a91914ab5072c68"]]},{"id":"f83bcce0ab8cdc65","type":"template","z":"d1395164b4eec73e","name":"Add Suffix","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.hour}}:{{payload.min}} h","output":"str","x":640,"y":100,"wires":[["47a99f14ac4c4260"]]},{"id":"3ddb7d3c8b7b7f28","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"25","payloadType":"num","x":170,"y":120,"wires":[["24ea235007e5388f"]]},{"id":"47a99f14ac4c4260","type":"debug","z":"d1395164b4eec73e","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":100,"wires":[]}]

A node can only work on one message at a time.
To process data from two messages together you must combine them into a single message.

Research the Join node.

I actually tried it with a join node but it didn't work as with function nodes. Actually im really new to node red so often i do not know the best way.

Wow, it works perfectly. I didn't think of putting the messages together in the "suffix-function". I tried to do it beforehand. Probably cus i have no clue about jacascript (i guess it is?)

Thanks for the flow! Exactly what i needed!

1 Like