I am trying to control a MQTT light. This light requires a JSON string to change any of its settings. With mqtt explorer I can easily change settings of the light by exactly typing the change in the string below:
to change the light from “on” to “off” I just send {"state":"OFF"}
I read many topics, but I can’t find any description how to configure a thing/channel/item to do this. I know I need JSONPATH, but I have no idea how to integrate it in the mqtt command topic.
It would be appreciated if someone can post an example.
Don’t have an immediate example, but check the formatBeforePublish option within the MQTT Binding Things and Channels documentation. For this you don’t need JSONPATH
However, you do need JSONPATH for the incoming message.
When I switch the item from on to off nothing happens. MQTT explorer doesn’t show anything, but the opeHAB log file shows an error which indicates that something is not correct with the JASONPATH transformation.
2021-11-14 16:10:49.421 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: Invalid path '$.state' in 'OFF'
2021-11-14 16:10:49.421 [DEBUG] [ab.binding.mqtt.generic.ChannelState] - Transformation 'org.openhab.binding.mqtt.generic.values.OnOffValue@622668c5' returned null on 'JSONPATH', discarding message
2021-11-14 16:10:49.421 [DEBUG] [qtt.generic.AbstractMQTTThingHandler] - Successfully published value OFF to topic milight/commands/0x1D/rgb_cct/1
JSONPATH takes a string of JSON, and extracts a bit of data from it.
The raw output of a switch type channel is going to be something like ON or OFF, not JSON.
Have another look at @denominator suggestion, and see how that creates JSON for the outbound message.
transformationPatternOut says “apply this transformation to the raw command from the Item and use the result for the outbound message”
JSONPATH analyzes JSON data and returns one piece of it, e.g. {"fred":"apple","state:"XX"} -> "JSONPATH:$.state" -> returns "XX"
It doesn’t work backwards e.g.
Item command ON -> "JSONPATH:$.state" -> error, this "ON" you have given me to analyze is not JSON and there is no "state" element in it.
There is no MAKEJSON transformation.
That’s okay, we really only want to drop the command into a placeholder in a predefined string, its just string formatting.
@rossko57 has provided the background, so to be explicit: remove the transformationPatternOut line completely from your configuration. You’ll see it doesn’t feature in @denominator example.
I removed the JSONPATH part completely. Inow understand it is only required to read the values included in the stateTopic. I also added a brightness setting which listens to “level”. In short the “formatBeforePublish” is all I need (at least it is working). I still received some warnings, but they disappeared after a reboot. What I also noticed is that if I make a mistake in the formatting, like forgetting a " then mqtt stops working.
Thanks all for the support!
[8,10]: no viable alternative at input 'formatBeforePublish'
[11,10]: mismatched input 'commandTopic' expecting ']'
[12,10]: no viable alternative at input 'formatBeforePublish'
[13,36]: mismatched input '[' expecting ':'
[15,7]: missing EOF at '}'
2021-11-16 13:35:28.961 [DEBUG] [ab.binding.mqtt.generic.ChannelState] - Unsubscribed channel mqtt:topic:myMQTTBroker:MQTTOfficeLedStrip:Brightness form topic: milight/states/0x1D/rgb_cct/1
2021-11-16 13:35:28.961 [DEBUG] [ab.binding.mqtt.generic.ChannelState] - Unsubscribed channel mqtt:topic:myMQTTBroker:MQTTOfficeLedStrip:StateString form topic: milight/states/0x1D/rgb_cct/1
2021-11-16 13:35:28.961 [DEBUG] [ab.binding.mqtt.generic.ChannelState] - Unsubscribed channel mqtt:topic:myMQTTBroker:MQTTOfficeLedStrip:PowerSwitch form topic: milight/states/0x1D/rgb_cct/1
2021-11-16 13:35:28.976 [DEBUG] [nternal.handler.EspMilightHubHandler] - MQTT brokers state changed to:CONNECTED
2021-11-16 13:35:28.976 [WARN ] [.core.thing.binding.BaseThingHandler] - Handler EspMilightHubHandler tried updating the thing status although the handler was already disposed.
2021-11-16 13:35:28.976 [DEBUG] [nternal.handler.EspMilightHubHandler] - MQTT brokers state changed to:CONNECTED
2021-11-16 13:35:28.976 [WARN ] [.core.thing.binding.BaseThingHandler] - Handler EspMilightHubHandler tried updating the thing status although the handler was already disposed.
2021-11-16 13:35:28.976 [DEBUG] [nternal.handler.EspMilightHubHandler] - MQTT brokers state changed to:CONNECTED
2021-11-16 13:35:28.976 [WARN ] [.core.thing.binding.BaseThingHandler] - Handler EspMilightHubHandler tried updating the thing status although the handler was already disposed.
2021-11-16 13:35:28.976 [DEBUG] [nternal.handler.EspMilightHubHandler] - MQTT brokers state changed to:CONNECTED