Refresh Item when certain MQTT message is received

  • Platform information:
    • Hardware: Raspberry Pi 3 Model B Rev 1.2
    • OS: Raspbian GNU/Linux 10
    • openHAB version: 2.5.6-2 (Release Build)

Hello everyone,

I have connected 2 Tradfri switches and two different LEDs from IKEA to my Zigbee2MQTT and subsequently to my Openhab. I can control the Lights from Openhab and also from the switches (by binding them in zigbee2mqtt).

My problem is: When I use the (IKEA hardware) switches to turn the lights on or off, change brightness etc. Openhab doesn’t realize that. I have set up rule with a cronjob that simply runs a mosquitto_pub to get the current values:

executeCommandLine(“mosquitto_pub -h localhost -t zigbee2mqtt/Licht_Vitrine/get/state -m “””)
executeCommandLine(“mosquitto_pub -h localhost -t zigbee2mqtt/Licht_Kommode/get/state -m “””)

however, that is not quite elegant. I was looking for making a rule that triggers when I receive the button press in my mqtt broker, e.g.:

Client mosqsub|3127-openhab received PUBLISH (d0, q0, r0, m0, ‘zigbee2mqtt/SchalterVitrine’, … (71 bytes))

I found serveral posts about creating an item and binding it to the mqtt, but these are apparently all for MQTT 1. Since I got MQTT 2 that doesn’t work.

Does anyone have an idea how to get this running?

Kind Regards,

Christoph Bismark

don’t the lights publish a state on change?

With an mqtt “thing” there’s a command topic for setting the state and a state topic for receiving a state.

This is pretty close to the setup of a switch:

Bridge mqtt:broker:mqttBroker [ host="192.168.x.x", secure=false ]
	Thing topic mqttTest "mqttTest" {
			Type switch : mqttSwitch "MQTTSwitch" [ stateTopic="MQTTSwitch/State", commandTopic="MQTTSwitch/Command" ]

Also with zigbee2mqtt there’s a setting, that will push everything to a topic, rather than needing json transformations of every item:

  output: attribute

Source: Zigbee2mqtt revisited: No more ugly transformations

I suppose you would set up MQTT (v2) Things and channels to listen for button presses, link to Item(s).
Then a rule triggered by button Items can predict the effect and update the light Items directly. Or invoke your light status refresh code.

It’s all a bit of a kludge, I’d be looking to have the IKEA lights report when they change.

I do have stateTopic defined:

Type switch : onoff “Schalter Vitrine” [stateTopic=“zigbee2mqtt/Licht_Vitrine”, transformationPattern=“JSONPATH:$.state”, commandTopic=“zigbee2mqtt/Licht_Vitrine/set”, transformationPatternOut=“JS:tradfriStateTransform.js”]

But apparently since I have bound the switch to the Light Group directly, the stateTopic seems to not be published when I press the hardware button?



Not sure what that means.
Have you got an Item bound to your MQTT generic Thing switch-type channel? Is it a Switch type Item?

Presumably they’ve linked/paired the switch directly to the bulb, through (IKEA’s?) Zigbee protocol…

Hello everyone,

I have bound the switch to a certain group in my zigbee2mqtt configuration. But I have now found the config-option
report: true
in the configuration.yaml. Now it works automagically :slight_smile: