# New user with newbie interpetation problems

**URL:** <https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862>\
**Category:** General\
**Created:** [27 January 2021 11:03 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862 "2021-01-27T11:03:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![geotete](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@geotete](https://discourse.nodered.org/u/geotete)\
**Post date:** [27 January 2021 11:03 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/1 "2021-01-27T11:03:14Z")

</div>

Hi everybody,

I'm in this forum for more than a month getting information and solving my problems, but now I stuck with a string problem.  
I'm using version 1.2.6 of node red in a raspberry Pi 3B+.  
I found a flow for my APC UPS, and I'm getting the data from the apcupsd, but the info comes in a big string of info, and I can't get what I need.  
This is the string:

```auto
APC : 001,043,1001DATE : 2021-01-27 09:52:47 -0100 HOSTNAME : DreamVERSION : 3.14.14 (31 May 2016) debianUPSNAME : APCCABLE : USB CableDRIVER : USB UPS DriverUPSMODE : Stand AloneSTARTTIME: 2021-01-24 11:42:55 -0100 MODEL : Smart-UPS 1000 STATUS : ONLINE LINEV : 244.8 VoltsLOADPCT : 0.0 PercentBCHARGE : 100.0 PercentTIMELEFT : 291.0 MinutesMBATTCHG : 5 PercentMINTIMEL : 3 MinutesMAXTIME : 0 SecondsOUTPUTV : 244.8 VoltsSENSE : HighDWAKE : -1 SecondsDSHUTD : 180 SecondsLOTRANS : 208.0 VoltsHITRANS : 253.0 VoltsRETPCT : 0.0 PercentITEMP : 28.3 CALARMDEL : 30 SecondsBATTV : 27.5 VoltsLINEFREQ : 50.0 HzLASTXFER : No transfers since turnonNUMXFERS : 0TONBATT : 0 SecondsCUMONBATT: 0 SecondsXOFFBATT : N/ASELFTEST : NOSTESTI : 14 daysSTATFLAG : 0x05000008MANDATE : 2004-10-18SERIALNO : AS0443230286BATTDATE : 2004-10-18NOMOUTV : 220 VoltsNOMBATTV : 24.0 VoltsFIRMWARE : 600.3.I USB FW:1.5END APC : 202...

```

The flow that I'm using is this one (not mine), but adapted to me:

```auto
[{"id":"ff997979.d294b8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"73db5098.7513c","type":"exec","z":"ff997979.d294b8","command":"/sbin/apcaccess","addpay":false,"append":"","useSpawn":"","timer":"","name":"apcaccess","x":350,"y":140,"wires":[["dca40517.33c468","b4aed1aa.aec878"],[],[]]},{"id":"a3de16ec.d1803","type":"inject","z":"ff997979.d294b8","name":"every 10 seconds","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"10","crontab":"","once":true,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":177,"y":139,"wires":[["73db5098.7513c"]]},{"id":"dca40517.33c468","type":"function","z":"ff997979.d294b8","name":"Parse apcaccess data","func":"var wanted = ['upsname', 'serialno', 'status', 'linev', 'linefreq', 'loadpct', 'battv', 'bcharge', 'timeleft', 'itemp', 'xonbatt', 'xoffbatt'];\nvar lines = msg.payload.trim().split(\"\\n\");\nvar outputMsgs = [];\nvar mqtttopic = 'home/common/ups/APC';\n\n// loop over every line\nlines.forEach(function (line) {\n // assign values\n var stats = line.split(' : ');\n var label = stats[0].toLowerCase();\n var value = stats[1];\n\n // remove surrounding spaces\n label = label.replace(/(^\\s+|\\s+$)/g, '');\n context.curValues = context.curValues || {};\n context.curValues[label] = context.curValues[label] || 0;\n\n // if found as wanted value, store it\n if (wanted.indexOf(label) > -1) {\n value = value.replace(/(^\\s+|\\s+$)/g, '');\n if (context.curValues[label] != value) {\n outputMsgs.push({topic:mqtttopic + '/' + label, payload:value});\n context.curValues[label] = value;\n }\n }\n});\n\nreturn [outputMsgs];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":120,"wires":[["be0d6ac0.c1f378","d4ecacc3.36cc68"]]},{"id":"be0d6ac0.c1f378","type":"mqtt out","z":"ff997979.d294b8","name":"","topic":"APC","qos":"2","retain":"","broker":"1425f6c7.b9ea79","x":750,"y":120,"wires":[]},{"id":"d4ecacc3.36cc68","type":"debug","z":"ff997979.d294b8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":180,"wires":[]},{"id":"b4aed1aa.aec878","type":"debug","z":"ff997979.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":220,"wires":[]},{"id":"cb1c6661.c219d","type":"function","z":"ff997979.d294b8","name":"UPS_Load","func":"msg.payload = Number(msg.payload.ups_load);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":750,"y":280,"wires":[[]]},{"id":"bd434a78.e3c448","type":"function","z":"ff997979.d294b8","name":"Battery Status","func":"var Vbats = msg.payload.battery_voltage;\n\nvar Vbat = Vbats.replace(\"_\",\".\");\n\nmsg.payload = Number(Vbat);\n\nreturn msg;","outputs":1,"noerr":0,"x":740,"y":360,"wires":[[]]},{"id":"c3d3cefd.564dd8","type":"function","z":"ff997979.d294b8","name":"V IN","func":"var Vins = msg.payload.input_voltage;\n\nvar Vin = Vins.replace(\"_\",\".\");\n\nmsg.payload = Number(Vin);\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":600,"wires":[[]]},{"id":"42e0e523.4e45f4","type":"function","z":"ff997979.d294b8","name":"Bat Runtime","func":"msg.payload = Number(msg.payload.battery_runtime);\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":440,"wires":[[]]},{"id":"fbdb99d8.a26208","type":"function","z":"ff997979.d294b8","name":"Bat Charge","func":"var Vcharges = msg.payload.battery_charge;\n\nvar Vcharge = Vcharges.replace(\"_\",\".\");\n\nmsg.payload = Number(Vcharge);\n\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":520,"wires":[[]]},{"id":"1425f6c7.b9ea79","type":"mqtt-broker","name":"MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

So I would like to use the info of the UPS\_Load, Battery Status, Bat. Runtime, Bat Charge and V In, but I'm not getting there.  
I already tried other ways to get there, but without any success.  
I know, that this is easy for the majority of the users, but I'm stuck here...

Thanks in advance,

Tete

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 January 2021 11:24 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/2 "2021-01-27T11:24:07Z")

</div>

> [@geotete](#):
>
> `1001DATE`

Is there really no divider or anything between the `1` and the `D`? Can you feed it exactly as you get it into a debug node and post a screenshot please?

---

<div class="post-metadata">

**Author:** ![geotete](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@geotete](https://discourse.nodered.org/u/geotete)\
**Post date:** [27 January 2021 11:42 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/3 "2021-01-27T11:42:15Z")

</div>

This is what I'm getting:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/1920862d50e94368fa26e01881576e397055034a.png)

Thanks,

Tete

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 January 2021 11:59 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/4 "2021-01-27T11:59:43Z")

</div>

That is better, there are new line characters there that can be split on. Though it isn't always obvious what exactly to split on (`\n` or `\r`)  
Try this in a function node

```auto
function addProperty(obj, str) {
    const split = str.split(":")
    obj[split[0].trim()] = split[1].trim()
    return obj
}
msg.payload = msg.payload.split("\n").reduce( addProperty, {})
return msg;

```

and see what you get.

---

<div class="post-metadata">

**Author:** ![geotete](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@geotete](https://discourse.nodered.org/u/geotete)\
**Post date:** [27 January 2021 12:05 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/5 "2021-01-27T12:05:56Z")

</div>

Other error:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04ef3da727dd01b69075f5c5bd00f346d64418e5.png)

Tete

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 January 2021 12:15 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/6 "2021-01-27T12:15:30Z")

</div>

Hi your original string example had no newlines and was truncated, so i did this

```auto
[{"id":"6b24e166.a7bb58","type":"inject","z":"a2d9cfcc.919dd8","name":"every 10 seconds","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":140,"y":100,"wires":[["d116d6e9.3d4f08"]]},{"id":"d116d6e9.3d4f08","type":"exec","z":"a2d9cfcc.919dd8","command":"/sbin/apcaccess","addpay":false,"append":"","useSpawn":"","timer":"","name":"apcaccess","x":350,"y":140,"wires":[["d989cc8.1ea653","28470eeb.44571a"],[],[]]},{"id":"d989cc8.1ea653","type":"function","z":"a2d9cfcc.919dd8","name":"Parse apcaccess data","func":"var wanted = [\"LINEV\",\"BCHARGE\"];\nvar hold =msg.payload;\nmsg.payload ={};\nvar mqtttopic = 'home/common/ups/APC';\n// loop over every wanted value\nwanted.forEach((patt) => {\n let regx = new RegExp( patt+\"\\\\s*:\\\\s*\\\\d+\", \"g\");\n regx = hold.match(regx);\nmsg.payload[patt]=regx[0].split(\":\")[1].trim();\n});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":120,"wires":[["defeffa2.acad18"]]},{"id":"28470eeb.44571a","type":"debug","z":"a2d9cfcc.919dd8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":220,"wires":[]},{"id":"defeffa2.acad18","type":"debug","z":"a2d9cfcc.919dd8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":180,"wires":[]},{"id":"b1cc23c4.15fe6","type":"inject","z":"a2d9cfcc.919dd8","name":"test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"APC : 001,043,1001DATE : 2021-01-27 09:52:47 -0100 HOSTNAME : DreamVERSION : 3.14.14 (31 May 2016) debianUPSNAME : APCCABLE : USB CableDRIVER : USB UPS DriverUPSMODE : Stand AloneSTARTTIME: 2021-01-24 11:42:55 -0100 MODEL : Smart-UPS 1000 STATUS : ONLINE LINEV : 244.8 VoltsLOADPCT : 0.0 PercentBCHARGE : 100.0 PercentTIMELEFT : 291.0 MinutesMBATTCHG : 5 PercentMINTIMEL : 3 MinutesMAXTIME : 0 SecondsOUTPUTV : 244.8 VoltsSENSE : HighDWAKE : -1 SecondsDSHUTD : 180 SecondsLOTRANS : 208.0 VoltsHITRANS : 253.0 VoltsRETPCT : 0.0 PercentITEMP : 28.3 CALARMDEL : 30 SecondsBATTV : 27.5 VoltsLINEFREQ : 50.0 HzLASTXFER : No transfers since turnonNUMXFERS : 0TONBATT : 0 SecondsCUMONBATT: 0 SecondsXOFFBATT : N/ASELFTEST : NOSTESTI : 14 daysSTATFLAG : 0x05000008MANDATE : 2004-10-18SERIALNO : AS0443230286BATTDATE : 2004-10-18NOMOUTV : 220 VoltsNOMBATTV : 24.0 VoltsFIRMWARE : 600.3.I USB FW:1.5END APC : 202...","payloadType":"str","x":430,"y":80,"wires":[["d989cc8.1ea653"]]}]

```

if you can provide a correct and full string i could do better.

---

<div class="post-metadata">

**Author:** ![geotete](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@geotete](https://discourse.nodered.org/u/geotete)\
**Post date:** [27 January 2021 12:21 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/7 "2021-01-27T12:21:05Z")

</div>

YES!  
Yours it's working!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aab0e9de3c510fb6285dfa87709c7c925b76a3ec.png)  
Now I'm going to try to get the other values and see if I can get everything working.  
Many thanks!

Tete

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 January 2021 12:21 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/8 "2021-01-27T12:21:08Z")

</div>

Interesting, first check that this gives an array with each element being of the form `name : value`

```auto
msg.payload = msg.payload.split("\n")
return msg;

```

If it does then try

```auto
function addProperty(obj, str) {
    const split = str.split(":")
    if (split.length >= 2) {
        obj[split[0].trim()] = split[1].trim()
    } else if (split.length === 1) {
        obj[split[0].trim()] = ""
    } else {
        obj[str] = "unsplitable"
    }
    return obj
}
msg.payload = msg.payload.split("\n").reduce( addProperty, {})
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 January 2021 13:50 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/9 "2021-01-27T13:50:37Z")

</div>

I would be interested in knowing the issue with my solution if you had a couple of minutes to try what I asked.

---

<div class="post-metadata">

**Author:** ![geotete](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@geotete](https://discourse.nodered.org/u/geotete)\
**Post date:** [27 January 2021 16:55 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/10 "2021-01-27T16:55:21Z")

</div>

Hi Colin,

Yours is working also, and I have to thank you also!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e68e559242884c1eea06cb25393028112ceffd2d.png)

All the best,

Tete

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 January 2021 17:13 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/11 "2021-01-27T17:13:56Z")

</div>

Good, thanks. I haven't used `Array.reduce()` previously but I thought this looked like exactly the sort of problem that it should be able to crack.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 March 2021 17:14 UTC](https://discourse.nodered.org/t/new-user-with-newbie-interpetation-problems/39862/12 "2021-03-28T17:14:26Z")

</div>

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