Using MQTT topic in rule

Openhab 4.2.1

At the bottom of this documentation, it says how to get the topic of the MQTT channel.

I’m not able to get this to work, and have tried all of these incantations:

Type trigger : trigger_accessControl    "Access control" [stateTopic="zwave/#/Kwikset/notification/endpoint_0/Access_Control/#"]
Type trigger : trigger_accessControl    "Access control" [stateTopic="zwave/+/T6Pro/#",trigger=true,publishTrigger=true] 
Type trigger : trigger_accessControl    "Access control" [stateTopic="zwave/+/T6Pro/#",separator="#" ]
Type trigger : trigger_accessControl    "Access control" [stateTopic="zwave/+/T6Pro/#" ]
Type trigger : trigger_accessControl    "Access control" [stateTopic="zwave/+/T6Pro/#",trigger=true ]
Type trigger : trigger_accessControl    "Access control" [stateTopic="zwave/+/T6Pro/#",separator="#",trigger=true ]
Type publishTrigger : trigger_accessControl "Access control" [stateTopic="zwave/+/T6Pro/#",separator="#",trigger=true ]
Type publishTrigger : trigger_accessControl "Access control" [stateTopic="zwave/+/T6Pro/#",separator="#" ]

I have a rule like this, trying to get the topic to show.

rule "Lock alarm triggered"
when
    Channel 'mqtt:topic:generic:trigger_accessControl' triggered
then
	// var actionName = receivedEvent.getEvent()
  	logInfo("LockAlarmTriggered",receivedEvent.toString)
end

I don’t use and don’t support .things files so I cannot help if there is a syntax error or the like.

I can confirm the configuration parameters are stateTopic and separator. I don’t know where you are getting these trigger and publishTrigger properties from. The type of the Channel is trigger, there’s nothing else to configure after that.

Inside your rule, when triggered, receivedEvent should be a String of the format <topic><separator character><message> (e.g. "zwave/something/T6Pro/something#ON").

Make sure you are using the subscription wildcards correctly. + is a single level wildcard. zwave/+/T6Pro will match zwave/foo/T6Pro but it will not match zwave/foo/bar/T6Pro. # is a multilevel wildcard. zwave/#/T6Pro will match both zwave/foo/T6Pro and zwave/foo/bar/T6Pro.

I’m pretty sure you’ll have to select string as type - or another “real” type matching the type of data which will be received. Something like this:

Type string : trigger_accessControl "Access control" [stateTopic="zwave/+/T6Pro/#", trigger=true ]

But I’m not at home so it’s not easy to check whether I’m right… :slight_smile:

Trigger Channels have a type of trigger. If OP changes the type to string it will change the Channel to be a state Channel instead of an event Channel. If the Channel is defined on the Broker Thing, it needs to be of type publishTrigger. If it’s a Channel on a Generic MQTT Thing it need to be of type trigger.

But it’s different in the docs…

  • trigger: If true, the state topic will not update a state, but trigger a channel instead.

The list of channel types does not include trigger at all.

Edit: As it’s for the received topic, it certainly has to be a publishTrigger channel as part of the bridge Thing.

Maybe it’s a difference between the JSON representation and the file based representation.

On the Broker a managed event Channel looks like this:

channels:
  - id: dadsoh_out
    channelTypeUID: mqtt:publishTrigger
    label: Dad's OH EB out topics
    description: ""
    configuration:
      stateTopic: dadsOH/out/#
      separator: "#"

channelTypeUID maps to the Type in a .things file as I understand it.

When creating the Channel, “Trigger” is among the types.

And the channelTypeUID is set to mqtt:trigger instead of there being a separate configuration parameter.

channels:
  - id: test
    channelTypeUID: mqtt:trigger
    label: test
    description: ""
    configuration:
      stateTopic: test/topic/#
      transformationPattern: "#"

Maybe the docs are out of date? Maybe there is a wide difference between managed MQTT Things and .things files, though that is very unusual. The names of the properties and how they are used usually map between the two.

Here’s a “switch” state Channel for comparison.

channels:
  - id: online
    channelTypeUID: mqtt:switch
    label: Online status
    description: Indicates online status of this sensor_reporter
    configuration:
      stateTopic: sensor_reporter/hydra/status
      off: OFFLINE
      on: ONLINE

Hmm, logs are showing:


==> /var/log/openhab/events.log <==
2024-05-31 11:43:37.250 [INFO ] [openhab.event.ChannelTriggeredEvent ] - mqtt:topic:generic:trigger_accessControl triggered {"time":1717173817242,"value":1717173786575}
2024-05-31 11:43:37.253 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'acs' changed from { "value": 0 } to {"time":1717173817242,"value":1717173786575}
2024-05-31 11:43:37.293 [INFO ] [openhab.event.ChannelTriggeredEvent ] - mqtt:topic:generic:trigger_accessControl triggered {"time":1717173817254,"value":0}
2024-05-31 11:43:37.295 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'acs' changed from {"time":1717173817242,"value":1717173786575} to {"time":1717173817254,"value":0}
2024-05-31 11:43:37.335 [INFO ] [core.model.script.LockAlarmTriggered] - {"time":1717173817242,"value":1717173786575}
2024-05-31 11:43:37.337 [INFO ] [core.model.script.LockAlarmTriggered] - {"time":1717173817254,"value":0}

No topic information.

Things:

  Type trigger : trigger_accessControl    "Access control" [ stateTopic="zwave/CCSE/T6Pro/#", separator="#", trigger=true ]
  Type string : trigger_accessControlStr  "Access control" [ stateTopic="zwave/CCSE/T6Pro/#", separator="#" ]

Items:

String acs { channel = "mqtt:topic:generic:trigger_accessControlStr"}

The rule:

rule "Lock alarm triggered"
when
    Channel 'mqtt:topic:generic:trigger_accessControl' triggered
then
  	logInfo("LockAlarmTriggered",receivedEvent)
end

I’m getting this error:


==> /var/log/openhab/openhab.log <==
2024-05-31 11:49:59.237 [WARN ] [rnal.handler.GenericMQTTThingHandler] - Configuration error for channel 'mqtt:topic:generic:trigger_accessControl'
java.lang.IllegalArgumentException: ChannelTypeUID not recognised: publishTrigger
        at org.openhab.binding.mqtt.generic.values.ValueFactory.createValueState(ValueFactory.java:86) ~[bundleFile:?]
        at org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler.initialize(GenericMQTTThingHandler.java:189) [bundleFile:?]
        at org.openhab.core.thing.binding.BaseThingHandler.thingUpdated(BaseThingHandler.java:157) [bundleFile:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
        at org.openhab.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:147) [bundleFile:?]
        at org.openhab.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
        at java.lang.Thread.run(Thread.java:840) [?:?]

with

  Type publishTrigger : trigger_accessControl    "Access control" [ stateTopic="zwave/CCSE/T6Pro/#", separator="#", trigger=true ]

publishTrigger can only be used as a type for a Channel added to the MQTT Broker thing, not for a Generic MQTT Thing.

Okay, I got it.
mqtt.things

Bridge mqtt:broker:home "MQTT Mosquitto Broker" [ 
  host="localhost",
  secure=false,
  port=1883,
  qos=0,
  retain=false,
  clientid="mqttThingFile",
  keep_alive_time=30000,
  reconnect_time=60000,
]

{
  Channels:
    Type publishTrigger : trigger_accessControl "Access control" [stateTopic="zwave/+/T6Pro/#",separator="#" ]
}

Thing mqtt:topic:generic "MQTT generic thing" (mqtt:broker:home) {
Channels:

  Type string : switch_shellyPlug_002_status "Range Extender" [stateTopic="zwave/ShellyPlug_002/switch_binary/endpoint_0/currentValue",transformationPattern="JSONPATH:$.value"]
  Type string : switch_shellyPlug_003_status "Range Extender" [stateTopic="zwave/ShellyPlug_003/switch_binary/endpoint_0/currentValue",transformationPattern="JSONPATH:$.value"]

  Type switch : switch_shellyPlug_002 "Range Extender"           [stateTopic="zwave/ShellyPlug_002/switch_binary/endpoint_0/currentValue",transformationPattern="JSONPATH:$.value",on="true",off="false",commandTopic="zwave/ShellyPlug_002/switch_binary/endpoint_0/targetValue/set"]
...
}

Logs are showing:

2024-05-31 12:11:17.023 [INFO ] [openhab.event.ChannelTriggeredEvent ] - mqtt:broker:home:trigger_accessControl triggered zwave/CCNW/T6Pro/sensor_multilevel/endpoint_0/Humidity#{"time":1717175477011,"value":57}