[SOLVED] Publish state of Homematic device to MQTT 2.4 binding

Aftersuccesfully receiving values from my MQTT broker and reflecting the received states in OH, I now need to get the other direction working: OH knows the state of a Homematic device and should publish it to MQTT. How do I do this using Paper UI as I did for receiving values/commands from MQTT?
I read through the MQTT binding 2.4 docs incl the blog post but I don’t understand how to get the state of an device published by OH to my MQTT broker similar to what the eventbus automatically did with the statePublishTopic in MQTT binding v1. It doesn’t necessarily need to be done automatically for all devices, but at least for some I need to set it up.

You’ll have to create rules on state change and use the mqtt action or a proxy item

1 Like

I created the following rule:

rule "Garagentor Kontakt links oben MQTT Publish"
when
        Item Garage_Kanal_3 changed
then
        val mqttActions=getActions("mqtt","mqtt:broker:homeBroker")
        mqttActions.publishMQTT("Garage/TorLinks/oben",Garage_Kanal_3.state)
end

However, when the rule is triggered because the contact is closed/opened I get the following error in openhab.log:

Rule 'Garagentor Kontakt links oben MQTT Publish': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.binding.mqtt.action.MQTTActions.publishMQTT(org.eclipse.smarthome.core.thing.binding.ThingActions,java.lang.String,java.lang.String) on instance: null

I have created the broker thing through Paper UI and it works for other use cases in OH (such as receiving values and assigning them to OH items):

image

        mqttActions.publishMQTT("Garage/TorLinks/oben", Garage_Kanal_3.state.toString)
3 Likes

Thank you so much - now it works!