Noob - Reading serial - splitting correctly

New to node read so go easy!!

I am trying to read serial connection. When I output to debug, I get the following.

The message payload I am after is the 225 chars starting ts1 etc.. How do I setup the serial connection to filter out just that part and pass it into a format which I can send to anywhere e.g. home assistant :slight_smile:

Welcome to the forum @boydo

Where is that coming from and do you have control over it?

Show us how you have setup the serial port at the moment. The page where you setup the baud rate.

Thanks @Colin

The debug is connected directly to the serial, it's a device for storing water and I have Modbus control over it using HA. But I can't get HA to read both Modbus and serial at the same time (think it's a limitation of parallel serial coms or something but so far, no help here) so I thought I would try and pipe it into Node-Red and pass it back to HA. As I suspected the serial dose work as we are getting some readings into Red but now it's just a question of filtering it and making it readable.

image

The setup for the serial is as follows.

image

I have tried to play with the setup to see if I can filter/split the incoming to make it meaningful, but I am unsure of the process.

Well you seem to have a nice mix of ascii text and binary data there :-)... If you are sure that the text part always contains ts1 and the binary parts never do - then you could use a simple switch node set to msg.payload ... contains .... ts1 - and that would filter out just those lines.

However the data itself is almost JSON but not quite... so to anticipate Colin's next question - if that data could actually be proper JSON then you could then very easily get to each of the variables inside. One way would be to fix the sending end to send real JSON... the other would be to add a function node (after the switch node we added above) - to "fix up" the string - eg something like.

msg.payload = '{' + msg.payload.trim() + '}';
msg.payload = JSON.parse(msg.payload);
return msg.payload;

That would then return a nice object with all the properties that could be accessed like msg.payload.Charging etc

1 Like

It hasn't got the quotes round the key names (so it is not "tss": 0 for example) so I am not sure whether JSON.parse will work.

1 Like

Thanks @Colin & @dceejay

yeah JASON.parse develops issues. What i was trying was:

image

image

So the CSV output puts it into columns, which I can name if I want to, but then they I was trying to pop the text after the colon I am getting a syntax error, my JS is not very strong :slight_smile:

Thanks

Thanks all, all I ended up doing is sending the payload into a HA call for service and HA can format it as required :slight_smile:

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