Values from different sources to a display

I'm trying to display the values coming from two ds18b20 probes on an lcd1602. The node I'm using for the display is the node-red-contrib-lcd20x4-i2c, which looks for a message in this format:

msg.payload = {
    msgs: [
        {
            msg: "string",
            pos: number,
            center: "boolean"
        },
        {
            msg: "string",
            pos: number,
            center: "boolean"
        }
    ]
};

I've asked about something similar to this before. Simply getting a value from something displayed, and it was accomplished running it through the template node, but I can't seem to figure out how to get two sources at once to display.
I've changed the message name on one to something like msg.payloadSecond, keeping the first one msg.payload. It will display each independently on their respective line just fine, but will error out if both sources are connected at once.

What is your approach right now to combine them? Can you post your flow?
Best regards Johannes

Assuming I understand your problem correctly, then have a look at this cookbook post on how to combine multiple messages into one message using the Join node.

Here's the test I have set up for it now:

[{"id":"6131dde0.4e2fc4","type":"debug","z":"9e4fee93.cae7a","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":850,"y":480,"wires":[]},{"id":"7d4a80c9.fee9b","type":"ds18b20","z":"9e4fee93.cae7a","name":"","sensorid":"28-01192142b5ae","timer":".2","x":330,"y":320,"wires":[["524b7c8b.82f7d4","edc23dd3.b9d17"]]},{"id":"57e43d38.d75634","type":"ds18b20","z":"9e4fee93.cae7a","name":"","sensorid":"28-0119143a6dae","timer":".2","x":330,"y":380,"wires":[["edc23dd3.b9d17","6131dde0.4e2fc4"]]},{"id":"50d3290e.873a58","type":"LCD20x4-I2C","z":"9e4fee93.cae7a","name":"","speed":"3","size":"20x4","address":"0x3F","x":930,"y":340,"wires":[]},{"id":"b02c210.bff1ae","type":"template","z":"9e4fee93.cae7a","name":"","field":"payload","fieldType":"msg","format":"javascript","syntax":"mustache","template":"{\n    \"msgs\": [\n        {\n            \"msg\": \"{{payload[0]}}\"\n        },\n        {\n            \"msg\": \"{{payload[1]}}\"\n        }\n    ]\n}","output":"json","x":720,"y":340,"wires":[["50d3290e.873a58"]]},{"id":"524b7c8b.82f7d4","type":"debug","z":"9e4fee93.cae7a","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":140,"wires":[]},{"id":"edc23dd3.b9d17","type":"join","z":"9e4fee93.cae7a","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":550,"y":340,"wires":[["109cfcb3.bd76d3","b02c210.bff1ae"]]},{"id":"109cfcb3.bd76d3","type":"debug","z":"9e4fee93.cae7a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":720,"y":260,"wires":[]}]

I have the join node set up to output an array. Then the template is supposed to grab each value in the array and put in the appropriate spot. I am able to get correct display if there's just one value going in using {{payload}}, but it doesn't seem to like trying to call out of the array using {{payload[0]}} or {{playload[1]}}. I know basically nothing about coding, but the debug node is able to pull out each value separately from the joiner if I have it look for msg.payload[0] or [1], so I thought that would work.
Thank you for the replies and help.

I got it. Needs to be {{payload.0}} and {{playload.1}} in the template node.
Looks like:

{
    "msgs": [
        {
            "msg": "T1 Temp is {{payload.0}}"
        },
        {
            "msg": "T2 Temp is {{payload.1}}"
        }
    ]
}

Outputting as Parsed JSON. Thank you Colin for the Join node idea and instructions. I was looking at that node earlier, but again, knowing no JavaScript, wasn't sure what I could do with it. But I guess like some things, just throw stuff at the wall until something sticks.

As I'm typing this, I noticed the values are alternating position. Looks like I need to stage the order the values come in to ensure that the first sensor is the first to report to keep it first in the list. Thanks again. Love this forum.

Use key/value mode in the Join rather than array (as in the cookbook), give the values different topics on the way to the join node and then you can access them by name, trying to do it with an array and getting the order right will be fraught with problems.

I found a node for the sensors that work off an inject node, so I have better control over what order they come in at now. Seems to work great so far. But just for the sake of learning, what would that look like in the template node, in that object there that the lcd needs to receive? Let’s say the topics were probeOne and probeTwo. Thank you for the help.

It won't keep working though, have you coped with the possibilty that one of them fails to return a value for example. Do it using key/value.

I don't know what template you are referring to. Import the example from the cookbook and play with it to see what it does. The net result will be that you will end up with a payload containing an object something like

{
  probeOne: 21,
  probeTwo: 12
}

then wherever you want to use those you can use, for example, msg.payload.probeTwo.

I’ll try it that way, makes sense if one fails. It’s just a home project for fun so coping with failure won’t be that difficult.
Template is a node. Is there another way to create that piece of code that I posted on the 4th post?

You just need to pick up the appropriate property of the payload
{
    "msgs": [
        {
            "msg": "T1 Temp is {{payload.probeOne}}"
        },
        {
            "msg": "T2 Temp is {{payload.probeTwo}}"
        }
    ]
}

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