Fire a rule with Channel Trigger on MQTT Binding

Hi,
I am runniing OH 3.3.0. I have an MQTT Thing defined like this:

Thing mqtt:topic:zigbee2mqtt:button:Xiaomi_Test "Zigbee - Button Test" (mqtt:broker:myMQTTBroker) {
    Channels:
            Type string : click "click" [ stateTopic = "zigbee2mqtt/Xiaomi_Button_Test/action" ]
            Type number : voltage "voltage" [ stateTopic = "zigbee2mqtt/Xiaomi_Button_Test/voltage" ]     
            Type number : battery "battery" [ stateTopic = "zigbee2mqtt/Xiaomi_Button_Test/battery" ]     
            Type number : linkquality "linkquality" [ stateTopic = "zigbee2mqtt/Xiaomi_Button_Test/linkquality" ] 
} 

click contain the action and it could be: single, double…

Rule is:

rule "Test Zigbee"
when
    Channel "mqtt:topic:zigbee2mqtt:button:Xiaomi_Test:click" triggered
then
  	logInfo("Zigbee.Xiaomi.Button.Test","Start -> Action Name: single")
end

Rule is not fire when I press Button, but linking click channel to an Item, item will be updated, so MQTT is configured fine.
I alsto tried:

Channel "mqtt:topic:zigbee2mqtt:button:Xiaomi_Test:click" triggered single

without success.
is there something missing?

Thanks

I don’t do file based configs for Things but I’m pretty sure a trigger Channel needs to be defined and it’s of type trigger. Your click Channel is of type String.

1 Like

Ok I will check.
Thanks

Hi,
for future reference here is the code:

Thing mqtt:topic:zigbee2mqtt:xiaomi:button:Xiaomi_Test "Zigbee - Button Test" (mqtt:broker:myMQTTBroker) {
    Channels:
            Type trigger : click "click" [ stateTopic = "zigbee2mqtt/Xiaomi_Button_Test/action" ]
           } 

@rlkoshak I have another question, because I do not find a solution, I use this code to get event name:

rule "Test Zigbee"
when
    Channel 'mqtt:topic:zigbee2mqtt:xiaomi:button:Xiaomi_Test:click' triggered
then
	var actionName = receivedEvent.getEvent()
  	logInfo("Zigbee.Xiaomi.Button.Test","Start -> Action Name: "+actionName)
end

I got error in the logs

Script execution of rule with UID 'xiaomiSwitch-1' failed: The name 'event' cannot be resolved to an item or type; line 17, column 19, length 5 in xiaomiSwitch

Is there a way to get event into the rule, without create one rule for each event?
Thanks

According to the UI, the event should be passed and it should contain the MQTT topic that the Channel subscribes to. If you define a separator character (e.g. #) it will be the topic#message.

I think the variable is just receviedEvent. Rules | openHAB which is a String. At least that is how it sort of looks in JS rules.

1 Like

Hi @rlkoshak
many thanks, with this code, I have the clean event into a variable:

rule "Test Zigbee"
when
    Channel 'mqtt:topic:zigbee2mqtt:xiaomi:button:Xiaomi_Test:click' triggered
then
	var actionName = receivedEvent.toString
  	logInfo("Zigbee.Xiaomi.Button.Test","Start -> Action Name: "+actionName)
end