# Function node IF-ELSE doesnt work

**URL:** <https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812>\
**Category:** General\
**Created:** [27 July 2024 10:54 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812 "2024-07-27T10:54:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![imperium85](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/imperium85/32/83662_2.png) [@imperium85](https://discourse.nodered.org/u/imperium85)\
**Post date:** [27 July 2024 10:54 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/1 "2024-07-27T10:54:36Z")

</div>

Hi Oeople,  
I creted a function node to check if the msg is equal or not.

`if (msg.payload[0].timestamp == msg.newTime) { msg.payload = '1'; } else { msg.payload = '0' } return msg;`

I can see in a debug window that both msg going in is exactly the same but it always comes back 0.

What am I missing? Thanks

---

<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:** [27 July 2024 11:01 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/2 "2024-07-27T11:01:08Z")

</div>

> [@imperium85](#):
>
> What am I missing?

Hard to tell unless you show us your incoming msg.payload and msg.newTime.

---

<div class="post-metadata">

**Author:** ![imperium85](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/imperium85/32/83662_2.png) [@imperium85](https://discourse.nodered.org/u/imperium85)\
**Post date:** [27 July 2024 11:24 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/3 "2024-07-27T11:24:09Z")

</div>

fair enough thanks 🙂

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 July 2024 11:37 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/4 "2024-07-27T11:37:39Z")

</div>

That looks like 2 different messages. You will need to join the streams

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

Or use context to save each message [Working with context : Node-RED](https://nodered.org/docs/user-guide/context) [Node context : Node-RED](https://nodered.org/docs/creating-nodes/context)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [27 July 2024 11:38 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/5 "2024-07-27T11:38:34Z")

</div>

Does

- payload[0].timestamp
- msg.newTime

Appear in the same message object, i.e they are within the same message, when you're running that function?

> [@E1cid](#):
>
> That looks like 2 different messages. You will need to join the streams

My thought exactly

---

<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:** [27 July 2024 11:47 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/6 "2024-07-27T11:47:29Z")

</div>

Assuming that these are properties of a single message:

> [@imperium85](#):
>
> if (msg.payload[0].timestamp == msg.newTime)

I think you need to force both sides of the == to be treated as a number not a date object

```auto
if (Number(msg.payload[0].timestamp) == Number(msg.newTime))

```

---

<div class="post-metadata">

**Author:** ![imperium85](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/imperium85/32/83662_2.png) [@imperium85](https://discourse.nodered.org/u/imperium85)\
**Post date:** [27 July 2024 23:34 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/7 "2024-07-27T23:34:14Z")

</div>

yes it part of a lon msg.payload

---

<div class="post-metadata">

**Author:** ![imperium85](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/imperium85/32/83662_2.png) [@imperium85](https://discourse.nodered.org/u/imperium85)\
**Post date:** [28 July 2024 02:25 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/8 "2024-07-28T02:25:05Z")

</div>

I made it work with a filter node, only sending out the msg. if it it different. Thanks for the help

---

<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:** [26 October 2024 02:26 UTC](https://discourse.nodered.org/t/function-node-if-else-doesnt-work/89812/9 "2024-10-26T02:26:00Z")

</div>

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