In the article RaspberryPi - Install MQTT Server on the RaspberryPi I had written a little about MQTT and explained how to install an own MQTT server on the RaspberryPi.
In order to be able to use an MQTT server from FHEM, you have to install a few FHEM modules. How to do that and what you have to pay attention to I have described in the following article.
Safety instructions
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/
Affiliate links/advertising links
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
Requirements
Before you start with this article you should have prepared the RaspberryPi so that it is accessible via the network and controllable via SSH.
The following three articles describe what needs to be done to prepare the RaspberryPi:
RaspberryPi – setup for nerdiys!
RaspberryPi – The first configuration!
RaspberryPi – Control the RaspberryPi via SSH
Your own smart home - installation and getting started with FHEM
Mosquitto on the Raspberry - Install MQTT Server on the RaspberryPi
Tools needed:
-no-
Materials needed:
In the following list you will find all the parts you need to implement this article.
Log in to the RaspberryPi via SSH
To start, you must first log in to the Rasp Pi with Putty via SSH. How to do this is in the article RaspberryPi - Control the RaspberryPi via SSH described.
Update package management
The package management in Linux is a "central place" through which various software packages can be installed. In order for this to work reliably, the lists and sources of the package management should be updated before each installation of new packages.
sudo apt-get update && sudo apt-get upgrade
Install Perl MQTT packages
In order for FHEM to communicate with the MQTT server, two Perl packages must be installed.
sudo apt-get install libmodule-pluggable-perl && sudo cpan install Net::MQTT:Simple && sudo cpan install Net::MQTT:Constants
The installation of the two packages took me about five minutes. So time enough to get a new coffee 🙂 .
Create MQTT clients
In order to establish a connection with an MQTT device in the next step, an MQTT client must first be created. This will later save the necessary data for the connection with the MQTT server.
define mqttClient MQTT 192.168.0.1:1883
The command to create an MQTT client corresponds to the following “define name-of-mqtt-client MQTT host-or-ip-of-mqtt-server:port-of-server”. If your MQTT server has the IP address “192.168.0.1”, the port 1883, which corresponds to the standard port for MQTT), the command to create the MQTT client is “define mqttClient MQTT 192.168.0.1:1883”. Of course, any other name can be used for “mqttClient”.
shutdown restart
After the server is restarted, you are ready to create new MQTT bridges and devices.
Setting up the MQTT Bridge using the example of a MAX! heating thermostat
In this example, we create an MQTT bridge. As the name suggests, the MQTT bridge connects something to each other. In this case the MAX! heating thermostat with the MQTT world. In principle, this link works according to the same scheme with all already installed sensors or actuators. In the case of the MAX! heating thermostat, this works with the following command.
defineHeatingWorkroomMqtt MQTT_BRIDGE MAX_15504f
This creates the module “HeatingWorkroomMqtt” of type “MQTT_BRIDGE” and links it to the already existing module “MAX_15504f”.
There is also the attribute “publishReading_XYZ”. This causes the MQTT bridge to automatically forward all updates on the reading “XYZ” to the specified topic.
attr HeizArbeitszimmerMqtt publishReading_temperature heizung/Arbeitszimmer/temperature
Make sure that “publishReading” and “temperature” (the reading of the radiator thermostat (or any other sensor/actuator)) must be connected with an underscore. This results in “publishReading_temperature”.
For the reading “Battery” the whole thing would look like this.
attr HeizArbeitszimmerMqtt publishReading_battery heizung/Arbeitszimmer/battery
attr HeizArbeitszimmerMqtt subscribeSet_desiredTemperature heizung/Arbeitszimmer/desiredTemperature
This causes the topic “heating/study/desiredTemperature” to be subscribed to and every value received from it is automatically entered into the reading “desiredTemperature”. In this way you can control the desired temperature of the heating thermostat via MQTT message.
Testing the set MQTT bridge
One way to test the freshly created MQTT bridge is described here.
For this purpose I built a mini MQTT viewer in NodeRed, which shows you the values that are sent to the set topics. “heating/study/temperature” and “heating/study/battery” can be sent. You also have the possibility to send to the topic “heating/study/desiredTemperature” to send the value 25. This has the consequence that - if everything works correctly - the linked radiator thermostat is set to 25°C.
… finds “desiredTemperature”. This is the reading that we have in the configuration of the MQTT bridge with the MQTT topic “heating/study/desiredTemperature” have linked. Here you will also see the current value of the reading and the time of the last update.
If you click on the Inject-Node in the self-built MQTT-Viewer you send the value 25 to the Topic “heating/study/desiredTemperature”. So this should now also update the reading of your heating thermostat to the value 25. In addition, the time of the last update is set to the current time.
To test the opposite way - i.e. the publication of a changed reading on an MQTT topic - just scroll to the top of the detailed view of the MAX! heating thermostat. There you change the desired temperature of your heating thermostat as shown.
Creating an MQTT device using the example of a temperature sensor
Another possibility of the MQTT module is the creation of MQTT devices. This includes all sensors or devices that send their measured values directly via MQTT. In order to collect these values and combine them in a virtual sensor, you must create an MQTT device.
In this example, we have a temperature sensor in our network that sends the measured temperature value at regular intervals via the MQTT topic “ambient data/basement/temp”. In order to record this, an MQTT device is first created using the following command.
define kellerSensor MQTT_DEVICE
Before this MQTT_Device can display values, it must of course first be linked to the desired MQTT topic. In this example, we want to display all incoming values on the MQTT topic “Environmental data/cellar/Temp” in the created MQTT device.
To do this, you need to set the following attribute:
attr kellerSensor subscribeReading_Temp Ambient Data/keller/Temp
This causes the reading “Temp” to be created in the created MQTT_Device “kellerSensor” and linked to the subscribed topic “Environmental data/keller/Temp”.
To make the view even more pretty, you can use the following attribute to specify that a “°C” should be appended to the reading temp value.
attr kellerSensor stateFormat Temp °C
More information
https://wiki.fhem.de/wiki/MQTT_Einf%C3%BChrung
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂
Hi Fab
Thank you so much for all of the information you have made available here. I have spent many years with home assistant, and no time at all with FHEM which I am now using to try to get better control of my FHT heating devices. If I can get FHEM communicating with an existing MQTT server that would be my dream.
I have FHEM running on Raspian OS within a docker container, the install went as per your advice with no errors – but when I try to enter the commands via FHEM web front end I get Cannot load module MQTT_BRIDGE or Cannot load module MQTT_CLIENT which makes me think either I've messed up not installing the perl/mqtt from within the docker container or I've not done something within FHEM to invoke the new MQTT capability?
Kind regards
Nick