Best way to mqtt publishing a switch in OH3?

Hi all,

I am looking for the most efficient way to publish a virual switch via MQTT in openhab.
Currently I followed: openHAB 3.0 - how to create a switch that publishes its state via MQTT messages | Manuel Munier's Homepage
which basically means I created a virtual item switch and defined two rules to send a “ON” and a “OFF” command via mqtt.

This works, but I want to add my switch to my model/location in OH3. How would I do that?

Or is there even a more elegant way to publish a switch via MQTT in OH3?

Thanks for your feedback.

You definitely don’t need to use rules just to send ON/OFF from a Switch Item through the MQTT Binding - you can do this via the native Things Channels - check out steps 4 & 5 here, modifying the topic to suit your setup.

Thanks a lot, this helped my out and is much more elegant!

I am just facing one more “issue”. I added the “Availability Topic”, but if my mqtt client (in my case an esp32) turns off without notification there is no change in the status of my thing in openhab. Is there a way to define a timeout when a thing is considered to be offline?

What is your device? What code or program are you running on it? Whatever it is needs to make sure that it correctly sets up the Last Will and Testament (LWT) topics and messages with the MQTT broker.

Currently I am programming an “ESP32 Dev Kit C V4 NodeMCU” by myself and I did not look into Last Will and Testament topic yet.
Would this still be send before power off?
Or is the last will stored in the MQTT Broker and the broker handles the timeout?

I am new to MQTT and this is my first project :slight_smile: so not all things are fully clear to me yet.

Yep, this one. When your device connects it will setup the LWT topic, and also tell your broker what to publish when your broker can no longer see your device.

Check out this page for an overview of LWT:

1 Like

Perfect, thanks again!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.