Help with the `node-red-contrib-os` `drives` node

I have a USB stick in a RasPi.

I want to keep a eye on how full it is. I do that with the drive node.
But.... (And isn't there always a but?)
The output is an array.

I noticed that I wasn't getting a result for the USB stick memory usage recently.
The position in the array for the USB stick has changed from 9 to 8.

Basic code:

[{"id":"7ff716d6.e21418","type":"inject","z":"aa48e352.9ad2f8","name":"Start / test","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":680,"y":2690,"wires":[["6577d12.52626b"]]},{"id":"6577d12.52626b","type":"Drives","z":"aa48e352.9ad2f8","name":"","x":630,"y":2770,"wires":[["782006fd.3e7dd","993a88f2.a45528"]]},{"id":"782006fd.3e7dd","type":"function","z":"aa48e352.9ad2f8","name":"","func":"var used = msg.payload[8].capacity;\nmsg.payload = used * 100;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":2770,"wires":[["ef0a36fd.aa9b7","5b576c7e.3b7f74","757c403.9cec9c","454f45f.552593c"]]},{"id":"5b576c7e.3b7f74","type":"debug","z":"aa48e352.9ad2f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1040,"y":2690,"wires":[]}]

Without making it too complicated how can I get it to track the USB and not having to change the code in the function node?

Because what I have to do is look at the message out of the node and open each one to see which is the USB drive and change the function node to that number.

Use msg.payload.forEach() to step through the array till you find the one with the right mount point.

Or more concisely use Array.reduce(), whichever you are happiest using.

1 Like

Seems I have two commands I need to study to see how they work.

This would/should work too - yeah?

I tried it and it seems to do the trick.

[{"id":"7ff716d6.e21418","type":"inject","z":"aa48e352.9ad2f8","name":"Start / test","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":680,"y":2690,"wires":[["6577d12.52626b"]]},{"id":"6577d12.52626b","type":"Drives","z":"aa48e352.9ad2f8","name":"","x":630,"y":2770,"wires":[["782006fd.3e7dd","993a88f2.a45528","5a7ab15f.f4439"]]},{"id":"5a7ab15f.f4439","type":"split","z":"aa48e352.9ad2f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":850,"y":2610,"wires":[["ee281b31.d242e8","1b529ead.2ceed1"]]},{"id":"1b529ead.2ceed1","type":"switch","z":"aa48e352.9ad2f8","name":"","property":"payload.filesystem","propertyType":"msg","rules":[{"t":"eq","v":"/dev/sda1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":990,"y":2610,"wires":[["10aa292a.d13ef7"]]},{"id":"10aa292a.d13ef7","type":"debug","z":"aa48e352.9ad2f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":2610,"wires":[]}]

Yes, that is a good node-red way of doing it. Much better than my ideas :slight_smile:

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