# Writing an if function

**URL:** <https://discourse.nodered.org/t/writing-an-if-function/82445>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 October 2023 13:47 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445 "2023-10-29T13:47:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jared.rackliff](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jared.rackliff](https://discourse.nodered.org/u/jared.rackliff)\
**Post date:** [29 October 2023 13:47 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/1 "2023-10-29T13:47:46Z")

</div>

Good morning,

I am so new to Node-Red and really want to be able to succeed. I have been banging my head for days now on creating an if statement. I am trying to see If a specific deviceid is set to 1. if so it will then continue the flow.

Any ideas what node should call this if statement and how to write the if statement.

Thank you.

Jared

---

<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:** [29 October 2023 14:02 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/2 "2023-10-29T14:02:51Z")

</div>

Welcome to the forum.

Feed the message containing the device id into a debug node and show us what is there.  
You can use a Switch node to only pass messages on if the id is 1.

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to the id.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 October 2023 14:56 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/3 "2023-10-29T14:56:33Z")

</div>

Why not use a `switch` node?

---

<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:** [29 October 2023 15:09 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/4 "2023-10-29T15:09:08Z")

</div>

Let's say the deviceid is a property of msg.payload - msg.payload.deviceid.

In a function node you could write

```auto
if ( msg.payload.deviceid === 1 ) { // === means equals, both value and datatype ie 1 but not "1"
   msg.payload.somethingelse = "This is device 1!"
   return msg
}
else {
   msg.payload.somethingelse = "Different device" // This message is not passed on; no return statement
}

```

But the switch node allows you to do this without writing any code.

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

---

<div class="post-metadata">

**Author:** ![jared.rackliff](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jared.rackliff](https://discourse.nodered.org/u/jared.rackliff)\
**Post date:** [29 October 2023 17:04 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/5 "2023-10-29T17:04:54Z")

</div>

Here are the results: {"topic":"smartthings/ed19b230-866a-4923-ad30-b147f53fc5a3/motionSensor/motion","payload":"active","qos":1,"retain":true,"\_msgid":"8247653b365e91f6"}

I need an if statement that finds the Topic with the payload of active

Thank you.

Jared

---

<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:** [29 October 2023 17:28 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/6 "2023-10-29T17:28:04Z")

</div>

Personally I would do that by having an MQTT In node with that topic and then a Switch node configured to only pass messages where msg.payload is `active`.

---

<div class="post-metadata">

**Author:** ![jared.rackliff](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jared.rackliff](https://discourse.nodered.org/u/jared.rackliff)\
**Post date:** [29 October 2023 17:45 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/7 "2023-10-29T17:45:12Z")

</div>

I am getting it from MQTT

 ![2023-10-29-134132_1360x768_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5d4d8d6ceeed9945b6f9819eaaf42eb8329f39bd.png)

and to the far right you can see the output from MQTT

 ![2023-10-29-134431_1360x768_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/e/dedb431c161af1fceab8bf9d3064d550802a3870.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:** [29 October 2023 17:51 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/8 "2023-10-29T17:51:06Z")

</div>

So you don't need to test the topic, as you know already that will be correct. All you need is a Switch node checking for payload == "active"  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf9e3dac7866348fad5824180c9d6c40bbbaf1a8.png)

---

<div class="post-metadata">

**Author:** ![jared.rackliff](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jared.rackliff](https://discourse.nodered.org/u/jared.rackliff)\
**Post date:** [29 October 2023 17:56 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/9 "2023-10-29T17:56:40Z")

</div>

I ended up with two switch nodes

1st one checks that the Topic is correct  
2nd one checks that the payload is active

 ![2023-10-29-135141_1360x768_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0e94290f4ec19751dc8a5098b0da1307d702368.png)

There has to be a better way to use just one switch node to grab topic and payload or should I use a function and if true output to a switch node and then the debug

How would you create this coming out of MQTT? Here is my goal when the MQTT subscibes and gives me the correct Topic that is active it will the go to Amazon Echo to say Hawkeye is at the door.

I set a variable called cat outside when the motion triggers active it will send it through MQTT I pickup it is right Topic and active and then it goes to echo to say it at my speaker.

I guess I also need to make sure the value in device Hawkye is outside is a 1

Thank you.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 October 2023 20:23 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/10 "2023-10-29T20:23:33Z")

</div>

> [@jared.rackliff](#):
>
> How would you create this coming out of MQTT?

I would use two switch nodes.

---

<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:** [29 October 2023 20:24 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/11 "2023-10-29T20:24:50Z")

</div>

As I said, if you are interested in a particular topic then use an MQTT node configured to listen to just that topic - in this case "smarthings/ed19b230-866a-4923-ad30-b147f53fc5a3/motionSensor/motion". Then you can use just one Switch node.

---

<div class="post-metadata">

**Author:** ![jared.rackliff](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jared.rackliff](https://discourse.nodered.org/u/jared.rackliff)\
**Post date:** [30 October 2023 01:34 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/12 "2023-10-30T01:34:12Z")

</div>

That works great!

Thank you all.

Jared

---

<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:** [29 December 2023 01:34 UTC](https://discourse.nodered.org/t/writing-an-if-function/82445/13 "2023-12-29T01:34:20Z")

</div>

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