RFXtrx433E, Node-RED, MQTT and Home Assistant

This post describes how I have setup an RFXtrx433E device with a Raspberry Pi to transform data from inexpensive 433 MHz motion- and climate-sensors into MQTT messages on my local network. With the data available as MQTT messages I can store the data in InfluxDB for viewing in Grafana, show the data in Home Assistant and route the data to cloud services.

Background

I have some home made sensor devices scattered around our home that sends detection and climate information via 433 MHz using my own simple protocol. The signals are registered by an ESP8266 device that transforms the messages to MQTT messages over IP. These MQTT messages then go into a database, to the cloud and into Home Assistant. See these posts for details:

The Sensor433 library and Geeetech transmitters and receivers

InfluxDB and Grafana for sensor time series

A self-hosted MQTT environment for Internet of Things – Part 3

Home Assistant – getting started and using MQTT sensors

I recently bought some inexpensive off-the-shelf 433 MHz sensor devices that I want to include in my home automation system. As they use proprietary protocols for communication, I looked for different solutions for parsing their data and finally settled on RFXtrx433E which is a 433 MHz transceiver that can read and write a set of different protocols.

My new sensors

The new sensors that I want to include in my system are a Telldus motion sensor and a Telldus outdoor temperature/humidity sensor. I also have an old no-name thermometer that I want to add to my system.

new_433_sensors.jpg
Telldus motion sensor, Telldus outdoor temperature/humidity sensor and a no-name thermometer

Getting started with Rfxtrx433E

RFXtrx433E is a USB-attached device that supports a wide range of 433.92MHz protocols for incoming- and outgoing data.

rfxtrx433e_image.jpg
RFXtrx433E

For reducing noise when reading data, it is preferable to activate only the protocols that are needed in your setup. To change active protocols and also test what works with your devices, rfxcom’s RFXmngr has to be used. It can be downloaded from rfxcom’s site http://www.rfxcom.com/.

Unfortunately it is a Windows-only application, so I had to boot up an old Windows PC to configure my RFX-device. I started out with all protocols activated and noted in rfxmngr what protocols and ID:s my new sensors used (by watching the log window when my devices transmitted data). I came up with these protocols and ID:s:

rfx_types.png

rfxmngr_screenshot

After reducing the number of active protocols to these ones and storing the settings on the device, I could move the RFXtrx433E to my Raspberry Pi / Linux environment.

Using the rfxtrx platform in Home Assistant

After attaching the RFXtrx433E to my Raspberry Pi, my first experiment was to let Home Assistant read from the device via USB. I set up the rfxtrx platform according to:

https://home-assistant.io/components/rfxtrx/

and added some rfxtrx sensors (for temperatures and humidity) and one binary sensor (for the motion sensor).

It worked fine, but as I want the data and events to also go into other destinations like a database, I figured it would be better to have the RFXtrx433E messages transformed into MQTT messages first and then let Home Assistant and other applications subscribe to these messages. How to achieve this? I found a solution with Node-RED, see below.

Using Node-RED for transforming RFXtrx433 messages to MQTT

Node-RED is a NodeJS-based platform that can be tailored for a lot of different purposes. It is very useful for transforming data from one format to another and let data go in different directions depending on conditions that you define. Node-RED is easy to install on a Raspberry Pi (see https://nodered.org/docs/hardware/raspberrypi) and it can be configured via a browser on port 1880 (default).

Node-RED is configured with “flows” that typically reads data from a set of sources, checks some conditions, transforms the data and outputs it to different destinations. I want to transform data from RFXtrx433E to MQTT. It turns out that there is a perfect Node-RED package (by Maxwell Hadley) for this purpose:

https://www.npmjs.com/package/node-red-contrib-rfxcom

With this package installed for Node-RED, I was able to create a flow that transforms the data from my two climate devices (temperature, temperature+humidity) and my motion detector to MQTT messages:

433MHz_to_MQTT

Node-Red flow details

rfx-sensor read

This node reads from the rfxtrx device connected to the Raspberry pi. The msg.payload object will contain a temperature and a humidity property for my Telldus outdoor sensor and a temperature object for my no-name sensor. The msg.topic property is the name and ID of the device. (The structure of the rfxtrx objects can be investigated by adding a debug node to the rfx-sensor read node and checking the logs)

rfx-sensor-read.png

Route RFX to MQTT

This node routes the data to different nodes depending on the topic (which is the name and ID of the device):

route-rfx-to-mqtt.png

Create message and topic

These nodes takes the sensor value and sets it as msg.payload. The msg.topic is set to the desired MQTT topic. For the Telldus outdoor temperature, the node would be configured like this:

create-message-and-topic.png Publish MQTT package

This node uses the msg.topic and msg.payload (from the previous node) as MQTT topic and MQTT message and publishes it to an MQTT broker (my broker is on IP address 192.168.1.16 with the default port 1883):

publish_mqtt_message.png

rfx-lights

The Telldus motion sensor sends on-events via a Lighting protocol. I thus use the rfx-lights input node to read the data:

rfx-lights.png

Check if porch motion sensor

To check that the on-event is actually from my device, I setup a switch node that only routes the message if it matches my device.

check_if_porch_motion_sensor.png

Increase motion count as msg.payload

Home Assistant registers only changes in values and to make a new motion detection appear properly in HA, I have to do a special treatment of the data: Instead of sending  “on” from the motion sensor, I send a new value for each detected on-event. This can be achieved with a JavaScript-node in Node-RED:

increase_motion_count_as_msg_payload.png

A counter in the flow is used for creating messages with values from 0-100 (and then the counter starts from 0 again).

Create topic

As the message for the motion sensor was created in the previous node, I only have to set the proper topic before publishing the MQTT message:

create_topic.png

Testing the setup

The setup can be tested by using mosquitto_sub on a computer on the local network (where the -h switch specifies the ip-address where the mosquitto broker is running):

mosquitto_sub -h 192.168.1.16 -t "Home/#" -v

This command will subscribe to all MQTT topics starting with Home/ and outputs the topic and value when a message is received. With my sensors active, it should produce an output similar to:

Home/Porch/Temperature -2.6
Home/Outdoor/Temperature -3.6
Home/Outdoor/Humidity 82
Home/Porch/Motion 3

Configuring Home Assistant with MQTT sensors

I removed the rfxtrx-platform configurations from Home Assistant and added some MQTT sensors instead:

I use customization to give the entities dedicated icons and friendly names for the display:

so that they look nice in my Outdoor group:

new_mqtt_sensors.png

For the motion sensor to display when the last motion was detected, I use a template sensor:

It will appear as:

porch_last_motion.png

Storing the new MQTT messages in a database and viewing them with Grafana

In InfluxDB and Grafana for sensor time series I have described how I store all “Home/” messages in InfluxDB so that I can view them via Grafana. With my new topics available in the database, I can just add some new graphs to Grafana:

outdoor_temp2.png

porch_motion_events2.png

Routing the new MQTT messages to a cloud service

In A self-hosted MQTT environment for Internet of Things – Part 3 I have shown how I route MQTT message to the Adafruit IO cloud service. I can simply modify my routing rules to send my new topics to Adafruit IO. With Adafruit IO I can connect to IFTTT and build event chains depending on the data content.

Going further

As the RFXtrx433E is a transceiver, it can also be used for sending commands. I have created a new Node-RED flow for controlling a set of Telldus 433 MHz outlets via MQTT. The Node-RED flow is available from my GitHub repository.

MQTT_Telldus_Outlets.png

Configurations

My Home Assistant and Node-RED configurations are available from GitHub:

https://github.com/LarsBergqvist/Home-Assistant_configuration

https://github.com/LarsBergqvist/node-red-flows

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s