Control light via MQTT JSON

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:

{"state":"ON","level":51,"color_temp":370,"bulb_mode":"white"}

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.

1 Like

Something like

UID: mqtt:topic:5ff27bdb7f:dd2c974bfd
label: JSON Light
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:5ff27bdb7f
location: Celing
channels:
  - id: state
    channelTypeUID: mqtt:switch
    label: Shed Light
    description: null
    configuration:
      commandTopic: outgoing/message
      formatBeforePublish: '{"state":"%s"}'
      stateTopic: incoming/message
      transformationPattern: JSONPATH:$.state
  - id: Brightness
    channelTypeUID: mqtt:number
    label: Brightness of shed light
    description: ""
    configuration:
      commandTopic: outgoing/message
      formatBeforePublish: '{"level":"%s"}'
      stateTopic: incoming/message
      transformationPattern: JSONPATH:$.level

You can create a dimmer item and link both channels to it.

Thanks for the hints, but I am still struggling. Maybe you can point me in the right direction.
My things file looks like this:

Bridge mqtt:broker:myMQTTBroker
{
    Thing topic MQTTOfficeLedStrip "MQTT Office Led Strip " @ "Office" {
    Channels:
        Type switch : PowerSwitch  [ stateTopic="milight/states/0x1D/rgb_cct/1",
									commandTopic="milight/commands/0x1D/rgb_cct/1",
									transformationPatternOut="JSONPATH:$.state",
									formatBeforePublish="{\"state\":\"%s\"}",
									on="ON",
									off="OFF" ]
        Type string : StateString  [ stateTopic="milight/states/0x1D/rgb_cct/1" ]
      }
}

and my items file like this:

Switch MQTTOfficeLedStripSwitch	"MQTT Office Ledstrip mqtt" {channel="mqtt:topic:myMQTTBroker:MQTTOfficeLedStrip:PowerSwitch"}
String MQTTOfficeLedStripString "MQTT Office Ledstrip mqtt" {channel="mqtt:topic:myMQTTBroker:MQTTOfficeLedStrip:StateString"}

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.

Isn’t that what I tried to do in my things configuration or am I making changes at the wrong place?

No.

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.

1 Like

@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 transformationPatternOut and now the light is working, but I get the following error in my log:

Could not transform state 'ON' with function 'JSONPATH:$.state' and format '%s'

Please restart openHAB.

If after restarting you still have this issue please post your full configuration so we can have a look at the current state of play.

If you look at the whole error, I suspect it mentions profiles
See -

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

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.