[SOLVED] Tradfri remote (zigbee2mqtt) rule: just can't get simple rule to work

Novice warning!
openHAB version: 2.5.2-1 / zigbee2mqtt

Hi community,
I’m in need of some assistance on what i thought was going to be a simple rule. I’m no coder, so I’ve scavenged the forums for some working examples. Trying to catch a Tradfri remote’s (the round one with 5 buttons) button presses and do something with it.

Items file:

// LED1545G12 - IKEA TRADFRI LED bulb E26/E27 980 lumen, dimmable, white spectrum, opal white (Router)
Switch Kitchen_Light_Switch     "Kitchen Light [%s]" (Ikea) {channel="mqtt:topic:mosquitto:0x000b57fffe8d8719:kitchenLightState"}
Dimmer Kitchen_Light_Brightness "Kitchen light brightness [%d %%]" (Ikea) {channel="mqtt:topic:mosquitto:0x000b57fffe8d8719:kitchenLightBrightness"}

// E1524/E1810 - IKEA TRADFRI remote control (EndDevice)
Number Office_Remote_Linkquality  "Office remote link [%d]" <qualityofservice> (Ikea) {channel="mqtt:topic:mosquitto:0x000b57fffe8d3e46:officeRemoteLinkquality"}
Number Office_Remote_Battery      "Office remote [%.1f %%]" <battery> (Ikea) {channel="mqtt:topic:mosquitto:0x000b57fffe8d3e46:officeRemoteBattery"}
String Office_Remote_Action      "Office remote [%s]" (Ikea) {channel="mqtt:topic:mosquitto:0x000b57fffe8d3e46:officeRemoteAction"}

Rule file:

rule "OfficeRemote"
when
    Channel "mqtt:topic:mosquitto:0x000b57fffe8d3e46:officeRemoteAction" triggered
then
    //var actionName = receivedEvent.getEvent()
    switch(receivedEvent.getEvent()) {
        case "toggle": {
            Kitchen_Light_Switch.sendCommand(OFF)
        }
    }
end

I know the items are working (can see them with values in the basic ui and turn the light on/of). Pressing the buttons on the remote changes the action value in basic ui, i just can’t seem to grab it in a rule.

Any help/pointers are appreciated,
Kind regards.

How about using your Item for the trigger? There is actually a bug the the rules DSL with Channel triggers too. A benefit of using the Item for the trigger is that you can add it to a sitemap and perform the same functions through the UI.

rule "OfficeRemote"
when
    Item Office_Remote_Action received update
then
    switch(triggeringItem.state.toString) {
        case "toggle": {
            Kitchen_Light_Switch.sendCommand(OFF)
        }
    }
end

Hi,

To trigger actually rule by button you need to pass trigger=true in your item definition:

Thing mqtt:topic:openhab:mi-button-5b2d (mqtt:broker:openhab) {
	Channels:
		Type string : click [ stateTopic="zigbee2mqtt/0x00158d0001be5b2d", transformationPattern="JSONPATH:$.click", trigger=true ]
		Type number : battery [ stateTopic="zigbee2mqtt/0x00158d0001be5b2d", transformationPattern="JSONPATH:$.battery" ]
		Type switch : battery_low [ stateTopic="zigbee2mqtt/0x00158d0001be5b2d", transformationPattern="JS:z2m-lowbatt.js" ]
		Type number : link [ stateTopic="zigbee2mqtt/0x00158d0001be5b2d", transformationPattern="JSONPATH:$.linkquality" ]
}

(see click channel)

Now you can trigger rule:

rule "KU Arbeitlicht SW"
when
    Channel "mqtt:topic:openhab:mi-button-5b2d:click" triggered
then
	var state = receivedEvent.getEvent()
    switch(state) {
        case "single": {
            if (snf_eg_ku_arbeit.state == ON) {
                snf_eg_ku_arbeit.sendCommand(OFF)
            } else {
                snf_eg_ku_arbeit.sendCommand(ON)
            }
        }
        case "long": {
            if (snf_eg_ku_haupt.state == ON) {
                snf_eg_ku_haupt.sendCommand(OFF)
            } else {
                snf_eg_ku_haupt.sendCommand(ON)
            }
        }
    }
end
1 Like

@5iver and @Petrows thank you both! As always there’s more than one way to Rome. Now, on to finding out what is the best way for my use case.

Edit: apparently cannot select 2 solutions. For future reference: both above solutions work!

As I mentioned in my post though, there is a bug in Channel triggers…

So, the solution from @Petrows will suffer from this bug… but mine won’t! :stuck_out_tongue_winking_eye: