Add entity value to a mqtt payload

hi all,

i'm starting to get the hang of the basics of node red however i'm still fresh to understanding it so bare with me :slight_smile:

I'm trying to get my snips voice platform to tell me what the temperature is from one of my sensors in home assistant. i'm most the way there as i can get it triggered correctly how ever i'm stuck on how to add the value of the entity "sensor.new_inside_temperature" to this mqtt payload

{"init":{"type":"notification","text":"The Temperature inside is currently ??????? "}} .

I'm not sure how to get the "sensor.new_inside_temperature" to add its value where i have the ??????? in the above MQTT payload. Is it possible.

Appreciate any help on achieving this thanks

Current flow is ...
A whole handful of nodes to strip the snips payload down --> then a switch node to determine if im asking about inside temperature or outside temperature --> then a current state node to get the state of either inside or outside temp sensors. ---> then im stuck at this point ---> then i have the MQTT node to publish the above mqtt payload ( once i determine how to add the value )

I managed to work this out on my own. Yayyyy

For any other newbie in the future searching this type of topic this is how i done it

i used a change node
Set the topic to the temperature sensor value
then set the pay load to {"init":{"type":"notification","text":"The Temperature inside is currently $1 degrees "}}
then changed the payload to search for $1 and replace it with the topic

then sent that to the MQTT broker as per screen shot

Glad you figured it out! It's always nice to see your ideas work the way you envisioned...

A few comments on your solution:
Using msg.topic as a temporary place to hold the value is probably not the best choice, especially when dealing with mqtt messages. Since an object can contain any properties you want, it might be better to use something like msg.temp.

That being said, there are several simpler ways to insert that payload value into a string. I like using either a template node with mustache substitution
Temperature is {{payload}} °F

or a change node with a JSONata expression

"Temperature is " & payload & "°F"

Of course, a simple function node would also work:

msg.payload = "Temperature is " + msg.payload + "°F";
return msg;
2 Likes

Such a simple solution, but I hadn’t thought yet of using a template node for string substitution. In Python context, I would only consider using jinja2 templates when it was a common occurrence and multiline with multiple substitutions and other trouble. Nice tip, will keep this in mind for readable improvements to flows :slight_smile:

Plus, with NodeJS nowadays supporting ES6 templating strings the following function node also works:

msg.payload = `Temperature is ${msg.payload} °F`;
return msg;
1 Like

Thanks shrickus. That's awesome , love people giving multiple clear alternative solutions. It's a great way to learn and improve. Thanks I'm going to go give them a try