[SOLVED] Update item state from MQTT string

Hi
Following Bartus excellent how-to I have successfully gotten my LED strip to work with Openhab over MQTT. But, there’s something wrong (or unconfigured) that won’t have my items properly reflect the LED status. I’m on 2.4.0 stable and use MQTT binding v2.

I have been able to successfully get the items to update properly when setting up only one test/dummy item/reference for a channel, but with the LED strip, I’m passing a JSON string back and forth, not just one item status. I don’t know how to configure my things or items to get this properly set up. Any assistance will be appreciated. I’ve searched through the forum and some references to the Paper UI channel setup are made, but I don’t know how to do this.

A string looks like this:

{"state":"ON","color":{"r":255,"g":255,"b":255},"brightness":64,"effect":"noise","transition":30}

I have set up my MQTT broker and generic Thing through Paper UI, but I’m using .items files for my items.

Currently, I have only one channel in use for the LED strip and it’s holding both send/receive

/FrontXmasLights/state
/FrontXmasLights/command

My items file look like this:

String  FrontXmasLights         "Christmas Lights Front" {channel="mqtt:topic:MQTTgeneric:FrontXmasLights"}
Switch  FrontXmasLights_Power   "ON/OFF"
Number  FrontXmasLights_FX      "FX"
Color   FrontXmasLights_Color   "Color"    <rgb>
Number  FrontXmasLights_Speed   "Speed [%.0f]"
Number  FrontXmasLights_Intensity "Brightness [%d]" 

Sending commands to the strip is done through rules

//FrontXmasLights_Power
rule "Front Xmas Lights ON/OFF change"
when Item FrontXmasLights_Power received update
then FrontXmasLights.sendCommand("{\"state\":" + FrontXmasLights_Power.state + "}") end

//FrontXmasLights_FX
rule "Front Xmas Lights effect change"
when Item FrontXmasLights_FX received update
then FrontXmasLights.sendCommand("{\"effect\":" + transform("MAP","LEDStripEffectJSON.map",FrontXmasLights_FX.state.toString) + "}") end

//FrontXmasLights_Color
rule "Front Xmas Lights color change"
when Item FrontXmasLights_Color received update
then FrontXmasLights.sendCommand("{\"color\": {" +
                "\"r\": " + (FrontXmasLights_Color.state as HSBType).red + "," +
                "\"g\": " + (FrontXmasLights_Color.state as HSBType).green + "," +
                "\"b\": " + (FrontXmasLights_Color.state as HSBType).blue + "}}") end

//FrontXmasLights_Speed
rule "Front Xmas Lights speed change"
when Item FrontXmasLights_Speed received update
then FrontXmasLights.sendCommand("{\"transition\": \"" + FrontXmasLights_Speed.state + "\"}") end

//FrontXmasLights_Intensity
rule "Front Xmas Lights brightness change"
when Item FrontXmasLights_Intensity received update
then FrontXmasLights.sendCommand("{\"brightness\":" + FrontXmasLights_Intensity.state + "}") end

Any help on how I can get my Openhab items to update properly based on the MQTT message would be appreciated.

1 Like

Set up MQTT explorer and check what is the traffic to and from your lights
What payload the light needs exactly for each case?

Hi
In the MQTT explorer it looks like this for state:

{"state":"ON","color":{"r":255,"g":255,"b":255},"brightness":64,"effect":"bpm","transition":205}

and like this for command:

{"transition": "205"}

Sending commands to the strip works fine. It’s the item state update when doing so that doesn’t work. E.g If I change to an effect called “Rainbow” through my Sitemap, this has a default speed and brightness. My sitemap items aren’t updated to reflect these defaults.

You need to add channels to your MQTT thing (All with the same state topic) and add a JSONPATH transformation (Click SHOW MORE) to the incoming transformation
Then link your items to that channel

Thanks. I guess I need to create a separate channel per item: ON/Off Switch, Effect, Color, Brightness and Speed.

Are you able to help me get going with such a JSONPATH transformation syntax? I have played a bit with it but not succeeded. I’m not a very experienced programmer and not sure how to do this.

Play with this: http://jsonpath.com/

If I write them for you, you won’t learn, you can practice on this website
Just paste your main JSON on the left and play with JSONPATH on the top, you’ll see the output on the right. LIVE!

1 Like

You are a tough teacher :slight_smile:

I entered the site and conclude this will get what I need for the switch:

$.state

I have created an ON/OFF switch channel with the same state topic as the string channel already in use and linked it to my switch item.

If I add this line as incoming value transformation

JSONPATH:$.state

I get this error in the log:

2019-12-17 11:55:47.011 [WARN ] [l.generic.ChannelStateTransformation] - Transformation service JSONPATH for pattern $.state not found!
2019-12-17 11:55:47.026 [WARN ] [eneric.internal.generic.ChannelState] - Command '{"state":"ON","color":{"r":100,"g":23,"b":10},"brightness":64,"effect":"solid","transition":0}' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.{"state":"ON","color":{"r":100,"g":23,"b":10},"brightness":64,"effect":"solid","transition":0}

Any hints where to go from here would be appreciated.

Did you install the JSONPATH transformation service?

No, because I’m an idiot. Installed it and I can now receive updates correctly. Thanks for the help. Now I just have to figure out how to do the rest…

Cool, the best to learn is make mistakes and do it yourself.
I hope I was not a bad teacher…
You should be able to figure out the rest by yourself.

Please tick the solution, thanks

1 Like

The clue

The important part
Transformation service JSONPATH … not found!
It’s installable.

Sorry to bother you again…
Using the switch channel, naturally I want to be able to manipulate the strip from the switch item, not just ensure it’s updated if switched elswhere. I tried keeping the rule, but I’ll get stuck in an infinite loop. Surely the easiest must be to send using the channel, right?

Using the Outgoing value format on the command topic, it doesn’t send anything to the broker if left empty (the topic is disappearing from MQTT explorer). If I set it to {“state”: “%s”}, it will be sent as {“state”: “”}. Do you know how I can send the switch state correctly to the topic as {“state”: “OFF”}?

Outgoing transformations are not very good for this binding at v2.4

transformationPatternOut would be one tool to use, but is simply not included at 2.4

formatBeforePublish should I think be useable for you. I think you need to escape the quotemarks though.

Thank you.
I assume “Outgoing value format” in paper UI is the same as formatBeforePublish. I tried skipping the quotation marks, but the result is the same - just without the quotation marks… It seems the %s parameter doesn’t provide any data…

I guess I’m out of luck then - until I can upgrade to 2.5…

Don’t know, but I would imagine so.

I was thinking more of escaping them.
Start simple, with just %s
Bear in mind minor changes to things may not be picked up by the binding without a restart.