OpenHab2: Using Node-Red to create rules for MQTT controlled Items (a short introduction)

Goal: Introduce Node-Red as a Rule Engine into Openhab2 and perform your first hello world example.

Prequisites:

Node-Red Installation in Debian 9:

Installation of node-red (in short)

Basically the installation was done like this:

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install gcc g++ make
sudo npm install -g --unsafe-perm node-red

root@openhab:~/.node-red# node-red
9 Dec 09:37:45 - [info]

Welcome to Node-RED
===================

9 Dec 09:37:45 - [info] Node-RED version: v0.19.5
9 Dec 09:37:45 - [info] Node.js  version: v11.4.0
9 Dec 09:37:45 - [info] Linux 4.9.0-8-amd64 x64 LE
9 Dec 09:37:45 - [info] Loading palette nodes
9 Dec 09:37:46 - [warn] rpi-gpio : Raspberry Pi specific node set inactive
9 Dec 09:37:46 - [warn] rpi-gpio : Cannot find Pi RPi.GPIO python library
9 Dec 09:37:46 - [info] Settings file  : /root/.node-red/settings.js
(..)

After this the Node-Red Server will be listening on port http://your-ip:1880

The next thing we want to do is to add the “modules” for openhab2 into node-red:

npm install node-red-contrib-openhab2

In Node-Red you will have the following new items appearing at the buttom of the left lane:

image

So lets create our first switch so that we can watch for changes from our openHab install.

hit “Deploy” on your top right browser window and the “model” becomes active.

image

Head over to your MQTT Client and send a new message to turn the device on.

 mosquitto_pub -t 'SWTest' -m "ON" -d

You will see an instant update of the Model:
image

Next Goal: Turn off the switch after 5 seconds.

First we need an “IF” so that we only perform actions if our event is “ON”

Next thing is to add an delay and then we need an “Out device” (in order to control the switch)

.

Remember to deploy your config :slight_smile:

and test again:

 mosquitto_pub -t 'SWTest' -m "ON" -d

And 5 seconds later you’ll see that the Item is switched off again.

Here is the complete flow from Node-Red as a exported object (Head to Import > Clipboard )
in case you have finger trouble :slight_smile:

enjoy,

Christian

2 Likes

The openHAB nodes use the REST api, not MQTT.

Sorry, I meant that I wanted to extend my use case of “MQTT controlled / configured” items.
My understanding is as well that OpenHab2 <-> NodeRed are using Rest.

:slight_smile: