MisterHouse is an active, community-driven, open source home automation project
example.mht
The local/code/example.mht'
file I've provided contains a great deal more than you will need. Most of the file is lines of comments. At the start is a Format = A
and a #
noloop=start
statement. These statements tell MisterHouse two important things. First is that MisterHouse will treat this mht
file as a Format A file (there used to be an Format = XML
option also). The noloop
statement is the more important statement. As I stated earlier, MisterHouse is a large loop that runs code over and over again.
At times, you might want to run a series of commands only once. When that occurs, you can put the statements #
noloop=start
and #
noloop=stop
around the code to tell MisterHouse not to include that code in the loop section but, instead, run it once at startup (and restart). I have trimmed the example.mht
to include a noloop
section.
Note the noloop
and Format
statements in Listing 2. Lines that start with #
are comments, and the lines that start with the keyword CODE
contain Perl code. Future releases of MisterHouse will integrate automatic code generation (.mht
to .mhp
) of special MQTT statements into MisterHouse.
Listing 2
A Section of example.mht
I have required the Perl module mqtt.pm
, then initialized the MQTT interface with the subscription to the topic home/weather/#
(MQTT wild card) and set the Perl object $weatherSummary
to the home/weather/txt/summary
topic. I've done something similar for $weatherWind
, but for the topic home/weather/txt/wind
. These are the two objects that I will use later in my user code. For further information on the mqtt
module, visit MisterHouse MQTT web page [5]. In this particular example, the interface $mqtt_1
subscribes to the wild card topic home/ha/#
. The MQTT items $weatherSummary
and $weatherWind
variables correspond to the topics home/weather/txt/summary
and home/weather/txt/wind
, respectively. When either topic gets updated, MisterHouse gets those updates and sets the respective objects to the update values. Your code can then take those values and perform some action.
User Code: mqtt.pl
My user code uses a few built-in MisterHouse routines. If you visit the URL: http://IP_Address:8080/docs/index.html, click on the Doc link and you'll be taken to the web page with the information about these routines for handling events. One of those routines is the state_now()
, which returns the current state only for one pass after the object state is set. This approach is important, because you often only want to run some code when the state of an object has changed and not on every pass through the loop. Other routines can check for a new second, new minute, and new hour. MisterHouse provides a dizzying catalog of routines.
The user code in Listing 3 is not too complex. The noloop
statements are around the code that requires the use of JSON. A noloop
statement tells MisterHouse to keep the code out of the main loop and run it only once. I'm taking advantage of one of Perl's strengths, its libraries: In this case, the JSON library to decode the expected JSON string in the topic. The if()
condition checks for when the $weatherJSONSummary2
object has be updated and, if true, then decodes the JSON and sets the Wind
and Summary_short
variables in the Weather object accordingly. Much of the user code looks like this: a simple if
-then
-else
statement.
Listing 3
A Section of mqtt.pl
Publishing to a MQTT Topic
Publishing to a topic is really easy (see Listing 4). The two important options for the mosquitto_pub
command are -t
topic and -m
message. In this case, my code in the mqtt.pl
file expects a JSON string in the format shown with the -m
option. I could have just as easily used XML or plain text. The reason I chose to use JSON was that it was delivered from another source to MQTT in JSON. So, in my user Perl code, I needed to decode the JSON. If you want to publish the data to the MQTT topic, you need to send valid JSON.
Listing 4
Command-Line Publishing
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.