How to get "MQTT State Topic" of thing's channel in DSL script?

Hello,

OH - 4.1.1, platform Windows 11.

Quick question to experts of OH - is there possible to get the parameter “MQTT State Topic” which has been defined for the channel of thing of course on DSL script level?

Could you help me and give a example line of code (or method)?

Best regards. Radek

This seems like an XY Problem.

Why?

Hehehe my master Mr. @rlkoshak :slight_smile: nice to heare you as always in hard topics :slight_smile:

To be more precisely:

  • I have the thing (wall switch) with 1-gang
  • this gang has channel with defined:
    a. MQTT State Topic as “stat/tasmota_34DAFA/POWER1”
    b. MQTT Command Topic as “cmnd/tasmota_34DAFA/POWER1”

I would like to get in the DSL script MQTT State Topic (“stat/tasmota_34DAFA/POWER1”) or Command Topic (“cmnd/tasmota_34DAFA/POWER1”).

var varMQTTPath = triggeringItemName.***someMethodToGetMQTTCommandTopic***; //should give me "cmnd/tasmota_129640/POWER1";
or 
triggeringItemName.***someMethodToGetMQTTStateTopic***;//should give me "state/tasmota_129640/POWER1";
varMQTTPath = "cmnd/" + varMQTTPath.substring(5,varMQTTPath.length); //after string modyfication - expected result should be "cmnd/tasmota_129640/POWER1"

mqttActions.publishMQTT(varMQTTPath , "OFF");

So finally - is there possible to get the “MQTT Command Topic” from the channel of the item?

Indeed this is an XY problem.

In jruby you can get the stateTopic / commandTopic like this

rule "some rule" do
  # some trigger here
  run do |event|
    mqtt_path = event.item.channel.configuration["stateTopic"] # or "commandTopic" as needed
  end
end

However, looking at what you want to actually do, it is not the way to do it. You shouldn’t go down to MQTT level at all.

You would just send a command to the item, e.g. MyItem.sendCommand(OFF) and the binding will take care of it.

You’ve gone through the bother of setting up the Thing / channels / mqtt topics so you do not have to deal with that again in your rules just to send a simple command.

1 Like

Hello @JimT ,

thank you for the answer. I’m not using Jruby, I’m looking for DSL script. Will be it the same?

You right about MQTT, very good tip aboout MQTT, I should use sendCommand, I don’t know why I was focused on MQTT…

Thank you for your answer and tip! Have a nice day.

It’s all done inside jruby’s helper library to make it easier to use.
Doing it in DSL will be a pain / long.

Thank you so much @JimT ! Your knowledge and expirience is priceless!

Have a nice weekend!

Just to elaborate a tiny bit on @JimT 's answer to this, you do not have access to the ThingsRegistry in Rules DSL so you’d have to use the REST API and HTTP calls to get at the info.

jRuby makes this much easier. I’m not sure about JS Scripting though because, as has been said, you would never really need to do that for a use case like this. The while point of Items is to be a normalized interface between you devices and OH. You should almost never have to deal with the underlying technology at the rules level. Just send command to Items.