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)?
Hehehe my master Mr. @rlkoshak nice to heare you as always in hard topics
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?
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.
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.