Seriously? What on earth stops you to try it out?
If you really want to learn something, maybe you should do more than write posts and wait for others to tell you every single step?
Also again offtopic...
Seriously? What on earth stops you to try it out?
If you really want to learn something, maybe you should do more than write posts and wait for others to tell you every single step?
Also again offtopic...
I did it.
It was posted as -1
. I did it and nothing changed. I changed it to -2
and it worked.
I am simply asking why it was said as -1
when I need -2
when I am not getting two letters after the value.
I think you are slightly missing me wanting to understand why it happened.
I get that -1 will/should negate the last character. And -2 the last two.
So if I am getting 1007M why is it I need -2?
Not really "mission critical" as I worked it out. But it is anomalous to what was said and what is/should be how it works.
I've thought/looked into this a bit more - and stuck a csv node in the flow
This parses the data better and ensures we only get 6 messages after the split so that picking out msg index 3 "always" get us the free memory.
[{"id":"e9ee1b34.cfb5f8","type":"exec","z":"8376d5c1.273ed8","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":510,"y":780,"wires":[["1c9c4acf.d2cea5"],[],[]]},{"id":"c65210d3.f1ccf","type":"inject","z":"8376d5c1.273ed8","name":"","topic":"","payload":"df -h /dev/sda1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":780,"wires":[["e9ee1b34.cfb5f8"]]},{"id":"601069e4.f909b8","type":"debug","z":"8376d5c1.273ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1270,"y":760,"wires":[]},{"id":"86f87ffc.c62c6","type":"switch","z":"8376d5c1.273ed8","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":950,"y":760,"wires":[["fb9bdc21.c2116"]]},{"id":"fb9bdc21.c2116","type":"function","z":"8376d5c1.273ed8","name":"remove letter","func":"msg.payload = msg.payload.substring(0,msg.payload.length-1);\nreturn msg;","outputs":1,"noerr":0,"x":1090,"y":760,"wires":[["601069e4.f909b8"]]},{"id":"1c9c4acf.d2cea5","type":"csv","z":"8376d5c1.273ed8","name":"","sep":" ","hdrin":false,"hdrout":false,"multi":"one","ret":"\\n","temp":"","skip":"1","x":650,"y":760,"wires":[["76791b57.3272a4"]]},{"id":"76791b57.3272a4","type":"split","z":"8376d5c1.273ed8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":810,"y":760,"wires":[["86f87ffc.c62c6"]]}]
Really, you shouldn't worry about this. Firstly a USB stick is the same device as an SD card, but packaged in a big box with a USB interface. So any reliability issues due to mulitple writes will affect a USB stick in exactly the same way. In addition you are much more likely to cause problems with accidentally knocking the usb stick when it is in operation, or forgetting to unmount it before removing it, unless it is in not in an accessible location.
Provided you use good quality SD cards, and use cards with plenty of spare space on them, then you are unlikely to have issues, unless you are writing gigabytes every day.
Colin,
I somewhat agree with what you say.
However, as any memory read/write shortens the life of any memory unit, writing log files shall be relogated (?) to a USB stick, so: if/when it fails I am not also losing the O/S, etc.
The stick will be "set and forget" for the device, so the issues you mention shouldn't be of concern.
But thanks.
This is a good solution, but returns the units (e.g. G
, Mb
, %
) with each column's value.
Instead of trying to account for all possible unit strings, trimming them off before converting to a number, just parse the string to a number (ignoring trailing non-numeric characters)
In plain Javascript (and therefore, nodejs), you can use the parseFloat()
function like in these examples:
> parseFloat("30G")
30
> parseFloat("4.2Gb")
4.2
> parseFloat("186M")
186
> parseFloat("15%")
15
Note that this will not work if the numeric portion of the string contains commas:
> parseFloat("1,860M")
1
... but it will work with scientific notation:
> parseFloat("1.0E3")
1000
> parseFloat("1.0E-2")
0.01
FYI, another good technique is to use a regex to remove all non-numeric characters from your input string -- then you can just coerce it from a string to a numeric datatype using the +
operator, like so:
> var usePct = "15%"
> usePct.replace(/[^\d.]/, "")
'15'
> +usePct.replace(/[^\d.]/, "")
15
Hmmm I always thought the units may be useful to know when free space goes from 1G to 900M
Thanks both.
Interesting.
Just to share, this is what I finally did/got:
Ok. No I won't. It wasn't deployed and when I tested it, it failed.
I'll look at your suggestion @shrickus
(Via edit)
Ah, I just posted a thread on an error I am seeing/getting/got.
The USB device has moved from /deb/sda1
to /dev/sdb1
and not told me. So my command didn't work.
Fun.
So, this is what I have:
[{"id":"233a6bd4.5f4cfc","type":"exec","z":"1c428643.6f708a","command":"df -h /dev/sdb1 | grep / | tr -s ' ' | cut -d \" \" -f4","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Check size","x":660,"y":3440,"wires":[["169b293a.fe72c7","e7532336.711778"],[],[]]},{"id":"5d9d88f2.e30ee","type":"debug","z":"1c428643.6f708a","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":840,"y":3430,"wires":[]},{"id":"169b293a.fe72c7","type":"function","z":"1c428643.6f708a","name":"Format payload","func":"msg.payload = msg.payload.substring(0,msg.payload.length-2);\nmsg.payload = parseInt(msg.payload);\n//msg = {payload: parseInt(msg.payload), who: \"TelePi\"};\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":3480,"wires":[["5d9d88f2.e30ee","51fb4959.864118"]]},{"id":"51fb4959.864118","type":"switch","z":"1c428643.6f708a","name":"threashold","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"2","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":860,"y":3480,"wires":[["5c720a94.e2e65c","334e0a65.5aa23e"]]},{"id":"5c720a94.e2e65c","type":"debug","z":"1c428643.6f708a","name":"Action needed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":3430,"wires":[]},{"id":"334e0a65.5aa23e","type":"function","z":"1c428643.6f708a","name":"","func":"msg.payload = {\n payload: \"TelePi USB stick memory low\",\n who: \"TelePi\"\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":840,"y":3530,"wires":[["1f4a5729.8cc0a9","670fa7af.bfa3c"]]},{"id":"670fa7af.bfa3c","type":"mqtt out","z":"1c428643.6f708a","name":"","topic":"MESSAGE","qos":"","retain":"","broker":"1ecd3a9e.dc5bfd","x":1040,"y":3530,"wires":[]},{"id":"299c1a5e.60b73e","type":"inject","z":"1c428643.6f708a","name":"test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":680,"y":3530,"wires":[["334e0a65.5aa23e"]]},{"id":"1f4a5729.8cc0a9","type":"debug","z":"1c428643.6f708a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1020,"y":3490,"wires":[]},{"id":"e7532336.711778","type":"debug","z":"1c428643.6f708a","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":840,"y":3380,"wires":[]},{"id":"9144547c.163498","type":"inject","z":"1c428643.6f708a","name":"test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":470,"y":3480,"wires":[["233a6bd4.5f4cfc"]]},{"id":"1ecd3a9e.dc5bfd","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"0","birthPayload":"TelePi comms up","closeTopic":"EOM","closePayload":"TelePi shutting down","willTopic":"EOM","willQos":"0","willPayload":"TelePi Comms Failure"}]
It may be nice to know if it is talking Gig, Meg, or K.
But the output is 1007M. (1 Gig) So as it will warn me when I have less than 1 Meg, I think I have a long way to go before I need to worry.
Assuming you are on a Ubuntu/Debian - you can get the device ID and use that instead - this does not change - regardless of what else you plug into USB ports etc
cd /dev/disk/by-uuid will show them to you
You can see where they are mapped by
cat /etc/fstab
and you can also use this command
blkid
(all as root)
Craig
I have to learn about fstab.
But originally the USB stick was "seen" as:
/dev/sda1
After this morning's event where the RPz(W) had spat the dummy the USB stick was /dev/sdb1
There was no /dev/sda1
Got me.
If the stick is removed and re-inserted it may end up as a different device. To guarantee it always mounts as the same device will have to use udev rules (which is not for the faint hearted). However provided you don't remove and re-insert it during operation it will probably always end up as the same device.
However, as I think I said previously, this really isn't worth all the effort you are putting into it.
That is probably why I haven't gone there yet.
Just use the commands i gave you and access the device by the UUID - these do not change
Craig
Thanks.
This job's priority is bouncing around just now.
Other things happening in the mean time too.
Granted... But I was just trying to address all of the string trimming/converting techniques -- not whether the results would be useful ;*)
So instead of dealing with unit conversions by post processing string output, I would change the executed command to always return bytes.
LOL
Simply running df instead of df -h produces all figures in bytes
I fully agree. But for the fact that "originally" it was /dev/sda1
and after the oops moment it became /dev/sdb1
It was working fine but for the fact I need to check on remaining space on the stick.
To prevent the stick filling up you can use logrotate to delete old log files.