First post! Embedding payload into a formatted message

First of all. Node Red. What a game changer! Loving it.
Can someone help?

I'd like to know how to embed the payload message from "soc" (Battery state of charge) sensor in HA.

Sensor returns msg.payload "97.7" (as an example). But no unit of measurement ie: %

I'd like to format the payload in a message to send to telegram in a function node?

Desired message:

The batteries are at 97.7 %

Can paste the full flow text if needed.

Cheers!

Hi.

Glad you like Node-Red. I too was originally sceptical about it until one day I saw its potential.

Ok, you are receiving a 97.7 from a node which is the battery charge.

To format it as you want, there are a few ways.

The 97.7 is a number. Though a percentage is also (I guess) a number, not in the NR sense of things.

You need to convert the 97.7 (number) to a string. (I can't help as I don't know the command off the top of my head) and append a % symbol.

So you would have a function node as you guessed and all it would do is the conversion and append the % symbol.

Basically:

let x = msg.payload;
(convert that to a string with a magic command)
x = x + "%";
msg.payload = x;
return msg;

Then you send the payload (string) out and the next node to receive it processes it as a string.

(First edit)
(After a quick search and seeing Nic's reply)

The command:
x = x.toString();

Nic's way is using standard/supplied nodes to do things.

I am not going to say which way is better, but the way I suggest keeps it in one node and formats your message to have a trailing % sign.

(Second edit)

If you are wanting the message to be:
The batteries are at 97.7%
You could prefix the text when constructing the message in what I showed you or use Nic's way with the template node.

1 Like

Have a look at the Template node. You can use that to set payload (or any message property) to a piece of text with parts coming from message properties.

1 Like

Thanks for the help guys!
Went with the template node. Got the message delivered to telegram...

Screenshot 2019-12-27 at 22.31.31

1 Like