OH3: MQTT publish via ECMA script?

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.

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

Just posted yesterday…Link

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

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

Ok no worries, I will give it a try.

Cheers

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’);

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(...
2 Likes

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