Can I publish to MQTT from either a rule or item state?

I’m pretty happy with MQTT for incoming information, and sending commands.

But I’d like to publish something back to the MQTT server. States, or warning messages etc.

How can I do this?

String      VT_Notify_Trace                     { mqtt=">[mosquitto:/openhab/notification/trace:state:*:default]" }
String      VT_Notify_Info                      { mqtt=">[mosquitto:/openhab/notification/info:state:*:default]" }
String      VT_Notify_Alert                     { mqtt=">[mosquitto:/openhab/notification/alert:state:*:default]" }
String      VT_Notify_Warn                      { mqtt=">[mosquitto:/openhab/notification/warn:state:*:default]" }
String      VT_Notify_Alarm                     { mqtt=">[mosquitto:/openhab/notification/alarm:state:*:default]" }

Then to publish an alert you just do VT_Notify_Alert.postUpdate("Front door has been left open for 30mins!")

Ahh right, so in a rule, I go
VT_Notifity_Trace.postUpdate("Notification message")
and that will push "Notification Message to mqtt on topic /openhab/notification/trace

Correct - then you setup your various mqttwarn targets to watch the different notification topics, and route the messages to the appropriate end points and devices.

Means very easy to add a new device or remove one, or add a new notification type, without having to touch any rules.

That’s really elegant. And then it’s into MQTTWarn for processing. And in my case I have my boxcar code…since I still have the app, and the code I wrote for it.

IMG_8032

And done.

1 Like