OK, so I've been a bit slow in getting my head around JSONata. I knew it was going to be valuable but I've just had my first actual use case.
I needed to rebuild an output payload from several parts of the input so I could monitor inputs from an RFXtrx433E. I wired up all the input nodes from Max's RFX node, added a timestamp using my moment node then added a change node with 2 changes.
The first change alters the msg.topic - I wanted to add RFX/ to the beginning to push the MQTT output messages to a new, dedicated path. Here is the JSONata for that:
"RFX/" & topic
The second change pushes the msg down onto the msg.payload. Unfortunately, although you can grab the whole msg object using "$" in JSONata, Node-RED will not let you put the msg object into the payload directly as it thinks you are creating a circular object definition. So instead, we build the payload manually:
{
"topic": topic,
"status": status,
"payload": payload,
"timestamp": timestamp,
"_msgid": _msgid
}
I haven't included the RFX input nodes in the following flow as they won't work anyway unless you have the right hardware but hopefully you can see the idea. I've also missed off the MQTT output node as it can be annoying to end up with other people's MQTT configuration nodes.
I can very much recommend this talk from Andrew Coleman at IBM if you want to learn the basics of JSONata.