Clear/start new message

This is one way I know to clear the message and start a fresh one:

image

But I feel like there should be a more straightforward way to reset the message?

you could do the same with a change node

How? I tried setting the dropdown to msg. and leaving the input field empty, but it doesn't accept that:

image

Why do you want to clear the whole message? If you want to delete individual properties you can do that in a Change node.

I have a trigger (Home Assistant event) and when that happens I want to send a specific MQTT message. The incoming event message and the outgoing MQTT message are totally separate in their content.

If you are using a function node to construct your output message you could create a new message like this:

let newmsg = {}   //Create a new object for the output message
newmsg.topic = "cmnd/fan" 
newmsg.payload = ""
if (msg.payload.AM2301.Temperature > 30 ) {
    newmsg.payload = "ON"
}
if (msg.payload.AM2301.Temperature < 26 ) {
    newmsg.payload = "OFF"
}

return newmsg;  // return newmsg - the incoming msg is not propagated any further

However, while there may be other elements of the message, msg.payload and msg.topic are the only relevant elements for the mqtt-out node. Provided that you set msg.payload and msg.topic as required for MQTT, it's safe to ignore any other message properties.

Here is a flow to illustrate these two different approaches.

[{"id":"6001c8099957828e","type":"inject","z":"68e4f8b66ca3dee4","name":"temp 25.8","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"flavour","v":"Tomato","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"am2301","payload":"{\"Time\":\"2023-08-02T05:47:56\",\"AM2301\":{\"Temperature\":25.8,\"Humidity\":41.1,\"DewPoint\":15.2},\"TempUnit\":\"C\"}","payloadType":"json","x":100,"y":80,"wires":[["3c406ff0ff986ee3","3e46bf290566175e","fefdc519b53e1d67"]]},{"id":"dbc50ef50fbe5a46","type":"debug","z":"68e4f8b66ca3dee4","name":"Output via function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":100,"wires":[]},{"id":"3c406ff0ff986ee3","type":"function","z":"68e4f8b66ca3dee4","name":"function 48","func":"let newmsg = {}   //Create a new object for the output message\nnewmsg.topic = \"cmnd/fan\" \nnewmsg.payload = \"\"\nif (msg.payload.AM2301.Temperature > 30 ) {\n    newmsg.payload = \"ON\"\n}\nif (msg.payload.AM2301.Temperature < 26 ) {\n    newmsg.payload = \"OFF\"\n}\n\nreturn newmsg;  // return newmsg - the incoming msg is not propagated any further","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":100,"wires":[["dbc50ef50fbe5a46"]]},{"id":"3e46bf290566175e","type":"switch","z":"68e4f8b66ca3dee4","name":"<26 or >30?","property":"payload.AM2301.Temperature","propertyType":"msg","rules":[{"t":"lt","v":"26","vt":"str"},{"t":"gt","v":"30","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":290,"y":180,"wires":[["dba84307c503ea85"],["ab9eff830bc54894"],["aba9b453f77cb87b"]]},{"id":"dba84307c503ea85","type":"change","z":"68e4f8b66ca3dee4","name":"Payload OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"cmnd/fan","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":140,"wires":[["bcc9a5befa410d06"]]},{"id":"ab9eff830bc54894","type":"change","z":"68e4f8b66ca3dee4","name":"Payload ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"cmnd/fan","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":180,"wires":[["bcc9a5befa410d06"]]},{"id":"aba9b453f77cb87b","type":"change","z":"68e4f8b66ca3dee4","name":"Payload Blank","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"cmnd/fan","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":220,"wires":[["bcc9a5befa410d06"]]},{"id":"bcc9a5befa410d06","type":"debug","z":"68e4f8b66ca3dee4","name":"Output via change node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":180,"wires":[]},{"id":"b98119e69da57667","type":"inject","z":"68e4f8b66ca3dee4","name":"temp 30.8","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"timestamp","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"am2301","payload":"{\"Time\":\"2023-08-02T05:47:56\",\"AM2301\":{\"Temperature\":30.8,\"Humidity\":41.1,\"DewPoint\":15.2},\"TempUnit\":\"C\"}","payloadType":"json","x":100,"y":160,"wires":[["3e46bf290566175e","3c406ff0ff986ee3","fefdc519b53e1d67"]]},{"id":"5074cc2c24b661ee","type":"inject","z":"68e4f8b66ca3dee4","name":"temp 29.8","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"seasoning","v":"paprika","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"am2301","payload":"{\"Time\":\"2023-08-02T05:47:56\",\"AM2301\":{\"Temperature\":29.8,\"Humidity\":41.1,\"DewPoint\":15.2},\"TempUnit\":\"C\"}","payloadType":"json","x":100,"y":120,"wires":[["3e46bf290566175e","3c406ff0ff986ee3","fefdc519b53e1d67"]]},{"id":"bc9d3848b0a797b7","type":"debug","z":"68e4f8b66ca3dee4","name":"Input data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":60,"wires":[]},{"id":"fefdc519b53e1d67","type":"junction","z":"68e4f8b66ca3dee4","x":220,"y":60,"wires":[["bc9d3848b0a797b7"]]}]
1 Like

With a function node it's no problem, I had a solution in my original post.

I just thought it might be a common operation and there ought to be a specific node for it.

Yeah, it'll work for now, but I think it's kind of hacky. There's no guarantee that a future Node-RED update doesn't introduce a new input to the MQTT node.

It’s not really hacky as nodejs works by passing references to objects so just updating the payload and topic parts of the object and passing on the overall pointer are more efficient than creating a whole new object and copying things over.

What I meant is:

If in a future version of Node-RED the MQTT-out node gets a new input, this input will likely have a sensible default. But if I now pass the whole trigger message without filtering to the MQTT-out node, I might accidentally set this new input to gibberish.

Any node may be updated and start interpreting properties previously not used. That should always be a major update though, as it could be a breaking change. Following your logic one would have to clear out messages in front of every node in the flow. In fact unused properties are expected to pass through nodes unchanged, this is a very common method of passing data to nodes further along the flow.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.