Grabbing last character in a string in function node

I'm having a problem grabbing the last character of my msg.port. I want to use it to inject it into the right column of a csv file.

timestamp,port or sensor,pressure_0,pressure_1, temperature.

I did something stupid and cant seem to get the last character of the port grabbed.

[{"id":"f77e447e.b74878","type":"csv","z":"ea882813.8a2a08","name":"","sep":",","hdrin":true,"hdrout":false,"multi":"mult","ret":"\\n","temp":"time,port,pressure_0,pressure_1,temperature","skip":"0","strings":false,"x":770,"y":360,"wires":[["8df2b3b.e81e0d"]]}]

I am writing to a file and cant seem to get the first header row of the csv file written correctly either with the CSV node.

Added the slice function but still not quite right.

[{"id":"ec4bbb9f.3bd4d8","type":"function","z":"ea882813.8a2a08","name":"javascript","func":"var date;\ndate = new Date();\ndate = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2) + ':' + ('00' + date.getMilliseconds()).slice(-2));\npid = (\"pressure_\" + msg.port.slice(-1));\npressure = msg.payload;\npayload = {\"time\":date, \"port\":msg.port, pid:pressure};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":320,"wires":[["f77e447e.b74878"]]},{"id":"6e7a58b8.d2afc8","type":"inject","z":"ea882813.8a2a08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":280,"wires":[["69b01c88.337244"]]},{"id":"69b01c88.337244","type":"change","z":"ea882813.8a2a08","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"14.5","tot":"str"},{"t":"set","p":"port","pt":"msg","to":"/dev/ttyUSB0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":280,"wires":[["ec4bbb9f.3bd4d8"]]},{"id":"8df2b3b.e81e0d","type":"debug","z":"ea882813.8a2a08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":360,"wires":[]},{"id":"55d1ab98.eeef2c","type":"inject","z":"ea882813.8a2a08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":320,"wires":[["e010d9d5.7383c8"]]},{"id":"e010d9d5.7383c8","type":"change","z":"ea882813.8a2a08","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"14.5","tot":"str"},{"t":"set","p":"port","pt":"msg","to":"/dev/ttyUSB1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":320,"wires":[["ec4bbb9f.3bd4d8"]]},{"id":"5dc563c5.07c90c","type":"inject","z":"ea882813.8a2a08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":360,"wires":[["8f6fef77.5a3388"]]},{"id":"8f6fef77.5a3388","type":"change","z":"ea882813.8a2a08","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"75.5","tot":"str"},{"t":"set","p":"port","pt":"msg","to":"MCP9808","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":360,"wires":[["2cc0fee1.4186a2"]]},{"id":"2cc0fee1.4186a2","type":"function","z":"ea882813.8a2a08","name":"javascript","func":"var date;\ndate = new Date();\ndate = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2) + ':' + ('00' + date.getMilliseconds()).slice(-2));\npayload = {\"time\":date, \"port\":\"MCP9808\", \"temperature\":msg.payload};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":360,"wires":[["f77e447e.b74878"]]},{"id":"f77e447e.b74878","type":"csv","z":"ea882813.8a2a08","name":"","sep":",","hdrin":true,"hdrout":false,"multi":"mult","ret":"\\n","temp":"time,port,pressure_0,pressure_1,temperature","skip":"0","strings":false,"x":770,"y":360,"wires":[["8df2b3b.e81e0d"]]}]

I need to create the variables pressure0 and pressure1 so depending in the inbound messages port it is either /dev/ttyUSB0 or /dev/ttyUSB1

var date;
date = new Date();
date = (date.getFullYear() + '-' + ('00' + (date.getMonth()+1)).slice(-2) + '-' + ('00' + date.getDate()).slice(-2) + ' ' + ('00' + date.getHours()).slice(-2) + ':' + ('00' + date.getMinutes()).slice(-2) + ':' + ('00' + date.getSeconds()).slice(-2) + ':' + ('00' + date.getMilliseconds()).slice(-2));
pid = ("pressure_" + msg.port.slice(-1));
pressure = msg.payload;
payload = {"time":date, "port":msg.port, "pressure"(pid):pressure};
msg.payload = payload;
return msg;

The last character of a string can be obtained several ways such as 'this is a string'.slice(-1).

To output to a CSV, I think the msg.payload needs to be an array of objects where each object has a property for each column. Possibly you could get away with an array of array's but I've not tried it.

@hotNipi Any suggestion? I have been working so hard that I messed up.

I can't remember how to pull this together.

"pressure"(pid):pressure}

it should print pressure0:14.5 or pressure1:14.5 so it will go in the right csv column.

ES6 gives some ways to make dynamic properties

msg.payload = {"time":date, "port":msg.port, [`pressure${msg.port.slice(-1)}`]:pressure};
1 Like

You da man....Lets see [{()}]...Got it...Can't we find a way to incorporate |/!@#_-+ It's just not quite convoluted enough. We need that "Over the Top" garnish.

Thanks you... :slightly_smiling_face:

JavaScript is far from the worst culprit in that sense :smile:

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