[SOLVED] How to publish to mqtt topic with jython

Help will be appreciated

Thx

People would like to help but you make it very hard for them… please consider reading this topic.

@BrutalBirdie Thx, I thought the the topic is enough

I want to be able to publish a message to MQTT topic (py rule). I don’t know which function and from which package I need to import the function. E.g., for HTTP I do the following:

from org.eclipse.smarthome.model.script.actions.HTTP import sendHttpGetRequest

sendHttpGetRequest("http: ...)

What is the equivalent for MQTT?

Thx

I don’t use jython but maybe this link will help.

@H102

I have this code and they show how to work with actions but the MQTT is missing there and I currently cannot find how to do it. Still searching …

Thx

Is this what your are looking for? See the paragraph called Rule Actions:

@hilbrand

Not sure, there are no instructions there what you need to import in order to use it in Jython script. It also somehow complicate things as this is the new MQTT binding (AFAIK) and I am still using MQTT version 1.

Not tested but this could looks like the import:

from org.eclipse.smarthome.model.script.actions.Things import getActions

Also you can install both version 1 and 2 of mqtt next to each other. You only need to configure a generic bridge/thing. I think that might work.

For mqtt1 actions you need the mqtt1 actions binding installed. Have a look at that bindings documentation for how it would work in DSL rule files and that should give you a hint for how to do it in python syntax.

Thx all,

From the hints above I was able to use MQTT action from Jython script:

  1. Install MQTT action (this what I was missing, thx @David_Graeff)

  2. Import the action:
    from core.actions import Mqtt

  3. Call it
    Mqtt.publish(String brokerName, String topic, String message)