MQTT with MAP - sync both ways possible?

Hi all

Latest openHAB here, with MQTT binding in use. I’ve defined a channel “mode” linked to an item “modus”, and since on the MQTT side valid values are “0” and “1” but switches in openHAB have valid values ON and OFF, I selected MAP as profile and added the following simple transformation:

key=value
0=OFF
1=ON
=default

Now when I change the state on the MQTT side, e.g. like that:

mosquitto_pub -h <IP> -m "1" -t openWB/set/LegacySmartHome/config/set/Devices/2/mode

the state of the switch in openHAB changes immediately. But when I change it in openHAB, nothing happens on the MQTT side.

Channel config looks like:

MQTT State Topic

openWB/set/LegacySmartHome/config/set/Devices/2/mode

MQTT Command Topic

openWB/set/LegacySmartHome/config/set/Devices/2/mode

Why?

Thanks for your help,
Bernd

Why not just use the on and off configuration parameter for the channel?

Thank’s for pointing me into this direction.

But: now when I modify the channel (e.g. with mosquitto_pub), I can see ON / OFF for a fraction of a second as the state value, but then it immediately changes to NULL; why that?

BTW, I had the same behaviour before I added =default to the transformation I used earlier.

Did you keep the MAP transform too? Can you share your full configuration?

No, I selected Default profile.

Currently, I deleted items and channel, so I cannot share my config now.

Maybe to clarify: what I want to achieve is that

  • I can modify my “switch” from within openWB, which means that the MQTT topic changes (values are 0 or 1). This should be reflected within openHAB
  • I can modify my “switch” from within openHAB (ON, OFF), and this should be reflected within openWB

so basically a two-way sync.

Up until now, the only thing I got working is MQTT → openHAB, and only with the MAP transformation I mentioned.

Interestingly, I just re-created channel and item, and before altering the state within openWB, the new openHAB item showed OFF, which was the correct state. When I started modifying it within openWB, I get the former behaviour: ON/OFF for a fraction of a second, then NULL. So it seems to work openWB → openHAB, but the state is “overwritten” by NULL immediately.

Ah, when trying the other way (openHAB → openWB) and observing with MQTT Explorer:

  • if the value is 0 and clicking on the switch in openHAB, the value gets modified to 1, but the OH item stays OFF (icon) and NULL (text)
  • if the value is 1 and clicking on the switch in openHAB, the value stays 1 (but highlighted grey for a second in MQTT Explorer, so an update is sent), but openHAB shows OFF (icon) and NULL (text)

Strange, isn’t it?

  - id: MQTT_Thing_openWB_Spuelmaschine_Modus
    channelTypeUID: mqtt:switch
    label: "MQTT Thing openWB Spülmaschine: Manueller Modus"
    description: ""
    configuration:
      commandTopic: openWB/set/LegacySmartHome/config/set/Devices/2/mode
      qos: 1
      stateTopic: openWB/set/LegacySmartHome/config/set/Devices/2/mode
      off: "0"
      on: "1"
label: "MQTT Thing openWB Spülmaschine: Manueller Modus"
type: Switch
category: switch
groupNames: []
tags:
  - Point

OK, problem found: you shouldn’t use the same topic for get and set: command topic should be

openWB/set/LegacySmartHome/config/set/Devices/2/mode

and state topic should be

openWB/LegacySmartHome/config/set/Devices/2/mode