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

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"}

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.

[{"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.

And here the JSONATA solution :wink: :wink: The idea of @jbudd to use an array for all the values and assign them to object properties is the same. :slight_smile: 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. :wink:

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

[
    {
        "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

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

[
    {
        "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

1 Like

Ö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

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

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

Alright,
got it. As expected a small step only

2022-03-22 19_36_10-Node-RED _ 192.168.178.9

Well done!

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

1 Like

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. :wink:

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

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

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

So what do you need to see in mqtt explorer?

in MQTT Explorer I see the device, all fine.

But in HA I see

So my assumption is, my configuration send here is wrong
2022-03-28 13_53_06-Node-RED _ 192.168.178.9

to display my values
2022-03-28 13_53_32-Node-RED _ 192.168.178.9

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

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

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

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