# SERIAL IN string to be added with VALUE DESC, then polling as MQTT messages

**URL:** <https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204>\
**Category:** General\
**Created:** [22 March 2022 15:28 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204 "2022-03-22T15:28:34Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuhnino](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuhnino/32/58898_2.png) [@kuhnino](https://discourse.nodered.org/u/kuhnino)\
**Post date:** [22 March 2022 15:28 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/1 "2022-03-22T15:28:34Z")

</div>

Alright, hello out there.  
I want to connect an USB device, added to a Node-Red Docker container on an UNRAID server, parse those received data nad publis it using MQTT.

SERIAL IN : Is working already, the seril STRING is visible  
MQTT OUT : Also working, my need some more mods, but good to go.

What is not working is to PARSE the reveived string, add the value desciption and get one MQTT payload combined.

Have read and watched a lot videos already, but always getting stuck

Clearky spoken, this is what I receive over serial  
dr V1.31 200 12 1 1 1 530 530 650 636 594 29 89 198.9 199.72 0.2936 58.63 1988 388032 0 -18 1.3 816 616 596 0 1 1201002106080018 636 78

These are the desciption to to be added  
dr Vers dayctr status Rel\_dcwp Rel\_dc Rel\_ac tempist tempmin tempmax tempsoll\_dc tempsoll\_ac tempchip iso\_rel UMpp UIst Iist Pist Whperday whtotal whac dp Pwm scantimer minute dc\_ontime dc\_heatmode position serialno pot\_pos checksum

And once both are MERGED, it should look like  
SENSOR = {"dr":"dr","Vers":"V1.31","dayctr":"200","..........................}

Similar what my other MQTT devices send.  
SENSOR = {"Time":"2022-03-22T16:24:39","Switch2":"OFF","DHT11":{"Temperature":15.8,"Humidity":43.0,"DewPoint":3.2},"TempUnit":"C"}

 ![2022-03-22 16_20_39-Node-RED _ 192.168.178.9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/5593edcec243b8503b01187c3bbdd2dfd99b52de.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [22 March 2022 16:50 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/2 "2022-03-22T16:50:51Z")

</div>

You need to convert the values `dr V1.31 200 12 ...` into an array `[dr, V1.31, 200, 12, ...]`, which you can do with a split (split on " ") and a join node (join as an array).

And the descriptions `dr Vers dayctr status Rel_dcw ...` as another array.

Then you create an object from the two arrays.

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b43b283c5dc13a3d92d9853447bbd6c6f06ceaba.jpeg)

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5c5a537193490c7e833c9335642c7bdee969ea2.jpeg)

```auto
[{"id":"fe3f87d78808cd82","type":"tab","label":"Flow 5","disabled":false,"info":"","env":[]},{"id":"a36178e5a8edd6ad","type":"inject","z":"fe3f87d78808cd82","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"dr V1.31 200 12 1 1 1 530 530 650 636 594 29 89 198.9 199.72 0.2936 58.63 1988 388032 0 -18 1.3 816 616 596 0 1 1201002106080018 636 78","payloadType":"str","x":150,"y":140,"wires":[["8b662e534ba7c6e0"]]},{"id":"8af55cb5386c1953","type":"function","z":"fe3f87d78808cd82","name":"","func":"var values = msg.payload;\nvar names = [\"dr\", \"Vers\", \"dayctr\", \"status\", \"Rel_dcwp\", \"Rel_dc\", \"Rel_ac\", \"tempist\", \"tempmin\", \"tempmax\", \"tempsoll_dc\", \"tempsoll_ac\", \"tempchip\", \"iso_rel\", \"UMpp\", \"UIst\", \"Iist\", \"Pist\", \"Whperday\", \"whtotal\", \"whac\", \"dp\", \"Pwm\", \"scantimer\", \"minute\", \"dc_ontime\", \"dc_heatmode\", \"position\", \"serialno\", \"pot_pos\", \"checksum\"];\nfunction toObject(names, values) {\n var result = {};\n for (var i = 0; i < names.length; i++)\n result[names[i]] = values[i];\n return result;\n}\n\nmsg.payload = toObject(names, values);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":140,"wires":[["7725ce492a22c929"]]},{"id":"7725ce492a22c929","type":"debug","z":"fe3f87d78808cd82","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":140,"wires":[]},{"id":"8b662e534ba7c6e0","type":"split","z":"fe3f87d78808cd82","name":"","splt":" ","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":270,"y":140,"wires":[["e935e75280c24f15"]]},{"id":"e935e75280c24f15","type":"join","z":"fe3f87d78808cd82","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":140,"wires":[["8af55cb5386c1953"]]}]

```

There is no conversion in this example from string to integer / float.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [22 March 2022 17:41 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/3 "2022-03-22T17:41:50Z")

</div>

And here the JSONATA solution 😉 😉 The idea of @jbudd to use an array for all the values and assign them to object properties is the same. 🙂 Only the way of the solution is different - but as already said in the other thread - there are always different ways to get the solution and you can choose - what you prefer. 😉

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

Not all parameters used - and converting numbers - probably when it makes sense.

```auto
[
    {
        "id": "253ccb92e1a2d5e5",
        "type": "inject",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "dr V1.31 200 12 1 1 1 530 530 650 636 594 29 89 198.9 199.72 0.2936 58.63 1988 388032 0 -18 1.3 816 616 596 0 1 1201002106080018 636 78",
        "payloadType": "str",
        "x": 270,
        "y": 800,
        "wires": [
            [
                "16dea3cdd94daebc"
            ]
        ]
    },
    {
        "id": "138ff9641c9b8872",
        "type": "debug",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 800,
        "wires": []
    },
    {
        "id": "16dea3cdd94daebc",
        "type": "change",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$split(payload, \" \")\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\t \"dr\" : payload[0],\t \"Vers\" : payload[1],\t \"dayctr\" : payload[2],\t \"status\" : payload[3],\t \"Rel_dcwp\" :payload[4] ,\t \"Rel_dc\" : payload[5],\t \"Rel_ac\" :payload[6] ,\t \"tempist\" : $number(payload[7]),\t \"tempmin\" :$number(payload[8] ),\t \"tempmax\" : $number(payload[9]),\t \"tempsoll_dc\" : $number(payload[10]), \t \"tempsoll_ac\" : $number(payload[11]),\t \"tempchip\" :$number(payload[12]),\t \"iso_rel\" : payload[13],\t \"UMpp UIst\": payload[14]\t }",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 800,
        "wires": [
            [
                "138ff9641c9b8872"
            ]
        ]
    }
]

```

So I did not define each property of the object, but if you look to the change node - the object more or less is defined as it will look like later on as JSON String:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b56a519d8cbf0d1019f90a083f72d09809730a6.png)

---

<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:** [22 March 2022 18:11 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/4 "2022-03-22T18:11:39Z")

</div>

And here for good measure is the low code way using a csv node

```auto
[
    {
        "id": "253ccb92e1a2d5e5",
        "type": "inject",
        "z": "bf9e1e33.030598",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "dr V1.31 200 12 1 1 1 530 530 650 636 594 29 89 198.9 199.72 0.2936 58.63 1988 388032 0 -18 1.3 816 616 596 0 1 1201002106080018 636 78",
        "payloadType": "str",
        "x": 250,
        "y": 240,
        "wires": [
            [
                "23bdfd05.5d397a"
            ]
        ]
    },
    {
        "id": "23bdfd05.5d397a",
        "type": "csv",
        "z": "bf9e1e33.030598",
        "name": "",
        "sep": " ",
        "hdrin": "",
        "hdrout": "none",
        "multi": "one",
        "ret": "\\n",
        "temp": "dr,Vers,,dayctr,status,Rel_dcwp,Rel_dc,Rel_ac,tempist,tempmin,tempmax,tempsoll_dc,tempsoll_ac,tempchip,iso_rel,UMpp UIst",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 430,
        "y": 240,
        "wires": [
            [
                "138ff9641c9b8872"
            ]
        ]
    },
    {
        "id": "138ff9641c9b8872",
        "type": "debug",
        "z": "bf9e1e33.030598",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 240,
        "wires": []
    }
]

```

Adding the property names you wish and to skip values using extra commas `,,` skips 1 value `,,,` skips 2 etc

---

<div class="post-metadata">

**Author:** ![kuhnino](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuhnino/32/58898_2.png) [@kuhnino](https://discourse.nodered.org/u/kuhnino)\
**Post date:** [22 March 2022 18:23 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/5 "2022-03-22T18:23:56Z")

</div>

Öha,  
so impressed, I already spended hours to get this solved and three of you didi that within "minutes".  
Tx to all three.

As a beginner, I have to concentrate on one before proceeding.

By choice I started with option2 from [mickym2](https://discourse.nodered.org/u/mickym2)

I added all the parameter to the JSON script  
{  
"dr" : payload[0],  
"Vers" : payload[1],  
"dayctr" : $number(payload[2]),  
"status" : payload[3],  
"Rel\_dcwp" :payload[4],  
"Rel\_dc" : payload[5],  
"Rel\_ac" :payload[6],  
"tempist" : $number(payload[7]),  
"tempmin" :$number(payload[8] ),  
"tempmax" : $number(payload[9]),  
"tempsoll\_dc" : $number(payload[10]),  
"tempsoll\_ac" : $number(payload[11]),  
"tempchip" :$number(payload[12]),  
"iso\_rel" : payload[13],  
"UMpp": payload[14],  
"UIst" : $number(payload[15]),  
"Iist" : $number(payload[16]),  
"Pist" : $number(payload[17]),  
"Whperday" : $number(payload[18]),  
"whtotal" : $number(payload[19]),  
"whac" : payload[20],  
"dp" : payload[21],  
"Pwm" : payload[22],  
"scantimer" : payload[23],  
"minute" : payload[24],  
"dc\_ontime" : payload[25],  
"ac\_heatmode" : payload[26],  
"position" : payload[27],  
"serialno" : payload[28],  
"pot\_pos" : payload[29],  
"checksum" : payload[30]  
}

but why ever the MQTT message and Debug message is only working for the first line

![2022-03-22 19_18_19-Node-RED _ 192.168.178.9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a1dcb80da9dc9da8519dd2f3f20b1c8fdbeb407.png)

 ![2022-03-22 19_22_39-MQTT Explorer](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e14f9ce0407a8118740041549a75a9b08502f70.png)

Maybe only some small steps to go.......

---

<div class="post-metadata">

**Author:** ![kuhnino](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuhnino/32/58898_2.png) [@kuhnino](https://discourse.nodered.org/u/kuhnino)\
**Post date:** [22 March 2022 18:37 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/6 "2022-03-22T18:37:52Z")

</div>

Alright,  
got it. As expected a small step only

![2022-03-22 19_36_10-Node-RED _ 192.168.178.9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21e93ee5126d0db00681a24236077f267c5884fa.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [22 March 2022 18:59 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/7 "2022-03-22T18:59:42Z")

</div>

> [@kuhnino](#):
>
> As expected a small step only

Well done!

@mickym2, @E1cid: awesome. now I feel the urge to write Bash and AWK versions... Resisting of course! 😁

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [22 March 2022 19:56 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/8 "2022-03-22T19:56:56Z")

</div>

OK - I expected this - as I saw the arrows in your string - but could not evaluate what it was - so it was a tab as separator instead of a space. 😉

@E1cid - thanks as well, so I need to learn from the master as well - how flexible, specific nodes can be used.

---

<div class="post-metadata">

**Author:** ![kuhnino](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuhnino/32/58898_2.png) [@kuhnino](https://discourse.nodered.org/u/kuhnino)\
**Post date:** [28 March 2022 10:32 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/9 "2022-03-28T10:32:00Z")

</div>

gents, I hope some of you are still listening to this post.

My configuration is working now as expected but I have still some troubles of having these values in HomeAssistant.  
Seems my MQTT device CONFIG is not correct. Any idea how to proceed further ?

Screenshot of NODE RED and MQTT Explorer

 ![2022-03-28 12_29_54-Node-RED _ 192.168.178.9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/781f47afa27a54fb0e69e970fc93b4d59f613b2f.png)

So my device "PVGarage" is recognised inside HomeAssistant, but without any Entities

---

<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:** [28 March 2022 11:10 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/10 "2022-03-28T11:10:28Z")

</div>

So what do you need to see in mqtt explorer?

---

<div class="post-metadata">

**Author:** ![kuhnino](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuhnino/32/58898_2.png) [@kuhnino](https://discourse.nodered.org/u/kuhnino)\
**Post date:** [28 March 2022 11:54 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/11 "2022-03-28T11:54:26Z")

</div>

in MQTT Explorer I see the device, all fine.

But in HA I see

 ![2022-03-28 13_52_04-Einstellungen – Home Assistant](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c259346f2b325ece02752a84cb3fe51e5a546026.png)

So my assumption is, my configuration send here is wrong  
 ![2022-03-28 13_53_06-Node-RED _ 192.168.178.9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65099cd3ccee4bd096e0ad74bb3d99bf13c366a9.png)

to display my values  
 ![2022-03-28 13_53_32-Node-RED _ 192.168.178.9](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6bb21f051dd8c307af59a1ddfa53c10d90795efd.png)

---

<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:** [28 March 2022 12:42 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/12 "2022-03-28T12:42:07Z")

</div>

I think this is now entirely an HA question, so you would probably be better to ask on an HA forum.

---

<div class="post-metadata">

**Author:** ![kuhnino](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuhnino/32/58898_2.png) [@kuhnino](https://discourse.nodered.org/u/kuhnino)\
**Post date:** [28 March 2022 15:26 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/13 "2022-03-28T15:26:24Z")

</div>

fair enough,  
will try to do so, I just thought someone who has helped me already may have a hint again

---

<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:** [28 March 2022 15:52 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/14 "2022-03-28T15:52:18Z")

</div>

Few, if any, here use HA so probably no-one knows the answer. I may be wrong though.

---

<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:** [11 April 2022 15:52 UTC](https://discourse.nodered.org/t/serial-in-string-to-be-added-with-value-desc-then-polling-as-mqtt-messages/60204/15 "2022-04-11T15:52:26Z")

</div>

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