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

**URL:** <https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843>\
**Category:** Runtime\
**Tags:** mqtt\
**Created:** [September 2, 2017, 12:07am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843 "2017-09-02T00:07:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![psyciknz](https://community.openhab.org/user_avatar/community.openhab.org/psyciknz/32/17347_2.png) [@psyciknz](https://community.openhab.org/u/psyciknz)\
**Post date:** [September 2, 2017, 12:07am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843/1 "2017-09-02T00:07:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ben\_jones12](https://community.openhab.org/user_avatar/community.openhab.org/ben_jones12/32/52_2.png) [@ben\_jones12](https://community.openhab.org/u/ben_jones12)\
**Post date:** [September 2, 2017, 12:10am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843/2 "2017-09-02T00:10:36Z")

</div>

```auto
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]" }

```

---

<div class="post-metadata">

**Author:** ![ben\_jones12](https://community.openhab.org/user_avatar/community.openhab.org/ben_jones12/32/52_2.png) [@ben\_jones12](https://community.openhab.org/u/ben_jones12)\
**Post date:** [September 2, 2017, 12:12am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843/3 "2017-09-02T00:12:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![psyciknz](https://community.openhab.org/user_avatar/community.openhab.org/psyciknz/32/17347_2.png) [@psyciknz](https://community.openhab.org/u/psyciknz)\
**Post date:** [September 2, 2017, 12:12am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843/4 "2017-09-02T00:12:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ben\_jones12](https://community.openhab.org/user_avatar/community.openhab.org/ben_jones12/32/52_2.png) [@ben\_jones12](https://community.openhab.org/u/ben_jones12)\
**Post date:** [September 2, 2017, 12:14am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843/5 "2017-09-02T00:14:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![psyciknz](https://community.openhab.org/user_avatar/community.openhab.org/psyciknz/32/17347_2.png) [@psyciknz](https://community.openhab.org/u/psyciknz)\
**Post date:** [September 2, 2017, 12:19am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843/6 "2017-09-02T00:19:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![psyciknz](https://community.openhab.org/user_avatar/community.openhab.org/psyciknz/32/17347_2.png) [@psyciknz](https://community.openhab.org/u/psyciknz)\
**Post date:** [September 2, 2017, 12:28am UTC](https://community.openhab.org/t/can-i-publish-to-mqtt-from-either-a-rule-or-item-state/33843/7 "2017-09-02T00:28:43Z")

</div>

![IMG_8032](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/5/5dee3cd93edc0996010602aed71ef39a2283716e.jpg)

And done.
