# Clear/start new message

**URL:** https://discourse.nodered.org/t/clear-start-new-message/80238
**Category:** General
**Created:** [1 August 2023 23:31 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238 "2023-08-01T23:31:01Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![AndreKR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrekr/32/81657_2.png) [@AndreKR](https://discourse.nodered.org/u/AndreKR)
#### Post date: [1 August 2023 23:31 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/1 "2023-08-01T23:31:02Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a3c5f6f5ccee6c57da546df1a5fcca1c1cec217.png)

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

---

<div class="post-metadata">

### Author: ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)
#### Post date: [2 August 2023 01:20 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/2 "2023-08-02T01:20:50Z")

</div>

you could do the same with a change node

---

<div class="post-metadata">

### Author: ![AndreKR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrekr/32/81657_2.png) [@AndreKR](https://discourse.nodered.org/u/AndreKR)
#### Post date: [2 August 2023 01:37 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/3 "2023-08-02T01:37:57Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2bab233535e72b43d2de95a8b916c9c8ec51c447.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [2 August 2023 06:27 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/4 "2023-08-02T06:27:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![AndreKR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrekr/32/81657_2.png) [@AndreKR](https://discourse.nodered.org/u/AndreKR)
#### Post date: [2 August 2023 11:42 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/5 "2023-08-02T11:42:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 August 2023 12:29 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/6 "2023-08-02T12:29:49Z")

</div>

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

```auto
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.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e24024c747e603bc9d15bb2a22e050be1c425bf0.jpeg)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

### Author: ![AndreKR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrekr/32/81657_2.png) [@AndreKR](https://discourse.nodered.org/u/AndreKR)
#### Post date: [2 August 2023 13:45 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/7 "2023-08-02T13:45:26Z")

</div>

> [@jbudd](#):
>
> If you are using a function node to construct your output message

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.

> [@jbudd](#):
>
> 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.

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.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [2 August 2023 14:45 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/8 "2023-08-02T14:45:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![AndreKR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrekr/32/81657_2.png) [@AndreKR](https://discourse.nodered.org/u/AndreKR)
#### Post date: [2 August 2023 14:56 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/9 "2023-08-02T14:56:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [2 August 2023 20:31 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/10 "2023-08-02T20:31:03Z")

</div>

> [@AndreKR](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 October 2023 20:31 UTC](https://discourse.nodered.org/t/clear-start-new-message/80238/11 "2023-10-01T20:31:07Z")

</div>

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