Using template node and mustache syntax simply

I've had a lightbulb moment over past 24 hours on how useful the template node can be :slight_smile:

Previously, I'd only ever used it in one flow (and I'd only copied and pasted it so didn't bother working out what it really doing)

I'd only noticed it mentioned when people were talking about stuff with http in/out nodes or dashboard stuff (neither or which I really use) so I just let it woosh over my head.

But I've just realised it it is a VERY simple way of prefixing and appending text to payloads and means I don't have to write JSONata in change nodes or function nodes to get simple jobs done.

So for any others who missed using it simply - here it is

If you have a value (lets say a temperature reading) in msg.payload, then to make that into a nice readable string then simply stick a template node into your flow

image

and amend the text to way you want it

image

image

I'm now off to amend my flows :slight_smile:

8 Likes

I concur, it is also very nice to use to create 'new' objects. Sometimes I am bothered by the change node that keeps the payload in order, where one wants to delete/set new properties, it is more work than the template node. I am glad that NR has all the flexibility one wants.

1 Like

Have you got an example of what you mean?

Example payload:

{"Time":"2019-09-10T11:53:19","Epoch":1568109199,"BME280":{"Temperature":22.39,"Humidity":42.7,"Pressure":1017.3},"BH1750":{"Illuminance":4101},"PressureUnit":"hPa","TempUnit":"C"}

if I want to "translate" this to another format, I use the template node (set output to parsed JSON):

{ 
"temperature": {{ payload.BME280.Temperature }},
"lux": {{payload.BH1750.Illuminance}}
}

Output:

{"temperature":22.6,"lux":4466}
4 Likes

That looks strangely familiar to using this JSONata expression in a change node:

payload { 
    "temperature": BME280.Temperature,
    "lux": BH1750.Illuminance
}

Just sayin' ... ;*)

6 Likes

Yes it is a simplistic example :wink:

This is what I've discovered - the template node is really good for simple stuff where all you want to do is re-arrange the msg a bit or add a bit of text to it.

No trying to remember whether JSONata uses + or & to concatenate some text - all you need to know is msg.payload is called {{payload}}

And the default node shows you this as an example so no need to remember if its { {{ or [[ or (( :slight_smile:

But can the template create an object in msg.payload if the incoming msg.payload is a string?

Yes
You just have to make sure the output of the template is valid JSON and then select output as parsed JSON for it work lovely :slight_smile:

Ok I can't figure it out. Assume I have an inject node with msg payload as a string "Good afternoon" and I want to move it to msg.payload.text. Can you show me an example because I can't figure out how to do it.

The output field for the template node would have to be set to be msg.payload, and the mustache syntax would be a JSON object with the original payload substituted into a new object, like so:

{
    "text": "{{ payload }}"
}

Be careful to put quotes around the original payload placeholder, unless you can guarantee that the replaced value will not cause some invalid JSON syntax ( white space, punctuation, etc). Also, since the "context" of the musctache evaluator is already set to the input msg object, you don't need the "msg." prefix on the payload.

Note that this will cause the existing msg.payload value or object to be replaced by the new object with the one property called "text" -- which may or may not be what you wanted.

1 Like

Thanks Steve, I thought I had tried that variation, but I tried so many I must have missed that.

But for a simple case like that - it would be easier to actually just use a change node
image

2 Likes

Very very true and simple (why do I always forget the 'move' option). I should just give up today. I think I am coming down with a cold and my synapes feel like they are moving thru molasses. :face_with_thermometer:

Wait, I got a flu shot Monday, can I blame that?

3 Likes