Writing an if function

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

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) 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

Why not use a switch node?

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

In a function node you could write

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.

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

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.

I am getting it from MQTT

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

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

I ended up with two switch nodes

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

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.

I would use two switch nodes.

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.

That works great!

Thank you all.

Jared

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