Hello
I working on power and some other sensor monitoring application
My device is sending value via MQTT with a timestamp,
something like this -- 12:38:18:128.79 where {12:38:18} is a timestamp and {128.79} is a value which needs to show in a simple chart
Now can someone please help me in how to separate those to value and shown in the chart
I am just beginner in Nod red and with basic knowledge of programming.
Ans in little detail appreciated
Thanks in advance
Welcome! Your device is sending not so “well” formatted messages but never mind.
You can use a function node using the string.split() function and take your string appart:
var parts=msg.payload.split(“:”);
msg.timeString=parts[0]+”:”+parts[1]+”:”+parts [2];
msg.payload=Number(parts[3]);
msg.timestamp= new Date();
msg.timestamp=msg.timestamp.setHours(Number(parts[0]), Number(parts[1]), Number(parts[2])).valueOf();
return msg;
Code not tested just typed in on my phone but give it a try.
Thanks a lot, brother !!
For as of now i have solved issue using substring method some thing like this
var value = msg.payload;
value = value.substring(9,14);
return {payload:value};
and your code is very used full to use the timestamp from payload itself!
But be careful if your timestamp comes without leading zeros like “9:10:11”
var parts=msg.payload.split(“:”);
msg.payload=Number(parts[3]);
return msg;
Or even more safe
var parts=msg.payload.split(“:”);
msg.payload=Number(parts[parts.length-1]);
return msg;
Then even “5:40:123.45” will work.