Help with MQTT

Hello everyone,

I’m trying to create a mqtt binding and I’m having a headache on how to do something.

The MQTT configuration for the channel is as follow:

Status channel
/garage/status/door - values are: open, closed, opening, closing
Command channel
/garage/command/door - values are: open, close

How can I create a channel that do all of this?

Thank you

In the UI is should be pretty apparent.

All four parameters you are looking for are in that screen.

If you are using. this files, will this is one of the reasons why I don’t use this files. You have to look at and understand the reference docs instead of just reading what’s in the screen.

This doesn’t work. I already use this but the state topic and the command topic use different value.

I’ve put open and close in command. This make the command work when I toggled on/off. But, the button always stay ON. Why? Cause the off state is closed no close. And also since opening and closing state aren’t in any of these 2 command, it will never change the state.

I found a way and it’s really not this. In the item state description, I’ve entered a list of value from open, close, opening, closing with either on or off.

This is how the Mqtt is configure. This make the switch work but the state of the button doesn’t update, it always stays on since the off topic must be closed but the command for off is close.

Then in the item, I added metadata for the state description.

This is the magic that now make it work.

An easier fix would simply to allow to have custom state payload and custom command payload.

That’s why there is a separate field for them. The state topic and the contact topic are expected to be different.

Toggled off how? From OH in a rule or sitemap or main UI? From the device?

How you toggle it matters to understand where the ornament is.

:person_shrugging: Not enough information.

They could be “foo” and “bar” for all that matters. If you enter “open” for the on value and “closed” for the off value it will translate those to ON and OFF respectively since you are using a Switch (I assume).

Indeed and I missed that detail the first time around. A Switch can only have two states, ON and OFF. If you need for states, you can’t use a switch Channel. You’ll have to use a string or number Channel, or use a transformation to convert the four states to two.

Transformations are made for this. Instead of the on value and off value fields, use a Map transform to map the for values to ON and OFF.

Something I think you miss is that the state and the command for closing is not the same value.

So what happen. My things state on is open, state off is closed. When attaching an item to control it like a switch, this will update accordingly to the state message. Great.

But when I’ll press on the switch, the on/off trigger will send the custom on/off payload to the command path. Great.

This is where it doesn’t work in this case. Don’t ask me why, but the person who coded that Mqtt device decided that the closing command was close while the state say closed. Because of that, when I press the switch from the open state, it send closed on the command path and nothing happen.

If I change the thing so off value is close, than the command will work but the state value received is closed so the item doesn’t update its state.

Using item state description worked in sending on/off and is also one of the describe way to do it in the forum. I just though in the future, we could have custom command payload and custom state payload which only make this add-ons more versatile.

That’s why there is a separate that’s for the state and for the command.

As I said, if you have four states, you can’t use on value and off value at all. You must use transformations. The on value and off value fields are there to handle the must common simple case. If you have a minute complex case, like this, you need to use a transformation.

In this case, you could set up a single Map but you need to apply it to both the incoming and the outgoing transformation fields.

But we already do, by using transformations.

The state description only changes how the Item appears in the UIs. it doesn’t change what commands it sends and what updates it can receive.

State description is something I found on this forum and since I’ve done what I showed, everything is working so I guess it’s not just for show in the UI. Right now I got no transformation applied, I’ve put on valurle for open and closed value for off in the channel and put on=open and off=close in the state description. Now the switch open/close the garage door and the switch state properly update based on the Mqtt state value (tested by opening and closing the myq app).

Transformation isn’t as straight forward to simply adding 2 field just like there is for custom state value. I tried to find how to use the transformation and it might be clear for someone already using them but everything I tried just failed. Why having custom state value then, we could just put transformation? While looking for answer, I saw this coming back many time and honestly, I don’t see why it’s disregard just because there’s a transformation that doesn’t tell me how to make it send close when it’s off and open when it’s on.

From your description of the config it’s ignoring the “opening” and “closing” states entirely.

You are free to file an issue to see if someone will volunteer to add this feature.

Settings → Transformations → + to create a transformation


Settings → Things → YourMQTTThing ->Your MQTT Channel → Show Advanced


Yes I didn’t implement opening and closing state on the switch, I have this on a status one that is textual from the same item. I could be I didn’t do them.

Thank you for the transformation tutorial, I will star this reply for sure cause there’s many time where I wanted to use one and never understood how.

This is implemented for rollershutters in [mqtt.generic] Expose more advanced rollshutter config options by ccutrer · Pull Request #16051 · openhab/openhab-addons · GitHub (technically the underlying infrastructure has already been merged, and this is just exposing the functionality for configuration when using a generic MQTT thing). Switches underlying code already supports it, and would just need a PR similar to this one to expose it for a generic MQTT thing.

my way didn’t work in the end. I just used the rlkoshak way which worked. opening and closing are on open for me cause that’s how I want it.

As for the rollershutters, it doesn’t work for me in the mqtt version. I always have value of 0 in mqtt, it’s not openhab problem, it’s the ratgdo mqtt firmware that doesn’t update it. Like the motion detector that only send “motion” when there’s motion and never revert to “clear” when there’s no motion after x time. I have implemented that with a nodered workflow that reset a timer each time there’s a motion.

This is the sort of situation that Expire Item metadata was created for. You set a period of time after which if the Item doesn’t update or change it reverts to another state. So, for example, your Motion sensor Item would be configured to return to OFF 2 minutes after the last update to ON is received. No rules or NodeRed workflows required.

OH! I didn’t knew about that, I’ll try that right now, thanks!

edit: Does it publish the new “state” to mqtt or just in openhab?

If configured to expired with a command it will publish that to MQTT. If configured to only update the state (default behavior) it does not publish to MQTT.

This presupposes that the MQTT Channel is configured to publish on a command in the first place. If this is just a sensor it’s unlikely that it’s configured to publish.

ah yeah, I’ll make the other rules read the openhab state then, thanks!