# Need help with how to do this

**URL:** <https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540>\
**Category:** General\
**Created:** [1 June 2021 06:20 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540 "2021-06-01T06:20:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 06:20 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/1 "2021-06-01T06:20:53Z")

</div>

There is a _Gremlin_ in this somewhere.

I have narrowed it down to messages being received which are not correctly formatted.

A valid message should look something like this:

```auto
{"payload":"16:14:37@6,1","topic":"alarm_clock/text","_msgid":"efc6bef8.efa76","_event":"node:e8276ab2.052ef8"}

```

The part/s of interest are:  
the time (eg: 16:14:37) and the `@6,1`

That is sent by MQTT to a script.

I modified the script and if a message arrives and there is no `@` in it, it sends the entire message - I hope - back.

So here's my problem:

Every second I am sending a message.  
I want it _cached_. If another message (source) isn't received in a couple of seconds the message is dropped.

If another message (from else where) **IS** received, BOTH messages are kept and then on sent to the next node.

How would I do that?  
I'm stuck on the mechanics of doing it in NR just now.

P.S.  
While writing this question, the code has detected 8 instances of wrongly formatted messages, but the code I have to see them _leaving_ node-red has not detected any.

But I think it would still be good to see them both.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 06:44 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/2 "2021-06-01T06:44:56Z")

</div>

I bashed this together but it isn't doing what I think it should.

I am always getting an output (on left most DEBUG node) even if I press only the top `inject` node.

```auto
[{"id":"64e63da6.313d8c","type":"function","z":"8bb4de19.f72c88","name":"","func":"if (msg.topic == undefined)\n{\n context.set(\"LAST\",msg.payload);\n //return;\n} else\n{\n msg.payload = msg.payload + \" \" + context.get(\"LAST\");\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":3180,"y":2300,"wires":[["be93236a.377f3"]]},{"id":"fd7e579.a379028","type":"random","z":"8bb4de19.f72c88","name":"","low":"1","high":"10","inte":"true","property":"payload","x":3000,"y":2300,"wires":[["9fb96f49.2f72","64e63da6.313d8c"]]},{"id":"af990b30.acd4c8","type":"inject","z":"8bb4de19.f72c88","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"capture","payload":"I want this","payloadType":"str","x":2880,"y":2390,"wires":[["64e63da6.313d8c"]]},{"id":"be93236a.377f3","type":"debug","z":"8bb4de19.f72c88","name":"CAUGHT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3350,"y":2300,"wires":[]},{"id":"a140900e.5f3c4","type":"inject","z":"8bb4de19.f72c88","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2850,"y":2300,"wires":[["fd7e579.a379028"]]},{"id":"9fb96f49.2f72","type":"debug","z":"8bb4de19.f72c88","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":3010,"y":2330,"wires":[]}]

```

This is the code in the `function` node:

```auto
if (msg.topic == undefined)
{
    context.set("LAST",msg.payload);
    //return;
} else
{
    msg.payload = msg.payload + " " + context.get("LAST");
    return msg;
}

```

So, as is, a message comes in.  
No `topic`.

I'm guessing the first line:  
`if (msg.topic == undefined)`  
is wrong.

I've since tried `null`. Same.

So `== ""`?

I thought that was .... `null` or `undefined`.

Sorry.

Someone, again, explain what this is _called_.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [1 June 2021 07:20 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/3 "2021-06-01T07:20:14Z")

</div>

This simple test works for me - returns "no topic" when there isn't one - otherwise just passes msg thru so must be something else to do with your logic (or you do have a topic)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f2dcb28e94232e5c60dc601c6495300ad1a063bf.jpeg)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 07:23 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/4 "2021-06-01T07:23:25Z")

</div>

I put a `debug` node at the input to that `function` node and the topic was shown as "".

So I went down that road and changed it form `undefined` to `""` and it works.

But what is _undefined_, _null_ and _""_.

I again seem to be stuck on their definitions.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 07:29 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/5 "2021-06-01T07:29:37Z")

</div>

> [@Trying\_to\_learn](#):
>
> But what is _undefined_ , _null_ and _""_ .

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

You would be better off with

```auto
if (msg.topic) {
  ...
}

```

that will handle null, undefined & ""

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 07:32 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/6 "2021-06-01T07:32:03Z")

</div>

Aw! It's not a picture. I can't save it and use it locally in the future.

I'll work on how to get a copy of it though.

Thanks.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 07:32 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/7 "2021-06-01T07:32:25Z")

</div>

> [@Trying\_to\_learn](#):
>
> Aw! It's not a picture. I can't save it and use it locally in the future.

screenshot?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 07:33 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/8 "2021-06-01T07:33:29Z")

</div>

Yeah, ok. Sorry.

Firefox is being a bit difficult to me just now as well.  
(never rains, it pours!)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 June 2021 07:34 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/9 "2021-06-01T07:34:30Z")

</div>

> undefined, null and "".

`""` is a String object that contains no characters

`null` is the null object. It exists as a thing and contains no data of any type.

`undefined` means there no object of any sort.

As Steve has shared, when you do a Boolean test of them using `==` they all are equivalent to `false`.

This rather long web page explains it all, but you can skip down to the "sameness" table that gives a similar summary to the picture above.

> **[Equality comparisons and sameness - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)**
>
> JavaScript provides three different value-comparison operations:

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 07:38 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/11 "2021-06-01T07:38:31Z")

</div>

it s the opposite

use `if (!msg.topic) {`

or

```auto
if (msg.topic) {
  ...has topic
} else {
  ... no topic
}

```

---

<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:** [1 June 2021 07:39 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/13 "2021-06-01T07:39:26Z")

</div>

Or visually (from various places on internet) 😉

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30f933b2667884030d80f2d97b56394af41531a6.jpeg)  
they all == no paper

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 07:40 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/14 "2021-06-01T07:40:25Z")

</div>

Love it!  
😉

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 07:42 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/15 "2021-06-01T07:42:47Z")

</div>

Simon, for that particular instance (your picture) , remove the `!` from the first line and it will work.

`if ( ! msg.topic ) {` should read `if ( no topic ) { then `

where as

`if ( msg.topic ) {` should read `if ( has topic ) { then `

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 08:03 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/17 "2021-06-01T08:03:42Z")

</div>

> [@cymplecy](#):
>
> I'm not getting a different output when one message has no topic and one message has msg.topic = ""

you shouldnt for the `if(msg.topic)` test as "" and missing will both fail the test.

Perhaps I am missing the requirement? I thought Andrew wanted to check a topic was "something" hence the `if(msg.topic)` suggestion?

If I missed something (sorry) and Andrew actually wants a blank topic "" to be OK (but not `null` and not `undefined`) then `if(msg.topic == null)` will satisfy that.

Quick demo...

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

```auto
[{"id":"b7c94cb.651b7b","type":"inject","z":"b8d87de8.2e416","name":"topic missing","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1730,"y":160,"wires":[["90d9cc7f.6fd07","9962672.cb75598"]]},{"id":"292a6082.a2937","type":"inject","z":"b8d87de8.2e416","name":"topic == \"\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1720,"y":200,"wires":[["90d9cc7f.6fd07","9962672.cb75598"]]},{"id":"90d9cc7f.6fd07","type":"function","z":"b8d87de8.2e416","name":"if(msg.topic) {","func":"if(msg.topic) {\n msg.payload = \"msg has a valid topic: \" + msg.topic;\n} else {\n msg.payload = \"msg is empty\"\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1980,"y":160,"wires":[["32bcb95b.37df46"]]},{"id":"9962672.cb75598","type":"function","z":"b8d87de8.2e416","name":"if( msg.topic == null ) {","func":"if( msg.topic == null ) {\n msg.payload = \"msg is null or undefined\"\n} else {\n msg.payload = \"msg has a topic: \" + msg.topic;\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2000,"y":200,"wires":[["646ccb55.9d2514"]]},{"id":"32bcb95b.37df46","type":"debug","z":"b8d87de8.2e416","name":"if(msg.topic) {","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2200,"y":160,"wires":[]},{"id":"646ccb55.9d2514","type":"debug","z":"b8d87de8.2e416","name":"if(msg.topic == null) {","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2220,"y":200,"wires":[]},{"id":"76bc23e2.b2326c","type":"inject","z":"b8d87de8.2e416","name":"topic is \"hello topic\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"hello topic","payload":"","payloadType":"date","x":1750,"y":240,"wires":[["90d9cc7f.6fd07","9962672.cb75598"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 08:11 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/19 "2021-06-01T08:11:09Z")

</div>

Ok, just to clarify - sorry folks.

If a message is _going through the system_ I need to catch it if things go awry.

So I have an output (input... what ever) which is the message which is _non-compliant_ to a condition.

If it is, I want to see what the original message is/was that was sent.

So:  
The _Original_ message may (or not) have a defined topic. Not sure if it is null, undefined or "".

If I get a message of a non compliant message, I set the topic to "SOMETHING" (as in it has something in its field.)  
That is sent into a _node_ that has kept the original message, and sends the two out.

These two nodes now seem to do it.

Given the first input is the signal that the message was non compliant to needs.

```auto
[{"id":"1548c7fd.e430b8","type":"change","z":"eff5d6e6.236338","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"catch this","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1625,"y":1360,"wires":[["64e63da6.313d8c"]],"l":false},{"id":"64e63da6.313d8c","type":"function","z":"eff5d6e6.236338","name":"Catch","func":"if (msg.topic == \"\")\n{\n context.set(\"LAST\",msg.payload);\n //return;\n} else\n{\n msg.payload = msg.payload + \" \" + context.get(\"LAST\");\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1740,"y":1360,"wires":[["ea85326b.2d4148"]]}]

```

The original message is sent into the second node.

The output will ONLY happen if there is a message sent to the `change` node.

I can post a better example is needed.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 08:11 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/20 "2021-06-01T08:11:18Z")

</div>

> [@cymplecy](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/816d1a5bbe7b0c8fa30b872df080aae2969b56c3.png)

In the sense that ...

```auto
if (msg.topic) {
  //the topic is something more than "" - lets use it
} else {
  //the topic is either "", null or undefined
}

```

As i said Simon, I might have missed Andrews requirement.

_Nothing to see here folks, move along 🤪_

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 08:13 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/21 "2021-06-01T08:13:59Z")

</div>

> [@Trying\_to\_learn](#):
>
> The _Original_ message may (or not) have a defined topic. Not sure if it is null, undefined or "".
> 
> If I get a message of a non compliant message, I set the topic to "SOMETHING" (as in it has something in its field.)

In which case, as I said before...

```auto
if (msg.topic) {
  //the topic is something more than "" - OK
} else {
  //the topic is either "", null or undefined
  msg.topic = "SOMETHING";
}

```

or

```auto
if (!msg.topic) {
  //the topic is either "", null or undefined
  msg.topic = "SOMETHING";
}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 June 2021 08:15 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/23 "2021-06-01T08:15:14Z")

</div>

Its too early, and I am losing the will ...

Time to get the 🖍🖍🖍 out for me.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 June 2021 08:15 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/24 "2021-06-01T08:15:14Z")

</div>

Ok, a _better_ example of what I want to do:

```auto
[{"id":"a140900e.5f3c4","type":"inject","z":"8bb4de19.f72c88","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2850,"y":2300,"wires":[["fd7e579.a379028"]]},{"id":"fd7e579.a379028","type":"random","z":"8bb4de19.f72c88","name":"","low":"1","high":"10","inte":"true","property":"payload","x":3000,"y":2300,"wires":[["9fb96f49.2f72","64e63da6.313d8c","d52ad08a.3da53"]]},{"id":"9fb96f49.2f72","type":"debug","z":"8bb4de19.f72c88","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":3010,"y":2330,"wires":[]},{"id":"64e63da6.313d8c","type":"function","z":"8bb4de19.f72c88","name":"","func":"if (msg.topic == \"\")\n{\n context.set(\"LAST\",msg.payload);\n //return;\n} else\n{\n msg.payload = msg.payload + \" \" + context.get(\"LAST\");\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":3180,"y":2300,"wires":[["be93236a.377f3"]]},{"id":"d52ad08a.3da53","type":"debug","z":"8bb4de19.f72c88","name":"look at this","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":3160,"y":2240,"wires":[]},{"id":"af990b30.acd4c8","type":"inject","z":"8bb4de19.f72c88","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"capture","payload":"I want this","payloadType":"str","x":2880,"y":2390,"wires":[["64e63da6.313d8c"]]},{"id":"be93236a.377f3","type":"debug","z":"8bb4de19.f72c88","name":"CAUGHT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3350,"y":2300,"wires":[]}]

```

Press the top `inject` node.

Things are good.  
Keep pressing.

Say one of the messages cause _what ever_ to happen, then a signal is sent and the bottom `inject` button is pressed.

That then creates a message to send on the original message with the other message concatenated together.

Clearer?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [1 June 2021 08:17 UTC](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540/25 "2021-06-01T08:17:11Z")

</div>

I've deleted all my posts so your edited post can stand alone 🙂

[Next page](https://discourse.nodered.org/t/need-help-with-how-to-do-this/46540.md?page=2)
