- Platform information:
- Hardware: Raspberry Pi 3 B+, 120gb mSATA SSD in a USB enclosure
- OS: Raspbian Stretch
- Java Runtime Environment: 1.8.0_212
- openHAB version: 2.4.0 release build. Jython 2.7.0
Howdy!
Experience level is as follows:
C++ programmer: 20+ years
Arduino/ESP8266: 2 months
openHAB: 3 weeks
JSR223/Jython: 2 weeks
MQTT: 1 day
So. I have a whole bunch of wall-mounted 433mhz remotes, like this one:
I’ve written some code to make the most of them. For example, my code can reliably count pulses to multiply functionality, for example push the same button 3 times for action number 3.
My weekend project this time was to bring my kitchen ceiling fans into the home automation system so that I can use these wall-mounted buttons rather than the fiddly infrared remote that came with the fans.
After first unsuccessfully trying to follow a hopelessly confusing openHAB + ESP8266 + MQTT tutorial, I decided to break up the problem into smaller parts, and learn to understand MQTT outside of openHAB, first using MQTT.fx and then MQTT Explorer to monitor messages.
With my limited understanding of topics and payloads, I set it up with one topic per button, like so:
Pressing the left button yields: topic [payload]
home/mcu/LivingRoomMCU/433mhz/KitchenFan1 [0]
Middle button:
home/mcu/LivingRoomMCU/433mhz/KitchenFan2 [0]
Holding the middle button in for more than half a second yields:
home/mcu/LivingRoomMCU/433mhz/KitchenFan2 [1]
It seemed like a good idea.
Then, once I tried to bring openHAB into the picture, it seemed too “piecemeal” to have to create a separate channel AND a separate item for each.freaking.button (since each button had its own topic), so I looked around for other solutions and found:
Number temperature "temp [%.1f]" {mqtt="<[publicweatherservice:london-city/temperature:state:default]"}
Perfect, I thought! Defining them in an file is okay, I can live with one line per button… Or better yet I can use wildcards. And then I spent a couple of hours trying to get it to do anything at all (no error messages in the console either) until I understood that this was the Openhab 1.x way of doing things, and will not work in Openhab 2.x.
You can of course subscribe to more than one topic in an OpenHAB 2.x MQTT channel, by using wildcards… but then you won’t know what topic caused the channel to be triggered.
I found this workaround which will probably work… but now it’s starting to feel really clunky.
I mean, it is my understanding that MQTT is supposed to be flexible and lightweight.
If I have to subscribe to every single topic and filter in a script, that’s not going to be very efficient.
I could split my topic and payload diffently… for example:
home/mcu/LivingRoomMCU/433mhz/KitchenFan [Button1_Pressed]
home/mcu/LivingRoomMCU/433mhz/KitchenFan [Button2_Held]
But, it seems like a rather arbitrary limitation to not get the topic as part of the event in a normal channel subscription.
It’s probably just me trying to use it in a weird way.
So, would you please enlighten me, how would you do it? What is the recommended way?
How would you structure a remote with multiple buttons where each button has multiple functions into MQTT topics + payloads, for it to work well with openHAB?
How do the people who designed the MQTT binding for openHAB 2.x use it, to have made the design decisions they made?
Would appreciate any insight/help. Thanks in advance.