MQTT 1.x Bindings with channel returning json

Hi everyone,

i am still using MQTT 1.x ( i know about the mqtt 2.5 :slightly_smiling_face:)

i need to setup items with json inputs and json answers

Current example which is working with string

Switch tuya_x "x" <light> [ "Lighting" ] {mqtt="<[broker:tuya/id/key/ip/state:state:default:.*], >[broker:tuya/id/key/ip/command/on:command:ON:true], >[broker:tuya/id/key/ip/command/off:command:OFF:false]"}

the new tuya api now should get json instead of string

  1. /outbound>

/Instead of ON, i should send a json string { “dps”: 1, “set”: true }
/Instead of OFF, i should send a json string { “dps”: 1, “set”: false }

  1. /Inbound

the state is ON if “20=true” in the json
Returned json from the mqtt channel:

{"20":false,"21":"colour","22":1000,"24":"0149032903e8","26":0,"101":75,"102":80,"106":"classic"}

i don’t know how to integrate the json into the inbound and outbound channels

any help would much appreciated

Thanks in advance

For outbound, as discussed in the binding docs when talking about the transformation field for the binding config: https://www.openhab.org/addons/bindings/mqtt1/#item-configuration-for-outbound-messages

Any value other than the above types will be interpreted as static text, in which case this text is used as the message content.

So just put the JSON after the last “:” where you would normally put default or reference to a transformation. You will probably have to escape the : and " using \.

https://www.openhab.org/addons/transformations/jsonpath/ for extracting the JSON from inbound. See the docs for how to define it. Remember to install it, under Transform tab in PaperUI.

The thing I’ll never understand is why spend time trying to figure out stuff on a deprecated binding when you could use that time figuring out the new version which you will have to move to at some point anyway? It uses more of your time in the long run. And since you can run both bindings at the same time you don’t even have to migrate everything all at once.

Thanks Rich for your continous support

i think that a json transformation and also a function transformation is necessary

/Instead of ON, i should send a json string { “dps”: 1, “set”: true }

i didn’t know that i can run both bindings at the same time , indeed, in this case, i will try with the new mqtt embeded binding.

i am also using this old action according the number

image
why ? invest time on deprecated binding? because i don’t understand what changes should be done on a thing so simple like a mqtt message. it worked. on the long term, it means to update the mqtt channels of existing items to the new binding.

you are right, i will have to make the move…:slight_smile:

And per the documentation for the 1.x binding

>[broker:tuya/id/key/ip/command/on:command:ON:{ \“dps\”\: 1, \“set\”\: true }]

will publish the JSON (the stuff after the ON) on an ON command. You may need to escape the curly brackets too.

Be careful with terminology. There is an MQTT Embedded Broker (you should not use) and the MQTT Binding.

See the bottom of the MQTT v2 Binding’s documentation for examples for how to call the publsihMQTT Action. You don’t have to install the Action separately like you did with MQTT v1.

In the MQTT v2 binding, there is a field for switch type Channels that let’s you define a custom value to be published for ON and OFF commands. You would put your JSON string there.

There is also an optional


which might also be a good option which might work in other circumstances, not your specific one here though.

Except it doesn’t work or, in this case, you didn’t understand what changes should be done on the old binding to achieve what you want to do. So you could:

  1. spend the time figuring out how to make it work on the old binding, which is time that will be wasted in the long run because it’s throw away once you move to the new binding

  2. spend the time figuring out how to make it work on the new binding, which may take a bit more time up front but the skills and knowledge you gain isn’t throw away; you will be able to apply that when you decide to move your other Items to the new binding.

2 always seems like a better choice. Spend some time now to save wasted effort and save time later. But that’s just me.

There should be enough information here to get it working with either binding. If not, come back and I’ll answer what I can.

Thanks

just to summarize to be sure that i have well understood.

i do not need to install the embeded mqtt broker because i can continue using my mosquitto browser.

i should be able to create a broker thing based on my mosquitto.

i can install the two binding of mqtt 1.x and 2.5. does it means that the both formats are supported?

Not only correct, but please keep your Mosquitto broker.

Absolutely. That’s what most of us do.

Yes but if you use a 1.x format, it will only use the 1.x binding. If you use the 2.5 format it will use the 2.5 binding. You cannot use the 1.x format with the 2.5 binding.

Thanks a lot for your guidance and your clarification. You opened my horizons

Yes but if you use a 1.x format, it will only use the 1.x binding. If you use the 2.5 format it will use the 2.5 binding. You cannot use the 1.x format with the 2.5 binding.
`

And since you can run both bindings at the same time you don’t even have to migrate everything all at once.

to be clear, i can install both but only one format will be active at the time.

it’s not possible to have for example my irobot items 1.x and my tuya items in 2.5.

it means that all the items should be in format 1.x or in format 2.5. so, maybe i miss the point but yes i need to migrate/standardize all at once

Moreover, i have installed mqtt 2.5 even after the openhab restart the mqtt broker thing is not active

2020-01-10 12:11:30.513 [vent.ChannelTriggeredEvent] - logreader:reader:openhablog:newErrorEvent triggered 2020-01-10 12:11:26.500 [ERROR] [st.core.internal.thing.ThingResource] - Exception during HTTP PUT request for update config at 'things/mqtt:broker:83940817/config'

2020-01-10 12:11:30.517 [vent.ItemStateChangedEvent] - openhablogreaderLastError changed from 2020-01-10 03:47:15.942 [ERROR] [org.quartz.core.ErrorLogger         ] - Job (DEFAULT.Timer 9 2020-01-10T03:47:15.839+02:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | { to 2020-01-10 12:11:26.500 [ERROR] [st.core.internal.thing.ThingResource] - Exception during HTTP PUT request for update config at 'things/mqtt:broker:83940817/config'

2020-01-10 12:12:13.053 [hingStatusInfoChangedEvent] - 'mqtt:broker:83940817' changed from UNINITIALIZED to REMOVING

2020-01-10 12:12:46.768 [vent.ItemStateChangedEvent] - lastseen_Gil changed from 2020-01-10T12:11:26.011+0200 to 2020-01-10T12:12:46.733+0200

==> /var/log/openhab2/openhab.log <==

2020-01-10 12:13:09.032 [WARN ] [ore.thing.internal.ThingRegistryImpl] - Cannot create thing. No binding found that supports creating a thing of type 'mqtt:broker'.

No. If you have an Item with { mqtt=">[blah blah blah]" } that Item will use the 1.x binding. If you have an Item with { channel="mqtt:blah:blah:blah" } that Item will use the 2.x binding. You cannot use the first configuration with the 2.x binding.

This is totally possible.

Double check that your 2.5 binding is still installed.

1 Like

i have uninstalled the mqtt 2.5 binding and try to reinstall it

2020-01-12 00:43:48.099 [ERROR] [org.openhab.binding.mqtt            ] - bundle org.openhab.binding.mqtt:2.5.0 (359)[org.openhab.binding.mqtt.internal.MqttBrokerHandlerFactory(401)] :  Error during instantiation of the implementation object

java.lang.NoClassDefFoundError: org/openhab/binding/mqtt/MqttBindingConstants

	at org.openhab.binding.mqtt.internal.MqttBrokerHandlerFactory.<clinit>(MqttBrokerHandlerFactory.java:52) ~[?:?]

Caused by: java.lang.ClassNotFoundException: org.openhab.binding.mqtt.MqttBindingConstants cannot be found by org.openhab.binding.mqtt_2.5.0

getting the same even the openhab restart service

Clear the cache and reinstall to see if that clears things up.

Thanks!!! it worked

after 2 X(clear cache and restart), it worked

i have kept the existing items with the 1.x format

i have added some new items in the 2.5 format to support my tuya items like explained below

till now, all my conf folder is stored in git .
All my things, items, rules are only defined in files.
so, till now in case of the new installation i had just to download the last openhabian version and a pull from git.
why? because it seems to be the most simple approach and with all the git advantages.

on the second side, i have to admit that it was really simple with the paper ui to define the mqtt things.

i will need to find a way to get those things exported maybe with a such script
https://community.openhab.org/t/recommended-way-to-backup-restore-oh2-configurations-and-things/7193

the command topic is well working

the state do not work yet. i need to parse the json file

Just add /var/lib/openhab2/jsondb to your git. There is more in there you might want to add too in order to have"everything" in git. The stuff in etc and config in particular but some bindings have their own stuff in there own folder too. Just don’t include cache and tmp.

1 Like