Splitting message and reseting msg.parts.count?

Hi to All, I am new here (although I already found lot of answers here in the past). I am completely lost and after many days of searching through this forum and the internet.

I have arduino with three temp sensor on my heat storage tank and it is relaying the temp info over the serial to node-red machine and it does so each 10 seconds. In Node-Red I have Serial In node and from it I receives data as one messages with three strings separated by /n (enter). I am using split node to separate this messages and then I use switch node to route each number to separate output:

image

It works as expected. But there is problem, that this switch node routes only the first three numbers and after 10 seconds when new message arrives it is routing to higher output counts and not the first three.

How can I achieve that each 10 seconds the switch node would reset and send the numbers again to output 1 - 3? I understood that there is something called msg.parts.count and that it is what makes switch nodes to decide which output to use. Is there any way how to reset the msg.parts.count after each three numbers?

Hope I am describing this well. If it is confusing, let me know and I will try to reformulate:-)

Thank you in advance for any help!

Hi and welcome.
Can you show the switch node settings?

[edit]
Switching on property msg. parts.index and rules == 0 number, and 1 & 2 should work.

If you have control of the arduino code then I suggest modifying that to send the data as JSON which would then be much easier to handle in node red. Something like a string containing
[{"sensor": "temp_top", "temperature": 60.25},{"sensor": "temp_middle", "temperature": 66.25},{"sensor": "temp_bottom", "temperature": 25.00}]
Then in node-red you can feed that into a JSON node which will convert it to a javascript object.

Hi E1cid,

This is my switch node settings:
image

I am not sure how to do the rules as you mentioned. I am pretty new to node-red :slight_smile:

Hi Colin,

thanks for your reply. I am not sure how to adjust wiring code to send data as JSON.

This is my code:

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);  

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;

void setup(void)
{
  sensors.begin();  // Start up the library
  Serial.begin(9600);
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
}

void loop(void)
{ 
  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures(); 
  
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    tempC = sensors.getTempCByIndex(i);
    Serial.print(tempC);
    Serial.print('\n');
  }
  
  Serial.println("");
  delay(10000);
}

That is correct settings, so you may have varying output from your serial node, can you capture the output from the serial in node and check it is consistent, and display here any variations.

This is probably not the best forum to ask for help with arduino code, but instead of printing tempC you need to print something like the text {"deviceNo": i, "temperature": tempC}\n

Hi E1cid,

thanks for checking my switch node settings.

If I connect serial in node directly to debug node the messages appear like this:

It is always string of five characters followed by "enter". There is always fourth message with only the "enter" character (wiring code in Arduino will definitely need some tweaking).

My ultimate goal is to take each temp reading and direct it its separate flow ending in MQTT nodes (three of them)...

Thanks Colin,

I will look into it and try to adjust the arduino code:-)

Your Arduino code is sending the strings in four separate messages. Best thing to do is fix the Arduino code as @colin suggested.

You could fix it in Node-red you would first join the 4 incoming messages using a timeout setting of say 1 second. You could then split the joined string. This would give you correct message parts.
e.g.

[{"id":"54c6f04fe78d91ad","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":3960,"wires":[["8a718d8401ddf425"]]},{"id":"8a718d8401ddf425","type":"template","z":"da8a6ef0b3c9a5c8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1\n 2\n 3\n \n","output":"str","x":340,"y":3960,"wires":[["e4665a53689dd0d7"]]},{"id":"e4665a53689dd0d7","type":"split","z":"da8a6ef0b3c9a5c8","name":"simulate incoming data","splt":" ","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":530,"y":3960,"wires":[["43fffb16a6a4b95d"]]},{"id":"43fffb16a6a4b95d","type":"join","z":"da8a6ef0b3c9a5c8","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"","joinerType":"str","accumulate":false,"timeout":"1","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":230,"y":4100,"wires":[["80101fc614b1eaf8","7f53dcee207d6397"]]},{"id":"80101fc614b1eaf8","type":"split","z":"da8a6ef0b3c9a5c8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":430,"y":4100,"wires":[["879ade3ab99a8ea8"]]},{"id":"7f53dcee207d6397","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 230","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":4000,"wires":[]},{"id":"879ade3ab99a8ea8","type":"switch","z":"da8a6ef0b3c9a5c8","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"}],"checkall":"false","repair":false,"outputs":3,"x":650,"y":4100,"wires":[["82ecd20f34c36675"],["533f818aa46c97a2"],["0aaf55c41f00fa6b"]]},{"id":"82ecd20f34c36675","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 227","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":4180,"wires":[]},{"id":"533f818aa46c97a2","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 228","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":4220,"wires":[]},{"id":"0aaf55c41f00fa6b","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 229","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":4260,"wires":[]}]

[editr] fixed join node error

It works. I imported your nodes and I was able to manage to obtain desired three flows.

Thank you a million times :slight_smile: You saved me!

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