Display UDP/ASCII sensor values on Lovelace

Hi Everyone!

I have a weather station which sends out ASCII strings as per the below:

This is sent from the weather station via a GlobalCache’ RS232 converter to Home Assistant (a port which i can define in Node Red). The weather station sends an update of data every second.

What i want to do is take out the various perameters and display this on the lovelace UI, for exmaple wind speed, temperature, humidity, etc.

Any ideas?

Thanks.

it looks like csv format.
I suppose there are several ways how to cope with that. Maybe there is csv parser node.
However the first thing I would try is matching this string against regular expression in function node. The function node can output several messages at once using separate outputs. Then you can connect those outputs to sensor nodes - these will be create sensors in HA automatically.
Having sensor in HA, using them in lovelace is oretty straightforward

Thanks.

This is what I’m stuck on. I’m quite new to node red, so I need help extracting the relevant parts of the ASCII string - it’s broken down with commas between each section, so how can I relate that to within function?

Thanks.

Post that image as text instead and it will be easier to help.

1 Like
NODE, DIR,SPEED,PRESS, RH, TEMP, DEWPOINT,PRT,AN1,AN2,DIG1,VOLT, STATUS

 Q,014,000.06,1011.2,042.1,+023.0,+009.4,,#,#,#,+04.9,00, 40

Where:-

STX
Q Node letter
014 Wind direction
000.06 Wind speed
1011.2 Pressure
042.1 Humidity
+23.0 Temperature
+009.4 Dewpoint
,, PRT (PRT not configured).
# Analogue Input 1 (not configured)
# Analogue Input 2 (not configured)
# Digital Input 1(not configured)
+04.9 Supply voltage
00 Status code
 ETX
40 Checksum

Thanks!

This is how I would do it.

[{"id":"7bf7bca7.cb2724","type":"function","z":"caebef34.4abf3","name":"","func":"var key_str = \"NODE, DIR,SPEED,PRESS, RH, TEMP, DEWPOINT,PRT,AN1,AN2,DIG1,VOLT, STATUS\";\nvar keys = key_str.split(\",\");\n\nvar values = msg.payload.split(\",\");\n\nvar i;\nfor (i = 0; i < values.length; i++) {\n  msg[keys[i]] = values[i];\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":480,"wires":[["8a153fb6.efdcc","f271e113.5bc7e"]]},{"id":"fffcd4d2.f6e258","type":"inject","z":"caebef34.4abf3","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":" Q,014,000.06,1011.2,042.1,+023.0,+009.4,,#,#,#,+04.9,00, 40","payloadType":"str","x":250,"y":480,"wires":[["7bf7bca7.cb2724"]]},{"id":"8a153fb6.efdcc","type":"debug","z":"caebef34.4abf3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":480,"wires":[]},{"id":"f271e113.5bc7e","type":"ha-entity","z":"caebef34.4abf3","name":"weather","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[{"property":"dir","value":"DIR","valueType":"msg"},{"property":"speed","value":"SPEED","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":420,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]


I haven’t installed the component below that is why there is an error.

Function node code:

var key_str = "NODE, DIR,SPEED,PRESS, RH, TEMP, DEWPOINT,PRT,AN1,AN2,DIG1,VOLT, STATUS";
var keys = key_str.split(",");

var values = msg.payload.split(",");

var i;
for (i = 0; i < values.length; i++) {
  msg[keys[i]] = values[i];
}
return msg;

Using: Custom Component for node-red-contrib-home-assistant-websocket

1 Like

Amazing, thanks.

I will give this a go when the unit turns up and will revert back!

By some reason I can’t get dir to function as it should.
Perhaps it’s something wrong with the string I inject.

How did you set up the sensor node?

Are there any specific configurables there?

Thanks.

I see the error now.
There is spaces in the string with keys, remove them and it works fine.

Use the custom component I linked to above.

1 Like

Boom!

[{"id":"5292057a.54e7bc","type":"moment","z":"caebef34.4abf3","name":"","topic":"","input":"","inputType":"date","inTz":"Europe/Brussels","adjAmount":0,"adjType":"days","adjDir":"add","format":"yyyy-MM-DD HH:mm:ss","locale":"C","output":"datetime","outputType":"msg","outTz":"Europe/Brussels","x":420,"y":480,"wires":[["7bf7bca7.cb2724"]]},{"id":"fffcd4d2.f6e258","type":"inject","z":"caebef34.4abf3","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":" Q,014,000.06,1011.2,042.1,+023.0,+009.4,,#,#,#,+04.9,00, 40","payloadType":"str","x":250,"y":480,"wires":[["5292057a.54e7bc"]]},{"id":"7bf7bca7.cb2724","type":"function","z":"caebef34.4abf3","name":"","func":"var key_str = \"NODE,DIR,SPEED,PRESS,RH,TEMP,DEWPOINT,PRT,AN1,AN2,DIG1,VOLT,STATUS\";\nvar keys = key_str.split(\",\");\n\nvar values = msg.payload.split(\",\");\n\nvar i;\nfor (i = 0; i < values.length; i++) {\n  msg[keys[i]] = values[i];\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":480,"wires":[["8a153fb6.efdcc","f271e113.5bc7e"]]},{"id":"8a153fb6.efdcc","type":"debug","z":"caebef34.4abf3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":480,"wires":[]},{"id":"f271e113.5bc7e","type":"ha-entity","z":"caebef34.4abf3","name":"weather","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"weather"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"datetime","stateType":"msg","attributes":[{"property":"dir","value":"DIR","valueType":"msg"},{"property":"speed","value":"SPEED","valueType":"msg"},{"property":"pressure","value":"PRESS","valueType":"msg"},{"property":"humidity","value":"RH","valueType":"msg"},{"property":"temp","value":"TEMP","valueType":"msg"},{"property":"dewpoint","value":"DEWPOINT","valueType":"msg"},{"property":"volt","value":"VOLT","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":810,"y":420,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

But in your case you need to replace the inject with the sensor with that has the weather string.

I suggest using this function code:

var key_str = "NODE,DIR,SPEED,PRESS,RH,TEMP,DEWPOINT,PRT,AN1,AN2,DIG1,VOLT,STATUS";
var keys = key_str.split(",");

var values = msg.payload.split(",");

var i;
for (i = 0; i < values.length; i++) {
  msg[keys[i]] = values[i].replace("+", "")*1;
}
return msg;

This will remove the + on positive values but keep - and remove excess zeros.

1 Like

I will give this a go when it arrives!

Appreciate your help!

Thanks!

I make it with another approach. At first I found csv parser node, and secondly you can create separate sensors for each value. It may or may not fits your needs.
I did it as for exercise.

I didn’t created sensors. you can do it yourself, and put instead debug nodes.
Range nodes are here only to convert string to numbers. CSV parser has such feature built-in but it doesn’t work if number starts with multiple zeroes. Thanx to this conversion, you can archive sensors with real numeric values.
And note, there is no function node at all :wink:
Hint: if you hover on switch node outputs, NR shows the name of expected value

[{"id":"2a508ae3.f3cf96","type":"csv","z":"bcb4b1c6.803708","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"NODE, DIR,SPEED,PRESS, RH, TEMP, DEWPOINT,PRT,AN1,AN2,DIG1,VOLT, STATUS","skip":"0","strings":false,"include_empty_strings":"","include_null_values":"","x":130,"y":440,"wires":[["14d6f134.f9049f"]]},{"id":"47c57585.be7bc4","type":"inject","z":"bcb4b1c6.803708","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Q,014,000.06,1011.2,042.1,+023.0,+009.4,,#,#,#,+04.9,00, 40","payloadType":"str","x":130,"y":370,"wires":[["2a508ae3.f3cf96"]]},{"id":"2f316cba.27b44c","type":"debug","z":"bcb4b1c6.803708","name":"Wind dir","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":360,"wires":[]},{"id":"14d6f134.f9049f","type":"split","z":"bcb4b1c6.803708","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":280,"y":440,"wires":[["1bd48745.2163e1"]]},{"id":"1bd48745.2163e1","type":"switch","z":"bcb4b1c6.803708","name":"","property":"parts.key","propertyType":"msg","rules":[{"t":"eq","v":"NODE","vt":"str"},{"t":"eq","v":"DIR","vt":"str"},{"t":"eq","v":"SPEED","vt":"str"},{"t":"eq","v":"PRESS","vt":"str"},{"t":"eq","v":"RH","vt":"str"},{"t":"eq","v":"TEMP","vt":"str"},{"t":"eq","v":"DEWPOINT","vt":"str"},{"t":"eq","v":"PRT","vt":"str"},{"t":"eq","v":"AN1","vt":"str"},{"t":"eq","v":"AN2","vt":"str"},{"t":"eq","v":"DIG1","vt":"str"},{"t":"eq","v":"VOLT","vt":"str"},{"t":"eq","v":"STATUS","vt":"str"}],"checkall":"true","repair":false,"outputs":13,"x":430,"y":440,"wires":[[],["7723ba26.4c4d64"],["157ec637.f86212"],["e372024c.43923"],["30a515d5.afa6ba"],["71f34ba1.8986ec"],["e42e6590.a82728"],[],[],[],[],["53dc3123.12ead"],[]]},{"id":"157ec637.f86212","type":"range","z":"bcb4b1c6.803708","minin":"0","maxin":"1","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":620,"y":400,"wires":[["2b34d07d.d3f0d8"]]},{"id":"2b34d07d.d3f0d8","type":"debug","z":"bcb4b1c6.803708","name":"Speed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":400,"wires":[]},{"id":"e372024c.43923","type":"range","z":"bcb4b1c6.803708","minin":"0","maxin":"1","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":620,"y":440,"wires":[["2e576d63.0637ba"]]},{"id":"2e576d63.0637ba","type":"debug","z":"bcb4b1c6.803708","name":"Presssure","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":440,"wires":[]},{"id":"30a515d5.afa6ba","type":"range","z":"bcb4b1c6.803708","minin":"0","maxin":"1","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":620,"y":480,"wires":[["ae11d63c.8a175"]]},{"id":"ae11d63c.8a175","type":"debug","z":"bcb4b1c6.803708","name":"Humidity","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":480,"wires":[]},{"id":"71f34ba1.8986ec","type":"range","z":"bcb4b1c6.803708","minin":"0","maxin":"1","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":620,"y":520,"wires":[["a20e847d.9b5e1"]]},{"id":"a20e847d.9b5e1","type":"debug","z":"bcb4b1c6.803708","name":"Temperature","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":520,"wires":[]},{"id":"e42e6590.a82728","type":"range","z":"bcb4b1c6.803708","minin":"0","maxin":"1","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":620,"y":560,"wires":[["16e714b.ac79e6b"]]},{"id":"16e714b.ac79e6b","type":"debug","z":"bcb4b1c6.803708","name":"Dewpoint","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":560,"wires":[]},{"id":"53dc3123.12ead","type":"range","z":"bcb4b1c6.803708","minin":"0","maxin":"1","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":620,"y":600,"wires":[["f8138864.960af"]]},{"id":"f8138864.960af","type":"debug","z":"bcb4b1c6.803708","name":"Voltage","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":600,"wires":[]},{"id":"7723ba26.4c4d64","type":"range","z":"bcb4b1c6.803708","minin":"0","maxin":"1","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":620,"y":360,"wires":[["2f316cba.27b44c"]]}]

Thanks, question - in your comment it has a tab with code - how did you copy that into the comment section and can I import this into NodeRed?

I will give this a go when the device arrives :slight_smile:

do you mean code block?
to copy it to nr, click on copy icon (on the right side of code block), then paste to NR’s import window (see hamburger menu on top-right)

1 Like