MQTT 2 issues with Transform

Hi All,

I have just moved from the OLD MQTT binding to the new, I have few things working however I cannot seem to get my LED Strip light working via MQTT now isung transform (same setup on MQTT v1)

I have the following setup in ‘things’ file

Bridge mqtt:broker:broker [ host="192.168.178.3", secure="false" ]
    {
      Thing topic Bed_Light {
       Channels:
         Type switch : bedlight "Bed_Light" [stateTopic="bruh/bed/set",commandTopic="bruh/bed/set",transformationPattern="MAP:Bed_Light.map"]
        }
}

The following setup in my ‘items’ file:

**// ** Bed Light**
Switch bedlight **"Bed Light"** **<switch>** [ **"Switchable"** ] **{channel="mqtt:topic:broker:Bed_Light:bedlight" }**

My ‘map’ file has the following:

1={"state":"ON","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}
0={"state":"OFF","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}

When I trigger the ‘Switch’ from my Sitemap I can see it trigger in MQTT.fx with sending either “1” or “0”, the Transform is not taking place, instead I get the following error in my OPENHAB Logs:

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

2020-08-09 12:35:11.298 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command '{"state":"ON","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.{"state":"ON","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}

How do we now do the transform from “1” to a string etc in MQTT v2 ?

Is this about sending commands from openHAB? You have no transformationPatternOut set in your channel.

I don’t think it’s going to work out very well with identical stateTopic and commandTopic, you have constructed a feedback loop.

Yes this is about setting “switch” in openhab to send a command via MQTT to a ESP8266.

I have updated my .things file to:

      Thing topic Bed_Light {
       Channels:
         Type switch : bedlight "Bed_Light" [commandTopic="bruh/bed/set",transformationPatternOut="MAP:Bed_Light.map"]
        }

How ever I still get the same error:

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

2020-08-09 14:26:48.767 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command '{"state":"ON","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.{"state":"ON","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}

It looks to me like it does get the info from the MAP file, however it does not support a ‘string’ (I could be totally wrong…)

Well, in my switch configuration I use the transformationPattern and on the command way I just format my values ( formatBeforePublish).
TransformationPattern is in my configuration a JSONPATH that extracts the value from the mqtt payload for the switch. When I understand your configuration right that should be something like:
JSONPATH:$.state
Additionally I need to use the on and of settings of the channel of type switch to map my mqtt value 1 and 0 to the switch value ON and OFF, but I guess your LED provides already these values, so you may not need this.
On the comand way I just format the value, in your case I guess something like
{"state":%s,"color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}

BTW: you mapping file maps from 1,0 to a JSON. I guess you want to send that JSON to your command topic. If so you may try to map von ON/OFF. As far as I remember these are the values of the swich in openhab, not 1 and 0.

1 Like

What happens if you only send a state ON/OFF message via MQTT? Does your LED strip default to a different colour, brightness and effect?

This is because a Thing switch Channel can only deal with ON and OFF.
However, as @camillo mentions, you can use formatBeforePublish instead of transformationPatternOut to insert your ON/OFF into a JSON string. BUT this might not work on a switch Channel, depending on your MQTT binding version.

Assuming you’ve got the latest MQTT binding, then you can probably do (with string stolen and modified from @camillo) :

Thing topic Bed_Light {
       Channels:
         Type switch : bedlight "Bed_Light" [commandTopic="bruh/bed/set", formatBeforePublish="{\"state\":%s,\"color\":{\"r\":255,\"g\":244,\"b\":229},\"brightness\":255,\"effect\":\"solid\"}"]
        }

Thanks for this!
It appears to partially work…
When I check MQTT.fx i now get

{"state":1,"color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}

Written to the BedLight topic. How can I replace the “1” in the string to ON and “0” to OFF when it is sent?

Hmmm, not sure. Try:

Thing topic Bed_Light {
       Channels:
         Type switch : bedlight "Bed_Light" [commandTopic="bruh/bed/set", formatBeforePublish="{\"state\":%s,\"color\":{\"r\":255,\"g\":244,\"b\":229},\"brightness\":255,\"effect\":\"solid\"}", on="ON", off="OFF"]
        }

Did that help?

If not then from my point of view more input is needed.
What do you get on the state topic from the MQTT server and what does your light expect on the command topic?
What I got from your posts so far:
State and command are the same and look like:

{"state":"ON","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}
{"state":"OFF","color":{"r":255,"g":244,"b":229},"brightness":255,"effect":"solid"}

for on and off respectively.
Is that correct?

If so the JSAONPATH:$.state should yield an ON or OFF.
That is exactly what you need to change the value of the switch, becaus the values of the switch are ON and OFF.
Then, when you format your output in the way written by @hafniumzinc I would expect to see
{“state”:ON,“color”:{“r”:255,“g”:244,“b”:229},“brightness”:255,“effect”:“solid”}
on your command topic.
If your see a 1 instead of an ON maybe something else is still in your configuration that changes the values.

And: Maybe you need to enclose the %s with \" to get {"state":"ON", ... instead of {"state":ON, ...

Perfect! - This worked! Just got round to trying it.

Yes, What hafniumzinc had posted worked.
Thanks for the further info you had also posted! @camillo.

It appears when I setup a switch for MQTT the ON and OFF values are outputting as 0 or 1…
In the logs it states that the item has changed value to “ON” or “OFF” but I have a pretty simple setup and no other rules, transformations applying to any of my MQTT or switch setups…

I will live with what @hafniumzinc had posted on=ON off=OFF to do the conversion.

Thank you all for your help!

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