Generic MQTT thing, state in JSON format

Hey Guys, need some beginner help, i have setup a generic MQTT thing, like yaml below

UID: mqtt:topic:e94f1500bd:dad57a579d
label: Eetkamer
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:e94f1500bd
channels:
  - id: eetkamer
    channelTypeUID: mqtt:switch
    label: Eetkamer
    description: ""
    configuration:
      commandTopic: homeassistant/light/eetkamer/set_light
      stateTopic: homeassistant/light/eetkamer/state
      off: '{ "state": "OFF" }'
      on: '{ "state": "ON" }'

Working great, i can turn on/off the light… but the i dont see the state changing when i change it externally, i think i need a json value for the state, the same like i do to send a payload to turn it on/off
how can i transform that ?

thnx

Change the configuration of the channel as follows:

  1. install JSONPATH Transformation Service
  2. set formatBeforePublish to : '{ "state" : "%s" }'
  3. set transformationPattern to: JSONPATH:$.state
  4. delete off and on value

should look like this:

configuration:
      commandTopic: homeassistant/light/eetkamer/set_light
      formatBeforePublish: '{ "state" : "%s" }'
      stateTopic: homeassistant/light/eetkamer/state
      transformationPattern: JSONPATH:$.state

Both formatBeforePublish and transformationPattern are advanced configuration, so you need to click “show advanced” to get it visible.

1 Like

Thnx , that works…
For some reason the channels are corrupted when they are auto discovered… So I need to make them all manual now… Are you using HA too?

No, never used HA (took a glance on it in the early state…)

Ah ok, np

But maybe you have a look what’s wrong with this thing that was auto discovered?

Read from this post:

No, sorry, no idea, I never rely on auto discovery…

Ok, np :slight_smile:

Then I need to create them one by one, a lof of work :frowning:

Of course. :slight_smile:
I’m using text configuration, there is so much to simply copy&paste, then only change details like thing name, Label and parts of topics (i.e. device part of topic)

Yeah, need to learn openhab still, so it’s possible to create things with yaml?

As far as I know: No.

I’m using good old *.things files. :sunglasses:
Of course you could create things and then copy&paste the configuration to the things yaml, but that’s not as nice as doing it via VSCode and simple text files.

Perfect, will have a look at the things file, thnx for info! Appreciated