Tradfri shortcut button always on

  • Platform information:
    • Hardware: Raspi 3B
    • OS: Raspbian GNU/Linux 10
    • Java Runtime Environment: openjdk version “11.0.11”
    • openHAB version: 3.1.0

Hello all,
I am trying to set up a tradfri shortcut button as an remote switch for an Osram Plug via zigbee2mqtt and a mosqitto browser. Most things seem to work well, I can switch the Osram plug from the UI and I see events when I press the shortcut button. The button sends only an “on” when it is pressed and I created a rule to switch of the Osram plug. The problem is, once the button is pressed, the state is always on. I tried to configure autoupdate = “false” but without success.
Here is the mqtt.things file:

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhab", password="XXXX", clientID="openhab"]
{

    Thing topic Button_1 "Button_1" {
        Channels:
        Type string : mode "Mode" [ stateTopic="zigbee2mqtt/Button_1" ]
        Type number :  mode "Battery" [ stateTopic="zigbee2mqtt/Button_1/battery"]
        Type switch : action "Power" [ stateTopic="zigbee2mqtt/Button_1/action", on="on"]
    }
}

and the .items file

Switch Button_1 "Button 1" {channel="mqtt:topic:mosquitto:Button_1:action", autoupdate="false"}

and the simple rule

rule Button_1_rule 
when Item Button_1  changed to "ON"
then
    logInfo("Button", "Button_1 ON")
    if (Osram_2.state == "OFF"){
        Osram_2.sendCommand("ON")
    }
    else{
        Osram_2.sendCommand("OFF")
    }
    //Button_1.sendCommand("OFF")
end

I tried to solve the problem with Button_1.sendCommand(“OFF”) but I this did not work either.
Any help appreciated

I assume you are talking about this button?
I got the same in use as well and as shown on the z2mqtt page it only supports the “on” state, it is basically a “click” action, hence setting this state to something else might not work.

To my knowledge, and I am not home right now to check, I remember that I also went around it with rules, checking the state of the influenced item and then sending a command based on this, like you did in your button rule (i.e. I use it for my sleep mode home state). Hence you basically need to run the rule not on “changed” (as it will “never” change) but when it “receives” a command (again, cannot check the precise terminology OH uses as I am not home).

Just to add, the command was postUpdate (just got access to my OH):

image

This way you can change the item’s state back to OFF (whereas not the MQTT or physical item state) and use the trigger of changing to ON when you click the button.

But for some reason, you have disabled autoupdate on your Item. So sending commands to it will never alter the state directly.

If you want to update the Item state, just do so.
Button_1.postUpdate(OFF)

Grasping the difference between openHAB Item’s state and command will help you in future.

A more intuitive way to set up your MQTT channel might be to enable the postCommand option. Then when you click, the Item will get a command instead of a state update. That means it behaves the same as clicking on a widget in the UI.

Thank you very much for your quick support.
Because the sources of the first post still have further errors, I would like to post here the working solution:
.things:

Type switch : action "Power" [ stateTopic="zigbee2mqtt/Button_1/action", on="on", postCommand="true"]

.items

Switch Button_1 "Button 1" {channel="mqtt:topic:mosquitto:Button_1:action"}

.rules

rule Button_1_rule 
when Item Button_1  received command "ON"
then
    if (Osram_2.state == OFF){
        Osram_2.sendCommand("ON")
    }
    else {
        Osram_2.sendCommand("OFF")
    }
    // only for beauty, actually has no function 
    Button_1.postUpdate(OFF) 
end

In fact it is irrelevant for the function if Button_1 .postUpdate(OFF) is called. Since the rule responds to received command, it works even if the button is always ON.