Help with shell command to get a result in a node

I am wanting to fix a message I am getting.

This is the message:
{"_msgid":"3c5b556c.b18d0a","topic":"","payload":" Used\n 688\n","rc":{"code":0}}

What I want to get is the amount of usage on a USB stick and map it to a gauge.

I found the command:
df --output=used /dev/sda1
(If that can be made better, all the better.)

So I am getting the message above and want to turn it into a simple (integer) 688.

I've tried things but am not understanding how to do it.

Thanks in advance.

You could probably do it with JSONata (but I have no clue how).

I would first look up the df command help see if any command line parameter might simplify the output. If not, then I'd use a regular expression. Something like (\d+).

Here is a working example

Ps, that site also generates the is code for you to use in a function block (left have menu, generate code)

Thanks Steve.

I worked out if I string and delLeftmost to a \n I get 688.
But then I put it through a function node and convert it to an int value.

var x = parseInt(msg.payload);
msg.payload = x;
return msg;

Then send it to the gauge node. Looks good.

But appreciated.

You don't even need to create the x variable
msg.payload = parseInt(msg.payload)
will do the trick nicely

Yeah,

I think that is a layover from other problems parsing variables where sometimes it can't deal with something like msg.payload.

Or have I got that bit wrong too?
(Sorry. Brain drain happening.)

Or in JSONata in a change node after the command output, set as Payload to JSONata expression.
$number($match(payload, /(\d+)/)[0]['groups'][0])
(Typing from phone, pretty sure it’s typo free but untested)

What this does is execute the regex on the payload string to get the number out but still as a string, then parse it to a number and return it.

1 Like

Yet another way - grep the result from the original command via a pipe

df --output=used / | grep -v Used

then just feed into the gauge (as it will cope with simple text.)

[{"id":"425a6c38.bcad84","type":"inject","z":"df0a9f4b.bc75b","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":200,"wires":[["4f93063c.c91df8"]]},{"id":"4f93063c.c91df8","type":"exec","z":"df0a9f4b.bc75b","command":"df --output=used / | grep -v Used","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":370,"y":200,"wires":[["3af9a86f.268f78"],[],[]]},{"id":"3af9a86f.268f78","type":"ui_gauge","z":"df0a9f4b.bc75b","name":"","group":"1fc20d40.3dd9d3","order":0,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"10000000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":630,"y":200,"wires":[]},{"id":"1fc20d40.3dd9d3","type":"ui_group","z":"","name":"Default","tab":"92544f31.05d25","disp":true,"width":"6","collapse":false},{"id":"92544f31.05d25","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Sooo, many ways! Aren't computers fun :smiley_cat:

(How embarrassing.)

I already had most of that flow already. I just hadn't added the GUI.

One way I did it was:
df -h /dev/sda1 | grep / | tr -s ' ' | cut -d " " -f4
But that shows me the remaining spavce - I think. I didn't know the --output=used bit then.

And the way I did it last night was:
df --output=used /dev/sda1
(but then needed to do work on the output with other nodes.

Sorry guys.

It is called C.R.A.F.T. disease.

I'll let you work that out.

then just feed into the gauge (as it will cope with simple text.)

@dceejay Yeah, oops. Silly me.

Oh, not to sound petty.

Ok, so that part works.

It is/seems silly having that and not having a threshold above which it alerts me.
So I send the output to a switch and switch if the value > (what ever)

So I am guessing I will need a function node in there with msg.payload = parseInt(msg.payload) in it.

I remember a very, very old ad from the late 70's...it was a cartoon and it was an ad for,,,yes,,,IBM

Two men standing looking at a construction site
- if it wasn't for that excavator, 100 more men with shovels would have a decent job
- yeah, and if it wasn't for those shovels, it could be 1000 men with teaspoons

3 Likes

Just a tip; there's a set of very handy system nodes, which includes a disk-space node, as well as CPU usage, free RAM, and system uptime. They use the Node.js OS library and are compatible with both Linux and Windows. I've been using these on a Pi for the last couple of years with zero issues.
Screenshot_2019-09-16_19-59-36

4 Likes

Looks nice.

Thanks.

Shall install and use.

1 Like