[SOLVED] MQTT channel id in a rule (it doesn't trigger)

  • Platform information:
    • Hardware: Raspberry Pi 3
    • OS: Raspbian
    • openHAB version: 2.5 M3-1
  • Issue of the topic:rule doesn’t trigger
    Hi, despite to some well explained examples about triggering channel in Cube rule with Xiaomi Gateway binding I didn’t discovered the same for a MQTT (zigbee2mqtt) based solutions

It seems channel id being not well specified (in the gateway bind, a serial number is required not present in the generic mqtt thing

So I wonder if I correctly specify the channel for triggerring the associated event

  • Items configuration related to the issue
    mqtt.items&thing
String AqaraCubeAction         "Cube action [%s]"              <action>    (gAqaraCube) { channel="mqtt:topic:mosquitto:testthecube:action" }

MQTT.Thing
Thing topic testthecube "Aqara Cube Test" {
    Channels:
        Type string : action "Action"       [ stateTopic="zigbee2mqtt/testthecube", transformationPattern="JSONPATH:$.action"]
        Type number : voltage "Voltage"     [ stateTopic="zigbee2mqtt/testthecube", transformationPattern="JSONPATH:$.voltage"]
        Type number : battery "Battery"     [ stateTopic="zigbee2mqtt/testthecube", transformationPattern="JSONPATH:$.battery"]
        Type number : linkquality "RSSI"    [ stateTopic="zigbee2mqtt/testthecube", transformationPattern="JSONPATH:$.linkquality" ]
    }


```csv

rule "Xiaomi Cube"
when
Channel "mqtt:topic:mosquitto:testthecube:action" triggered
then
var actionName = receivedEvent.getEvent()
...
//    logInfo("xiaomi","action is {}",actionName)                 //<---
    logInfo( "CUBO", "INFO: Channel Event = '{}'", actionName )

This binding may not have channel triggers, not all bindings have
Link an item to the channel and change your rule trigger to:

Item MyItem received update
1 Like

Thanks I give it a try
The purpose is to manage a switch/case statement evidentley

Anyway can you better explain the concept?
MQTT bind doesn’t support this kind of construct (triggering channel) ?

It does.
I found this…

Sorry Vincent to bother you again but this is just my problem
the example you submit is the one where I started from but the difference is that I use a MQTT generic item not as specific as the MIHOME bind that refer its own channel id format

So again, can I trigger a MQTT channel event? Is my channel id correctly expressed in my rule (I assume the channel ID is ‘mqtt:topic:mosquitto:testthecube:action’

If both assumptions are YES then why my rule doesn’t trigger?

Quoting the Channel Configuratuon documentation:
* **trigger** : If true, the state topic will not update a state, but trigger a channel instead.
Did you set trigger to true?

2 Likes

So I will try tonite when at home this configuration, and I’ll let you know

Thing topic testthecube "Aqara Cube Test" {
    Channels:
        Type string : action "Action" [ stateTopic="zigbee2mqtt/testthecube", transformationPattern="JSONPATH:$.action"; trigger=true]

That’s seems to be an interesting hint

Don’t use the semicolon(;), but decimal point(,)

Test-Setup:

.items

Switch    Sonoff_Basic_03                "Schreibtischlampe Büro [MAP(de.map):%s]"          <tablelamp>             (EG_Buro,gSonoff)            { channel="mqtt:topic:hans:basic03:power" }

.things

    // Sonoff Basic
    Thing topic basic03 "Sonoff Basic 03 Schreibtischlampe" @ "MQTT2" {
    Channels:
        Type switch : power     "Power "                 [ stateTopic="stat/basic_03/POWER", commandTopic="cmnd/basic_03/POWER" ]
        Type number : rssi      "WiFi Signal Strength"   [ stateTopic="tele/basic_03/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI"]
        Type string : version   "Firmware Version    "   [ stateTopic="stat/basic_03/STATUS2", transformationPattern="JSONPATH:$.StatusFWR.Version"]
        Type switch : reachable "Reachable"              [ stateTopic="tele/basic_03/LWT",transformationPattern="MAP:reachable.map" ]
//        Trigger String : powerx     "Power x Trigger Test "                 [ stateTopic="stat/basic_03/POWER" ]   // seems not to work with this syntax
        Type switch : powerx     "Power X"                 [ stateTopic="stat/basic_03/POWER", trigger=true ]  // Trigger Channel; no need to define an item
    }

.rules

rule "Test MQTT Events"
when
    Channel 'mqtt:topic:hans:basic03:powerx' triggered
then
       logInfo("test_channel.rules", " Channel triggerd: {} ", receivedEvent.getEvent)  // test
       logInfo("test_channel.rules", " Channel triggerd: {} ", receivedEvent)  // test
end

Result:

2019-10-02 15:57:23.837 [INFO ] [home.model.script.test_channel.rules] -  Channel triggerd: ON 
2019-10-02 15:57:23.850 [INFO ] [home.model.script.test_channel.rules] -  Channel triggerd: mqtt:topic:hans:basic03:powerx triggered ON 
2019-10-02 15:58:34.777 [INFO ] [home.model.script.test_channel.rules] -  Channel triggerd: OFF 
2019-10-02 15:58:34.786 [INFO ] [home.model.script.test_channel.rules] -  Channel triggerd: mqtt:topic:hans:basic03:powerx triggered OFF 

Thx to @opus for this.

Annotation: Rule triggers after Startup too ?!:smirk:

For information: @miba, @cweitkamp

You’d better call , a comma, since a decimalPOINT is dependant in the language a . or a ,.

1 Like

I expect that is a consequence of openHAB connecting to MQTT broker and asking for the topic. A kind of REFRESH leading to an update.
It has to do that to find out if the topic is ‘retained’.

EDIT - it is debatable if an update (to UNDEF) should still be issued if no retained data is found on the broker - but that is what it does for now.

1 Like

Thanks everybody, everyting it’s now working triggering the channel and the subsequent switch/case operations

I wonder if for this kind of use case a received update on item is better or triggering the channel itself

It really depends on your choice.
I have no channel triggered rules but that more a habit from using openHAB form a long time

Considering for things like RSSI you are possibly going to want to display it on your UI sometimes, linking to an Item gives you that option.

There are occasional uses for profiles in conjunction with channels, these are only available for use with channels linked to Items. The fairly comprehensive transformation options for MQTT binding can usually take care of any needs there though.

2 Likes