# Node-RED (MQTT)

⚠️ This guide covers the use of MQTT in Node-RED. If you want to integrate Node-RED with there is an easier way to do this now. See this guide. This guide is still left here since it is still useful. ⚠️

Node-RED (opens new window) is a great tool for building rules on signals from IoT Open or other platforms. There are a lot of modules and functions that can be applied to your IoT Open installation.

This is just an example of what you can do.

# Example task

The task to solve is to control lights depending on a sensor value. We have a LoRaWan motion sensor, and a light controlled using a Z-wave interface.

# The idea

A detection of motion turns the light on and after an amount of time the light turns off. If there are reoccurring motions the time extends so that the light finally turns off after the amount of time after the last motion.

This is implemented in Node-RED with the following nodes.

MQTT in: Subscribe to the topic used by the motion sensor.

Switch: Used to check for motion only

Trigger: Sets the message to 1 (turn light on) on motion and 0 (turn light off) after an amount of time (we use only 5 seconds in the example for testing).

MQTT out: publish 1 or 0 to the topic used by the light.

The topics the functions use is copied from the IoT Open web GUI. The flow is shown below.


# Implementation

# MQTT in

Create a MQTT connection using your credentials to IoT Open. E.g.

Tab Connection

Server: lynx.iotopen.se
Port: 8883
Enable secure (SSL/TLS) connection: Checked

Tab Security

Username: can be anything
Password: API-Key from IoT Open

Subscribe to the topic that the motion sensor publish data to. This can be found in IoT Open. Also set Output to "a parsed JSON object" since the data from IoT Open is JSON.


# Switch

Since the motion sensor might send other values than motion like motion stop or something else we need to check that the value is corresponding to motion. In our example "1" means motion. Some sensors use 255 instead.

Since the data now is in object-form it is easy to check.


# Trigger

The Trigger node is a very powerful node that can be used fot lots of things. Here we use it to turn on the light on then after five seconds turn it off. We want to send the JSON string representing turning on and off the light. These parameters can be found in Lynx GUI. Note that we only sets the payload of the message here, the topic will be added in the next node.


# MQTT out

Similar to MQTT in but now we want to send the data from the trigger to the right topic. The topic can be found in Lynx.


# Test

When testing and observing traffic on MQTT the following messages are sent.

2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406543}
2/set/obj/zwave/1/switch {"value":1}
2/set/obj/zwave/1/switch {"value":0}

The first line is the motion detected by a lora motion sensor. The second line sent by Node-RED comes immediately after the first one. The light is lit. Then after 5 seconds the third message is sent by Node-RED and the light is turned off. `` What happens if there are repeated motions?

2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406551}
2/set/obj/zwave/1/switch {"value":1}
2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406552}
2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406553}
2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406554}
2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406555}
2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406555}
2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406556}
2/obj/lora/647fda00000012ea/motion {"value":1, "timestamp":1582406557}
2/set/obj/zwave/1/switch {"value":0}

Works like intended. Every motion postpones the turning off of the light by 5s.

Now let's set the delay to something more relevant, for instance 2 minutes.

# Test yourself

Below is the dashboard data. You can import it in your installation of Node-RED.

[{"id":"63d2e08b.83c55","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"7cfb8b89.d1cdc4","type":"mqtt out","z":"63d2e08b.83c55","name":"IoT Open MQTT","topic":"2/set/obj/zwave/1/switch","qos":"","retain":"","broker":"c9847e6f.595d9","x":440,"y":460,"wires":[]},{"id":"b2fa6ceb.d32fe","type":"mqtt in","z":"63d2e08b.83c55","name":"","topic":"2/obj/lora/647fda00000012ea/motion","qos":"2","datatype":"json","broker":"c9847e6f.595d9","x":200,"y":300,"wires":[["4b4d961.512e868"]]},{"id":"4b4d961.512e868","type":"switch","z":"63d2e08b.83c55","name":"If motion","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":240,"y":380,"wires":[["2ac0c2e8.82ab1e"]]},{"id":"2ac0c2e8.82ab1e","type":"trigger","z":"63d2e08b.83c55","op1":"{\"value\":1}","op2":"{\"value\":0}","op1type":"json","op2type":"json","duration":"5","extend":true,"units":"s","reset":"","bytopic":"all","name":"","x":420,"y":380,"wires":[["7cfb8b89.d1cdc4"]]},{"id":"c9847e6f.595d9","type":"mqtt-broker","z":"","name":"IoT Open Lynx","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]