# OH3: MQTT publish via ECMA script?

**URL:** <https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252>\
**Category:** Off-Topic\
**Created:** [June 6, 2021, 6:48am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252 "2021-06-06T06:48:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Whitehouse](https://community.openhab.org/user_avatar/community.openhab.org/george_whitehouse/32/41305_2.png) [@George\_Whitehouse](https://community.openhab.org/u/George_Whitehouse)\
**Post date:** [June 6, 2021, 6:48am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/1 "2021-06-06T06:48:49Z")

</div>

Hi community,

Could someone better than I point me in the right direction.

I would like to publish some MQTT via an ECMA script but have got stuck. I have the broker & binding working together.  
(thrilled to have my iPhone reading an NFC and Shortcuts/EasyMQTT sending a MQTT to my broker/OH item)

Have the below but it is not working.

```auto
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Experiments");  
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");   

logger.info("MQTT published...")
getActions("mqtt","mqtt:systemBroker:openHab").publishMQTT("test-topic", "test-data", true);

```

Kind Regards,  
George

---

<div class="post-metadata">

**Author:** ![chrismast](https://community.openhab.org/user_avatar/community.openhab.org/chrismast/32/66147_2.png) [@chrismast](https://community.openhab.org/u/chrismast)\
**Post date:** [June 6, 2021, 7:18am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/2 "2021-06-06T07:18:31Z")

</div>

Just posted yesterday…[Link](https://community.openhab.org/t/oh3-mqtt-topic-to-an-item-ecma-script/123223/2)

---

<div class="post-metadata">

**Author:** ![George\_Whitehouse](https://community.openhab.org/user_avatar/community.openhab.org/george_whitehouse/32/41305_2.png) [@George\_Whitehouse](https://community.openhab.org/u/George_Whitehouse)\
**Post date:** [June 6, 2021, 7:23am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/3 "2021-06-06T07:23:49Z")

</div>

Hi Chris,

Thanks for that, I got the channel to read from the broker.

To publish can I do it from a rule directly to the broker?  
Or does the rule need to update an item that is linked to a channel of the MQTT thing?

Cheers,  
George

---

<div class="post-metadata">

**Author:** ![chrismast](https://community.openhab.org/user_avatar/community.openhab.org/chrismast/32/66147_2.png) [@chrismast](https://community.openhab.org/u/chrismast)\
**Post date:** [June 6, 2021, 7:24am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/4 "2021-06-06T07:24:27Z")

</div>

To my understanding, at least for all of my devices, it goes through items.

---

<div class="post-metadata">

**Author:** ![George\_Whitehouse](https://community.openhab.org/user_avatar/community.openhab.org/george_whitehouse/32/41305_2.png) [@George\_Whitehouse](https://community.openhab.org/u/George_Whitehouse)\
**Post date:** [June 6, 2021, 7:25am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/5 "2021-06-06T07:25:00Z")

</div>

Ok no worries, I will give it a try.

Cheers

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [June 6, 2021, 7:25am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/6 "2021-06-06T07:25:16Z")

</div>

If it is a MQTT item just send the command:

//below is how to send a command to an item  
events.sendCommand(‘CoffeeMachine\_CoffeeMachine’, ‘OFF’);

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 7, 2021, 2:07pm UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/7 "2021-06-07T14:07:47Z")

</div>

> [@chrismast](#):
>
> To my understanding, at least for all of my devices, it goes through items.

The MQTT binding has an Action as shown in the original post. The only problem with that code is that in JavaScript (and other non-Rules DSL languages) one can’t just call a random OH function like getActions. One has to reference the Object that implements the function.

```
actions.get("mqtt", "mqtt:systemBroker:openhab).publishMQTT(...

```

---

<div class="post-metadata">

**Author:** ![system](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/6/6a52f2ac5d00e4c7b50471a354b3a9221188be90.png) [@system](https://community.openhab.org/u/system)\
**Post date:** [July 19, 2021, 6:08am UTC](https://community.openhab.org/t/oh3-mqtt-publish-via-ecma-script/123252/8 "2021-07-19T06:08:48Z")

</div>

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